Results 1 to 11 of 11

Thread: Scanner

  1. #1
    choko is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default Scanner

    Hello,
    I have a question about Scanner. I have a loop and I want to break it when user writes blank line. But I can't solve it.

    Thanks for every advice

    Choko

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Depends on what you are inputting... strings, chars, ints?
    I'll give you sample and a link to the javadocs:
    Java Code:
    Scanner userInput = new Scanner(System.in);
    while (!(myVar = userInput.nextLine()).equals("")) 
          {
            //do stuff
          }
    It would probably be better to make the comparation against a string, like "quit", "goodbye" or "Hastalavistababy". That way you won't have an accidental "enter" that terminates the entry process.

    Another way would be:
    Java Code:
    loop* {
      myVar = userInput.nextLine()).equals(""));
      if (myVar.equals(""))
       {
         break;
       }
    
    //do stuff
    
    }//end loop
    * replace "loop" with for, while, do-while, etc

    Scanner (Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Gregorius is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Java Code:
    loop* {
      myVar = userInput.nextLine()).equals(""));
      if (myVar.equals(""))
       {
         break;
       }
    
    //do stuff
    
    }//end loop
    Unfortunately, that doesn't work. From what I know, that'll force you to change "myVar" to a boolean value before it can run (which it can't be).
    If you take out the "myVar" at the start of your loop*, and copy the "userInput..." into the if loop, it should work. So try this instead:


    Java Code:
    while (!(userInput.nextLine().equals(""))) {
      if (userInput.nextLine().equals(""))) {
         break;
       }
       else {
         //do whatever you want here.
       }
    
    } //end loop

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default oooppss...

    Sorry about that (cut & paste snafu)... let me correct that:
    Java Code:
    loop* {
      myVar = userInput.nextLine();
      if (myVar.equals(""))
       {
         break;
       }
    
    //do stuff
    
    }//end loop
    and whoa.... it appears you have a blopper too...

    Java Code:
    while (!(userInput.nextLine().equals(""))) [COLOR="red"]{//<-this does the same as...[/COLOR]
      if (userInput.nextLine().equals(""))) { [COLOR="red"]//<- ...this!!![/COLOR]     
    break;
       }
       else {
         //do whatever you want here.
       } //end loop}
    It's either one (while with imbedded comparation) or the other (if comparation). Can't have both of them.

    CJSL


    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Gregorius is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Sorry about that (cut & paste snafu)... let me correct that:
    Java Code:
    loop* {
      myVar = userInput.nextLine();
      if (myVar.equals(""))
       {
         break;
       }
    
    //do stuff
    
    }//end loop
    and whoa.... it appears you have a blopper too...

    Java Code:
    while (!(userInput.nextLine().equals(""))) [COLOR="red"]{//<-this does the same as...[/COLOR]
      if (userInput.nextLine().equals(""))) { [COLOR="red"]//<- ...this!!![/COLOR]     
    break;
       }
       else {
         //do whatever you want here.
       } //end loop}
    It's either one (while with imbedded comparation) or the other (if comparation). Can't have both of them.

    CJSL


    CJSL
    The one thing I notice about my little "blooper" is that as long as I have the while statement there, it'll work fine, but if I don't have that if statement in there, it will terminate after two runs regardless of whether "myVar" has an input or not.

    And no offence intended, but I just typed your code snippet out in Eclipse and it works fine up until you don't input anything. It just goes to a new line and won't terminate... perhaps we need some sort of boolean to make it work properly.
    Last edited by Gregorius; 01-24-2009 at 04:16 AM.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    And no offence intended,
    none taken... I'm cool.
    I just typed your code snippet out in Eclipse
    Which one? the "while" one or the "if" one? I run the programs on the command prompt and they work OK. Here's a working version of the "if" case:
    Java Code:
    while (true)
    {
     myVar = userInput.nextLine();
     if (myVar.equals(""))
       {
        System.out.println("nothing entered");
         break;
       }
      System.out.println("Something entered: " + myVar);
    }
     System.out.println("Outta da while loop !!!");
    The above works exactly like the "while" vers.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default Not trying to get in between you two...

    Just have a need to point things out... (i guess). CJSLMAN is using pseudocode, so hard to evaluate... Gregorius, well see for yourself.
    PHP Code:
    import java.util.*;
    public class CJSLMAN {
       public static void main(String[] args){
          Scanner userInput = new Scanner(System.in);
          int line = 0;
          String myVar = "";
          
          while(true) {
             myVar = userInput.nextLine();
             if (myVar.equals("")){ 
                break;
             }
             line++;
          }//end loop
          System.out.println("line= "+line);
       }
    }
    PHP Code:
    import java.util.*;
    public class Gregorius {
       public static void main(String[] args){
          Scanner userInput = new Scanner(System.in);
          int line = 0;
          int line2 = 0;
          
          while (!(userInput.nextLine().equals(""))) {
             if (userInput.nextLine().equals("")) // ) <- MISMATCH one extra ')'
             {
                break;
             }
             else {
                //do whatever you want here.
                line++;
             } //end loop}
                line2++;
          }
          System.out.println("line= "+line);
          System.out.println("line2= "+line2);
       }
    }
    Java Code:
    S:\temp>java CJSLMAN
    1
    2
    3
    4
    5
    
    line= 5
    Java Code:
    S:\temp>java Gregorius
    1
    2
    3
    4
    5
    
    line= 2
    line2= 2
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    CJSLMAN is using pseudocode, so hard to evaluate...
    I wouldn't say it's hard to evaluate... it just makes you think a little more :)

    Gregorius's version does work, but I would consider it very confusing because both scanner nextLine are doing the same thing.

    Just different points of view...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    Gregorius is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Well, I trust your judgments... only because Eclipse won't compile those codes, but everything else is fine... but if you've got the proof that you're right; hey, what can I do? You're right.

    Glad I was able to have the chance to help, albeit not very much.

  10. #10
    choko is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default My solution

    First, I want to thank you.

    I have found one more solution on Internet:

    PHP Code:
    		Scanner sc = new Scanner(System.in);
    		while(sc.hasNextLine()) {
    			Scanner ss = new Scanner(sc.nextLine());
    			
    			if(!ss.hasNextLine()) {
    				break;
    			}
    			
    			while(ss.hasNextInt()) {
    				System.out.println(ss.nextInt());			
    			}
    			
    			ss.close();
    		}
    		sc.close();
    Choko

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Very similiar..

    The code that you show is very similiar to Gregorius's code... I think the most important difference is that the code you show closes the scanner, which is a very good idea.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. how to use Scanner with a number
    By cew27 in forum New To Java
    Replies: 10
    Last Post: 04-03-2009, 07:23 PM
  2. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 02:41 PM
  3. Tokenizing with Scanner
    By sireesha in forum New To Java
    Replies: 3
    Last Post: 02-05-2008, 09:44 PM
  4. MixedTypeInput example (Scanner)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 03:11 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:00 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
  •