Need A Bit Of Java Help

CronosCronos Join Date: 2002-10-18 Member: 1542Members
<div class="IPBDescription">Limiting String input</div> If anyone could help me I'd be glad.

I'm doing java programming and I need to limit a string input variable. The input can have no repeats, no charecters, no double numbers (such as 1.1, "a", any /*>< etc etc) and needs to be a specific length. Not only that it also needs to have no repeats.

So, lets say I put in several numbers...

1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 8, 8, 9, 10, 11, 15, 15

What this program would have to do is this

1, 2, 3, 4, 5, 6, 7, 8

In essence igonoring, or destroying, any invalid inputs and trunating any additional inputs afterwards.

I've thought of using StringTokenizer to check for repeats but that doesnt take care of any kind of invalid inputs and nor does it take care of extra input.

As I said, any help on the matter would be appreciated.

Comments

  • DaxxDaxx Join Date: 2002-04-16 Member: 460Members, Constellation, Reinforced - Shadow
    If I'm reading this right, your limiting input to ONLY numbers, and they can't be repeated?
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    Yes, we MUST use a string though, no two ways about that, and there must be between 6 and 8 numbers in length but thats a program dependent variable that I can specify depending on my needs. I just cant seem to figure out the barebones code though.

    I've been bashing my skull for the last few days to no avail =(
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    Eh?... Just tokenise it, check each token for validity, maintain a count of the number of tokens total to see that it does exceed the limit, and use a data structure mapping booleans to numbers to check whether numbers have been repeated.

    Not hard <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    Code! Code! Code would be easier to understand at the moment D:

    I've tried tokenising it but when I compare it to a sample string, say, well, heres the code I have...

    StringTokenizer Tok = new StringTokenizer(Numbers);
    StringTokenizer Tok2 = new StringTokenizer(Winners);

    while(Tok.hasMoreTokens())

    {

    while(Tok.nextToken().equals(Tok2.nextToken()));

    {

    count++;

    }

    }

    Currently I get an output equal to the number of numbers that I put in, but not a count of the number of matches found in the strings.

    It's probably staring me in the face though...

    By the way, Numbers is the user input, Winners are the "Winning Numbers". As you may have guessed this is a psuedo lottery.
  • KungFuDiscoMonkeyKungFuDiscoMonkey Creator of ns_altair 日本福岡県 Join Date: 2003-03-15 Member: 14555Members, NS1 Playtester, Reinforced - Onos
    edited September 2004
    My suggestion probally not the best way to go about it but oh well

    Have a string tokenizer that divides up the inputs and have a string array with the same number of elemetns as the number of inputs in the first string.

    There would be a do while look to loop while there were still tokens. During each loop the token would be checked with the string array to make sure that value doesn't exist. If it doesn't then it's put in the next empty slot in the array. At the end you just convert the string array to a normal array and output that. You can also do your data validity check in the loop.

    Edit: Crapy psudo code

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    do(moretokens)
    {
     //check token for validity
     if(value good)
     {
       //check to see if value already exist
       for(i=0;i<stringarray.lenght;i++)
       {
         check for value exist
         if(value exist)
            exit loop
         else
            add to array of good values
         } //end for
       }// end if
    } // end loop
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    Not my best code since I'm too lazy to actually try it out myself <!--emo&:p--><img src='http://www.natural-selection.org/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    edited September 2004
    Doing that would be the last solution i'd try. I have to have valid inputs from 1 through to 99, and you cant count upwards using strings using a while loop.

    Stuff it, heres the whole program code;

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->

    import java.util.*;

    public class Lotto

    {

    public static void main(String[] args)

    {

     String Numbers; //initialise a String called Numbers, no value input as of yet
     int [] Comp = new int [7]; //an array of 8 integers called Comp
     int count = 0; //count set at zero
     String Winners = ""; //a String called Winners set at a blank as opposed to null value

     System.out.println("Please enter the numbers you wish to submit \nin the fashion 'x, x, x, x, x, x, x, x'");//user prompt for input so they chuck in a string of numbers, 1, 2, 3, 4, 5, 6, 7, 8

     Numbers = Keyboard.readString(); //Keyboard is a class that reads for key input, it's a program my lecturers gave to me and is required when the user needs to punch in values. In this case it's reading a string value

     for(int i = 0; i < Comp.length; i++)

     {

      Comp[i] = (int) (Math.random()*5);
      System.out.print(Comp[i] + ", ");

    //this for loop generates a set of random numbers between 0 and 5. This set will be expanded when the real thing comes along but is shortened for testing purposes. It also prints out the values so that I can see whats going on, not to be shown in the final version of course

     }

     for(int i = 0; i < Comp.length; i++)

     {

      Winners = Winners + Comp[i] + ", ";

    //this puts in the values generated for the Array Comp into the String Winners, the ", " is a delimiter to seperate the values and allow StringTokenizer to do it's work

     }

     System.out.println(); //A space :P

     System.out.println(Winners); //Prints out the String Winners to check if the output was solid. Will be in the final version such that the user can see what numbers the lottery program has drawn.

     StringTokenizer Tok = new StringTokenizer(Numbers); //String Tokenizes Numbers
     StringTokenizer Tok2 = new StringTokenizer(Winners); //String Tokenizes Winners

     while(Tok.hasMoreTokens()) //While Tok, which is the tokens of the string numbers, has more tokens
     
     {

      while(Tok.nextToken().equals(Tok2.nextToken())); //and while the next token from tok is the same as the next token from tok2

      {

       count++; //add to the count

      }

     }

    System.out.println(count); //print out the count once your done in the loop
       
    }

    }

    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    I think I got owned by your edit, now I'm not sure what your trying to suggest =\

    {Edit}

    Commentary Added.
  • KungFuDiscoMonkeyKungFuDiscoMonkey Creator of ns_altair 日本福岡県 Join Date: 2003-03-15 Member: 14555Members, NS1 Playtester, Reinforced - Onos
    edited September 2004
    add a counter var then

    Edit: and I'm still trying to figure out how your program is supposed to work <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->

    Edit2: and that's why you put comments in your code <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    I'll edit in some comments, give me a second
  • KungFuDiscoMonkeyKungFuDiscoMonkey Creator of ns_altair 日本福岡県 Join Date: 2003-03-15 Member: 14555Members, NS1 Playtester, Reinforced - Onos
    edited September 2004
    More bad psudocode

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    //Loop to throw out bad input (or you might not need this since the loop below would check any input against the winning numbers)

    //Loop to tokenize your winning lotto numbers into an array intLottoNumbers[]

    booleon[]  boolLottoNumbers = new booleon[intLottoNumbers.length]  //To hold a true/false value for each of the winning numbers

    //check lotto numbers
    do(while more tokens)  //do loop to go through all the tokens
     for(j=0;j<intLottoNumbers.length;j++)  //For loop to check the current token against all the lotto numbers
     {
        //test current token against intLottoNumbers[j]
        if(true)
            boolLottoNumbers[j]=true
     } //end for loop
    } //end do loop
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
  • KungFuDiscoMonkeyKungFuDiscoMonkey Creator of ns_altair 日本福岡県 Join Date: 2003-03-15 Member: 14555Members, NS1 Playtester, Reinforced - Onos
    The psedo code is probally rather messy but hopefully you can see my logic somewhere in there <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    I think I see what your trying to get at. If the input is not a number, and is not the same as the winning numbers, it's an invalid input? Yet if it's true count++ then?

    It might work. Not sure.
  • KungFuDiscoMonkeyKungFuDiscoMonkey Creator of ns_altair 日本福岡県 Join Date: 2003-03-15 Member: 14555Members, NS1 Playtester, Reinforced - Onos
    hopefully cleared it up a bit better
  • KungFuDiscoMonkeyKungFuDiscoMonkey Creator of ns_altair 日本福岡県 Join Date: 2003-03-15 Member: 14555Members, NS1 Playtester, Reinforced - Onos
    Just wanted a conformation on if you think that helps you out or if I was way off. Probally try to head to bed soon but wanted to see if that helped you out first.
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->

    import java.util.*;

    public class Lotto

    {

    public static void main(String[] args)

    {

     String Numbers;
     int [] Comp = new int [7];
     int count = 0;
     int Rho = 0;
     String Winners = "";

     System.out.println("Please enter the numbers you wish to submit \nin the fashion 'x, x, x, x, x, x, x, x'");

     Numbers = Keyboard.readString();

     for(int i = 0; i < Comp.length; i++)

     {

      Comp[i] = (int) (Math.random()*5);
      System.out.print(Comp[i] + ", ");

     }

     for(int i = 0; i < Comp.length; i++)

     {

      Winners = Winners + Comp[i] + ", ";

     }

     System.out.println();

     System.out.println(Winners);

     StringTokenizer Tok = new StringTokenizer(Numbers);
     StringTokenizer Tok2 = new StringTokenizer(Winners);

     while(Tok.hasMoreTokens())
     
     {

      count++;
      Tok.nextToken();

     }

     boolean [] TF = {false, false, false, false, false, false, false, false};  //To hold a true/false value for each of the winning numbers

     while(Tok.hasMoreTokens())  //do loop to go through all the tokens

     {
     
      for(int j = 0; j < count; j++)  //For loop to check the current token against all the lotto numbers
       
      {
       
       if(Tok.nextToken().equals(Tok2.nextToken()))

       {
           
        TF[j]=true;
        Rho++;

       }

       
      }
     }

     System.out.println(count);
     System.out.println(Rho);
       
    }

    }<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    As you can see, I've adapted your code into my program. Creating the boolean array was a bit of a problem since I've never had to do it before, apparently you have to set it by hand because it doesn't recognise an array dimension, nor does it have a default value.

    Still, even with this code I'm getting the same results. The new counting integer, Rho, never counts up because the if statement is never achieved but I cant for the life of me understand WHY!
  • KungFuDiscoMonkeyKungFuDiscoMonkey Creator of ns_altair 日本福岡県 Join Date: 2003-03-15 Member: 14555Members, NS1 Playtester, Reinforced - Onos
    so you're trying to find the number of lotto numbers they got right?
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    At the moment; Yes, validation should be taken care of by the code I've adapted (thanks for that). Rho is the success counter, Count is the number of tokens (and input incidentally) to be compared.

    Ultimately, an output like;

    System.out.println("Congratulations! You got " + Rho + " numbers right out of " + count);

    Damn it; Code infiltrating communications algorithms; panic++;
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    Okay, after a little finicking about, I've found that the method I'm using counts order as highly important.

    If for example I have a string of 1, 2, 3, 4, 5, and another as 1, 3, 2, 4, 5, it will only count 3 matches. My assignment requires order to be important only on special occasions (such as supplementary numbers.

    So, in plain english, what I need to do is take the first token of the first string and compare it against all the tokens of the second string, then take the second token of the first string and compare it against the second string etc etc.

    As of writing this my test code is this:

    import java.util.*;

    public class Aiur

    {

    public static void main(String[] args)

    {

    String A = "1 1 1 4 5";
    String B = "1 2 3 4 5";

    StringTokenizer Tok = new StringTokenizer(A);
    StringTokenizer Tok2 = new StringTokenizer(B);

    while(Tok.hasMoreTokens())
    {

    if(Tok.equals(Tok2))

    {

    System.out.println("HAPPY DAYS!");
    Tok.nextToken();

    }

    }

    }

    }

    Note that this is a test program to see whats going on. I've found that Tok never ever equals Tok2.

    The reason is because Tok and Tok2 are memory addresses (according the the SOP line anyway) and can thus never be equal to one another becauase you cant hold the different sets of data in the same place at the same time (unless you have a quantum singularity for a computer that is).

    So as far as I can tell, my conundrum is that I cant keep one token still and the other moving. They both appear to have to move.

    So, lets say Column A is the user input 1, 2, 5, 4, 3, and 1, 2, 3, 4, 5 is the winning numbers of Column B

    A|B|Value

    1|1|True
    1|2|False
    1|3|False
    1|4|False
    1|5|False
    2|1|False
    2|2|True
    2|3|False
    2|4|False
    2|5|False
    3|1|False
    3|2|False
    3|3|True
    3|4|False
    3|5|False
    4|1|False
    4|2|False
    4|3|False
    4|4|True
    4|5|False
    5|1|False
    5|2|False
    5|3|False
    5|4|False
    5|5|True

    Where true is the A == B if statement, or if(A.equals(B)), where a true will return a Rho++;

    If I can get it working like that I'm sure it will work, if only I could figure out the correct code D:
Sign In or Register to comment.