Results 1 to 6 of 6

Thread: Scanner object

  1. #1
    xxsokol32xx is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Post Scanner object

    So I am very new to Java and programming in general and have been player around with the scanner object. my ultimate goal with this particular project that i am playing with is to have the user input two numbers then choose if they want to add or subtract the two numbers. I am getting stuck at the point where I ask the user whether they want to add or subtract. for some reason the program skips the input.nextLine() method but it works with input.next()...ive been searching around for explanations on the two and checked the oracle site for the formal definitions but still cant seem to understand why it does not work. Here is the code that I have right now that stops after printing for the user if they want to add/sub...

    Java Code:
    import java.util.Scanner;
    
    public class apple {
    	public static void main (String[] args){
    		
    		double ans, fnum, snum;
    		
    		String operation;
    		
    		Scanner input = new Scanner (System.in);
    		
    		System.out.println("Please input first number");
    		fnum = input.nextDouble();
    		
    		System.out.println("Please input second number");
    		snum = input.nextDouble();
    		
    		System.out.print("Do you want to add or subtract?:");
    		operation = input.nextLine();
    		
    		System.out.println(operation);
    		
    		
    			
    		
    	}
    	
    	
    	
    }
    as far as I understand, nextLine() and next() are both used for stings but I must be missing something... I appreciate any and all help. Thank you

    also any resources that have helped you in the beginning stages are always welcome.

  2. #2
    Gio!? is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Scanner object

    Quote Originally Posted by xxsokol32xx View Post
    So I am very new to Java and programming in general and have been player around with the scanner object. my ultimate goal with this particular project that i am playing with is to have the user input two numbers then choose if they want to add or subtract the two numbers. I am getting stuck at the point where I ask the user whether they want to add or subtract. for some reason the program skips the input.nextLine() method but it works with input.next()...ive been searching around for explanations on the two and checked the oracle site for the formal definitions but still cant seem to understand why it does not work. Here is the code that I have right now that stops after printing for the user if they want to add/sub...

    as far as I understand, nextLine() and next() are both used for stings but I must be missing something... I appreciate any and all help. Thank you

    also any resources that have helped you in the beginning stages are always welcome.

    I'm also not as advanced in Java like you, but I maybe able to help you.

    From what I understand you want people to choose between adding or substracting and they have to typ down whatever they want.

    Java Code:
    import java.util.Scanner;
    public class Main
    {
        public static void main(String[] args)
        {
            double ans, fnum, snum;
    
            Scanner input = new Scanner (System.in);
    
            System.out.print("Do you want to add or subtract?:");
            String operation = input.nextLine();
    
            System.out.println("Please input first number");
            fnum = input.nextDouble();
    
            System.out.println("Please input second number");
            snum = input.nextDouble();
    
            
            if (operation.equals("add"))
            {
                ans = fnum + snum;
                System.out.println("Answer: " + ans);
            }
            else if (operation.equals("substract"))
            {
                ans = fnum - snum;
                System.out.println("Answer: " + ans);
            }
            else
            {
                System.out.println("Not valid command");
            }
        }
    
    }

    I tested the input.nextLine(); myself and it should work like this
    Last edited by Gio!?; 05-03-2012 at 02:58 AM.

  3. #3
    xxsokol32xx is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Scanner object

    i ran your code and i recieved this

    Please input first number
    2
    Please input second number
    3
    Do you want to add or subtract?:Not valid command

    so maybe something is wrong with my eclipse?
    it appears that it wants to continually skip the code on line 21

  4. #4
    Gio!? is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Scanner object

    I ran the code in eclipse too, it gives the same error.

    BUT when I ask for the question "Do you want to add or subtract?" before you insert both doubles, it does work for some reason...

    In netbeans it doesn't give an error at all =s

  5. #5
    xxsokol32xx is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Scanner object

    hmmm there must be something about the scanner class that I do not understand...either that or it is a bug with eclipse...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,862
    Rep Power
    19

    Default Re: Scanner object

    Yep.
    Which is one of the reasons I dislike it.

    Anyway (and I really ought to have this in a file somewhere so I can copy and paste it in, the number of times it comes up):

    Scanner has a buffer.
    The Scanner methods nextXXX() all clear that buffer and read in the next token, in the default case up to a new line.
    However it does not read in the new line character, which is left in the buffer.
    This is OK in the case of nextInt,, nextDouble etc because, as I say, they all clear the buffer out before looking for a token.

    The problem comes in with nextLine(). This does not clear the buffer. So if there is a new line character left over from (eg) a nextInt() call, then the nextLine() call will return an empty string because there is a new line in the buffer.
    The solution to this is to make an extra call to nextLine() after your last nextXXX() call to clear that character out.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 05-09-2013, 08:51 AM
  2. How do I reset my Scanner object ?
    By fatabass in forum New To Java
    Replies: 7
    Last Post: 01-13-2012, 09:09 PM
  3. Problem Of Scanner Object with its method nextLine()
    By Cluster Storm in forum AWT / Swing
    Replies: 12
    Last Post: 06-17-2010, 05:40 PM
  4. Replies: 3
    Last Post: 09-24-2008, 11:59 PM
  5. Replies: 1
    Last Post: 12-21-2007, 11:12 AM

Posting Permissions

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