Results 1 to 5 of 5
Thread: String in While loop
- 02-20-2011, 03:45 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
String in While loop
I am trying to create a while loop that reads the users input which is string. If the string doesnt match it will ask the user again to give the input. When i tired to create while loop it doesnt exit even though the string match. here is something i did.
System.out.println("What surface do you want?");
Scanner object = new Scanner(System.in);//creating the scanner object
String x = object.next();//scans from keyboard
while((!x.equalsIgnoreCase("t")) || (!x.equalsIgnoreCase("x")) ||(!x.equalsIgnoreCase("g")))
{
System.out.println("What surface do you want?");
x = object.next();
}
-
Use a Venn diagram and you'll see that the condition in your while loop is always true and so the loop will never end. Or use logic: At least two out of the three conditions will always be true, and since you're "or-ing" the three conditions, the whole statement will always be true.
Perhaps "or" isn't the answer (hint).Last edited by Fubarable; 02-20-2011 at 03:51 PM.
- 02-20-2011, 03:50 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
1. Use code tags.
2. Use && instead of ||. You have problem with your logic statement.
- 02-20-2011, 04:16 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
i kinda figure out how to do. Now it looks like this
while(!((x.equalsIgnoreCase("t")) || (x.equalsIgnoreCase("x")) ||(x.equalsIgnoreCase("g"))))
how ever new problem showed up. It did what i wanted to do but i want to ask the user if they want to ask the new object and repeat the process. like if the x equals the users in put it does the calculation and ask if they want to do new calculation. How to use while loop on this.
- 02-20-2011, 04:19 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
current code
import java.util.Scanner;
public class AreaDriver
{
public static void main(String[] args)
{
System.out.print("Choose a solid surface: ");
Scanner object = new Scanner(System.in);//creating the scanner object
String x = object.next();//scans from keyboard
while(!((x.equalsIgnoreCase("sphere")) || (x.equalsIgnoreCase("box")) ||(x.equalsIgnoreCase("cone")) || (x.equalsIgnoreCase("n"))))
{
System.out.print("Choose a solid surface: ");
x = object.next();
}
if(x.equalsIgnoreCase("sphere"))
{
does something
}
else if(x.equalsIgnoreCase("box"))
{
does something
}
else if (x.equalsIgnoreCase("cone"))
{
does something
}
}
}
i want user to ask here if they want to do it again. Whether is new or repeat. the whole processs.
Similar Threads
-
string loop
By durdanto in forum New To JavaReplies: 4Last Post: 02-18-2011, 09:06 AM -
Exiting a while loop using a String?
By Isshin in forum New To JavaReplies: 4Last Post: 02-21-2010, 11:33 PM -
String and while loop
By Exception in forum Java AppletsReplies: 5Last Post: 09-24-2009, 12:32 PM -
Using string to terminate loop
By mrblippy in forum New To JavaReplies: 3Last Post: 04-23-2009, 06:16 AM -
terminating a while loop with a string
By tkdvipers in forum New To JavaReplies: 3Last Post: 07-09-2007, 11:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks