Results 1 to 3 of 3
  1. #1
    kylekep8 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default While loop issue.

    Hello,
    Thanks in advance for reading this. I have a rather simple question (difficult for me, but most likely not for some one you :P). To the point.. I simply want to prompt a user to continue entering in text until they provide nothing and press the enter key. I know there is something that I am missing. It is nothing as complex as using a keyevent or anything like that.

    Java Code:
     
    import java.util.*;
    
    public static void main (String [] args) {
    Scanner scan = new Scanner(System.in);
    String input = "";
    
       //this is what i need to break out of after no text is given and the enter key is pressed.
       while (...) {
         System.out.println("Please enter input here: ");
         input = scan.next();
      }
    }
    Anyways. This is a very basic layout of my code. The text itself is being pushed into a generic ADT, but that is really not relevant to this issue. Thanks again.

  2. #2
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: While loop issue.

    Java Code:
    while(!input.equals("")){
      System.out.println("Please enter input here: ");
      input = scan.next();
    }

    the ! means NOT.

    while(!input.equals("")) means while input doesn't equal "", or an empty string, do the following:...

    .equals() is only used in string comparisons, and it must be used in order for it to work right UNLESS you are working with nulls,
    which you aren't. We are working with an empty string. they are differen

    Another way to do it would be like this:
    //-----------------------------------------
    Java Code:
    package myPackage;
    import java.io.*;
    public class MyFirstClass {
        public static void main(String[] args)[B] throws IOException[/B]{
            String strUserInput = new String("Ham");
            InputStreamReader inputStreamReader = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(inputStreamReader);
            while(!name.equals("")){
                System.out.println("Type name:");
                strUserInput = reader.readLine();
                System.out.println("You typed: " + strUserInput);
            }
        }
    }
    //------------------------

  3. #3
    kylekep8 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: While loop issue.

    Got it thank you. My issue was using the scan.next(). Once I changed it to scan.nextLine() I was able to execute as was said. using the not equals. I believe the deliminator for the .next() is already an empty string so it continued on with the loop. Thanks again.

Similar Threads

  1. Conditional loop issue...
    By vb89 in forum New To Java
    Replies: 1
    Last Post: 02-02-2012, 09:11 PM
  2. issue with loop or IDE
    By neoginn in forum New To Java
    Replies: 3
    Last Post: 10-28-2011, 07:59 AM
  3. Loop Issue
    By burrish in forum New To Java
    Replies: 11
    Last Post: 09-25-2011, 10:26 PM
  4. Issue with nested for loop
    By sunshine64 in forum New To Java
    Replies: 5
    Last Post: 02-03-2011, 03:45 AM
  5. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 05:21 PM

Tags for this Thread

Posting Permissions

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