Results 1 to 5 of 5
  1. #1
    nebulared is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default 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();
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    1. Use code tags.
    2. Use && instead of ||. You have problem with your logic statement.

  4. #4
    nebulared is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    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.

  5. #5
    nebulared is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    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

  1. string loop
    By durdanto in forum New To Java
    Replies: 4
    Last Post: 02-18-2011, 09:06 AM
  2. Exiting a while loop using a String?
    By Isshin in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 11:33 PM
  3. String and while loop
    By Exception in forum Java Applets
    Replies: 5
    Last Post: 09-24-2009, 12:32 PM
  4. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 06:16 AM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-09-2007, 11:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •