Java help

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
I'm writing a program for CS that has to check an input string to see if it basically has any two letters that are the same. Well, everything is basically done and now I'm going back and doing the other small stuff that could be major. Anyway, this method needs to check for errors, and my first is checking for a space in the input because it is only supposed to be for one word. Here is my method:

public static String checkInput(String errCheck) throws IOException
{
if (errCheck.indexOf(" ") != - 1)
{
System.out.print("Please enter a single word: ");
errCheck = dataIn.readLine();
while (errCheck.indexOf(" ") != - 1) checkInput(errCheck);
}
return errCheck;
}
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
The problem is this:

Working example:
input: bob hello
output: Please enter a single word:
input: hello
output: answer accepted

Not working from same code as above:
input: bob hello
output: Please enter a single word:
input: hello bob
output: Please enter a single word:
input: hello
output: Please enter a single word:
...
output: Please enter a single word:
...
output: Please enter a single word:

the ... represents anything I type with or without a space. The string the method receives in the raw string the user inputted. Again, both those examples are from the same code except second doesn't stop asking.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
I can't get to a compiler right now ,so I can't verify this, but ...
[pseudocode]

public static boolean checkInput( String errCheck) {

errCheck.trim() //Removes leading and trailing whitespace
if ( errCheck.indexOf(" " ) == -1 ) {
return true;
} else {
return false;
}
}
....... and somehwere else ....

try{
do {
System.out.println( "Please enter a single word: " );
errCheck = dataIn.readLine();
} while ( checkInput( errCheck) == false );
} catch (IOException e ) {
System.err.println( "IOException in readLine()" );
System.err.println( e.getMessage() );
e.printStackTrace();
System.exit(1);
}
...or something to that extent. syntax is probably off somewhere, I can't check it right now. It looks like in your example you have a heck of a lot of recursion going on there, more than I think you should.


Lemme know if it helps.

-Josh

Edit: Added a brace, rearranged, typo
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81
public static String checkInput(String errCheck) throws IOException
{
if (errCheck.indexOf(" ") != - 1)
{
System.out.print("Please enter a single word: ");
errCheck = dataIn.readLine();
while (errCheck.indexOf(" ") != - 1) checkInput(errCheck);
}
return errCheck;
}
I'm pretty sure the while loop is causing your problem, and I don't think it even needs to be there.

When you first call checkInput, and it prompts for a new string, errCheck becomes the new string. If errCheck is still wrong then it enters the while loop and is never given the chance to change. errCheck from the first iteration stays wrong forever.
 

Daishiki

Golden Member
Nov 9, 2001
1,943
36
91
if possible, you could use a StringTokenizer with the space as a delimiter and check if there are more than one token, which would mean more than one word.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: diegoalcatraz
I can't get to a compiler right now ,so I can't verify this, but ...
[pseudocode]

public static boolean checkInput( String errCheck) {

errCheck.trim() //Removes leading and trailing whitespace
if ( errCheck.indexOf(" " ) == -1 ) {
return true;
} else {
return false;
}
}
....... and somehwere else ....

try{
do {
System.out.println( "Please enter a single word: " );
errCheck = dataIn.readLine();
} while ( checkInput( errCheck) == false );
} catch (IOException e ) {
System.err.println( "IOException in readLine()" );
System.err.println( e.getMessage() );
e.printStackTrace();
System.exit(1);
}
...or something to that extent. syntax is probably off somewhere, I can't check it right now. It looks like in your example you have a heck of a lot of recursion going on there, more than I think you should.


Lemme know if it helps.

-Josh

Edit: Added a brace, rearranged, typo

I'm going to try your's in some time, it seems to be more complex compared to what Yomicron suggested.

Yomicron: Instead of that while loop, I used this code:

public static String checkInput(String errCheck) throws IOException
{
boolean boolCheck = false;
while(!boolCheck)
{
if (errCheck.indexOf(" ") != - 1)
{
System.out.print("Please enter a single word: ");
errCheck = dataIn.readLine();
if (errCheck.indexOf(" ") == - 1) boolCheck = true;
}
}
return errCheck;
}

This time, after I enter two words, I do that again when it asks for a single word. The next time it asks, I enter a single word "bob" where it returns an error. I get an exception in "main" thread error along with String index out of range: 3 error. I'm going to try diegoalcatraz's idea, but if I can get something simpler to work, that would be best.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: xUCIxDaiSHi
if possible, you could use a StringTokenizer with the space as a delimiter and check if there are more than one token, which would mean more than one word.

Whats StringTokenizer? I'm still new to java programming, so I have quite a bit to study.
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81
I thnk you are making it more complicated than it needs to be, this should work (i think).

public static String checkInput(String errCheck) throws IOException
{
if (errCheck.indexOf(" ") != - 1)
{
System.out.print("Please enter a single word: ");
errCheck = dataIn.readLine();
errCheck = checkInput(errCheck);
}
return errCheck;
}

Note, it's been a while since I've done any programming and I don't have access to a java complier right now, so this has not been tested any place other than my head.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
String tokenzier takes a string, and a delimiter (what you use to separate "tokes"), and then you can advance through those tokens. For example, take the string:

"Hello world program example". You'd use the default delimiter (usually whitespace and newlines, things like that). You create a new stringTokenizer

StringTokenizer tok = new StringTokenizer( String, delimiter ); //I think you can leave delimiter out, if you want default

Then you'd call tok.nextToken() to get the first "word". That's what you'd want, in this example. Next we'd call

if ( tok.nextToken() != NULL ) {
...something's wrong.

That second call would return NULL if there was only one word in the string. However, if there is more than one, it would return the next word, etc.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: Yomicron
I thnk you are making it more complicated than it needs to be, this should work (i think).

public static String checkInput(String errCheck) throws IOException
{
if (errCheck.indexOf(" ") != - 1)
{
System.out.print("Please enter a single word: ");
errCheck = dataIn.readLine();
errCheck = checkInput(errCheck);
}
return errCheck;
}

Note, it's been a while since I've done any programming and I don't have access to a java complier right now, so this has not been tested any place other than my head.

Actually, I tried that, but it just hands there after I enter in a correct answer I get the same error I described earlier. I'll look into it because I think its something simple I'm missing. THanks for the help.
 

Daishiki

Golden Member
Nov 9, 2001
1,943
36
91
yea, if you did:

StringTokenizer st = new StringTokenizer(theString);

the default delimiter is the space, " ".
so if i tokenized "the string" with the " " delimiter, "the" would be the first token of type String, "string" would be the second.

so you just do something like
String x;
if(st.hasMoreTokens()) x = st.nextToken(); //puts the first word into x
if you call st.hasMoreTokens() again and it returns true, then there's a second word, false if not.
import java.io.StringTokenizer if you're going to use it.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Hrmm... its working now after I rebooted, I don't know what the problem was, but I ended up just making the change Yomicron suggested.

xUCIxDaiSHi: Is there any advantage to using the StringTokenizer over just calling the method again?
 

Daishiki

Golden Member
Nov 9, 2001
1,943
36
91
Originally posted by: AgaBooga
Hrmm... its working now after I rebooted, I don't know what the problem was, but I ended up just making the change Yomicron suggested.

xUCIxDaiSHi: Is there any advantage to using the StringTokenizer over just calling the method again?

whatever floats your boat. if you got it working now, don't change it.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Originally posted by: xUCIxDaiSHi
yea, if you did:

StringTokenizer st = new StringTokenizer(theString);

the default delimiter is the space, " ".
so if i tokenized "the string" with the " " delimiter, "the" would be the first token of type String, "string" would be the second.

so you just do something like
String x;
if(st.hasMoreTokens()) x = st.nextToken(); //puts the first word into x
if you call st.hasMoreTokens() again and it returns true, then there's a second word, false if not.
import java.io.StringTokenizer if you're going to use it.

I think the default limiter is actually any whitespace, eg. space, multiple spaces and tabs.

StringTokenizer is a pretty handy class :)
 

Daishiki

Golden Member
Nov 9, 2001
1,943
36
91
my mistake, forgot to mention those

from the api:
Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.

Originally posted by: screw3d


I think the default limiter is actually any whitespace, eg. space, multiple spaces and tabs.

StringTokenizer is a pretty handy class :)