Home arrow static arrow Java Programming [Archive] - While Problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - While Problem
This topic has 7 replies on 1 page.

Posts:2
Registered: 2/19/04
While Problem  
Jun 23, 2004 11:33 PM



 
System.out.print ( "\nAre you on the (G)raduate or (U)nderGraduate Program ?: ");
System.out.flush();
Grad = datainput.readLine();
while (Grad.substring(0) != "G" || Grad.substring(0) != "U" )
{System.out.print ( "\tEnter (G) for Graduate or (U) for UnderGraduate Program ?: " );
System.out.flush();
Grad = datainput.readLine();}

System.out.print ( "\nDo you want a Locker for $" + oStuLocker + " (Y/N) ?: ");

=================================================================================

I'm basically validating my input whether a G or U.... Even If i type in as a G or g my program won't goto
to the next line (which is the Locker room question) .. any suggestion ?
 

Posts:5,119
Registered: 16.08.01
Re: While Problem  
Jun 23, 2004 11:39 PM (reply 1 of 7)



 
When you post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read.
while (Grad.substring(0) != "G" && Grad.substring(0) != "U" ) // && NOT ||
 

Posts:1,135
Registered: 1/16/04
Re: While Problem  
Jun 23, 2004 11:40 PM (reply 2 of 7)



 
for strings you use equals()... Check java.lang.String API for usage...
while( !Grad.substring(0).equals("G") || !Grad.substring(0).equals("U")) {// do something ...


While you are looking at the java.lang.String, check out the toLowerCase() method...

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:1,135
Registered: 1/16/04
Re: While Problem  
Jun 23, 2004 11:43 PM (reply 3 of 7)



 
and what PhHein said...

- M...

- ' H... '
 

Posts:5,119
Registered: 16.08.01
Re: While Problem  
Jun 23, 2004 11:47 PM (reply 4 of 7)



 
Bummer, never noticed the == :P
 

Posts:56
Registered: 18/06/03
Re: While Problem  
Jun 23, 2004 11:48 PM (reply 5 of 7)



 
for strings you use equals()... Check java.lang.String
API for usage...

...or even equalsIgnoreCase(). Which (like first converting to lowercase) makes sure both "G" and "g" give the same results.

While you are looking at the java.lang.String, check
out the toLowerCase() method...

- negala
 

Posts:2,206
Registered: 8/15/02
Re: While Problem  
Jun 24, 2004 2:27 AM (reply 6 of 7)



 
Course this would work to...
while ( Grad.charAt(0) != 'G' || Grad.charAt(0) != 'U' ) { 
 

Posts:1,135
Registered: 1/16/04
Re: While Problem  
Jun 24, 2004 3:45 AM (reply 7 of 7)



 
Bummer, never noticed the == :P
and I forgot about equalsIgnoreCase() :-P
 
This topic has 7 replies on 1 page.