Need A Bit Of Java Help
<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.
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
I've been bashing my skull for the last few days to no avail =(
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-->
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.
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-->
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.
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-->
<!--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-->
It might work. Not sure.
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!
Ultimately, an output like;
System.out.println("Congratulations! You got " + Rho + " numbers right out of " + count);
Damn it; Code infiltrating communications algorithms; panic++;
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: