Results 1 to 6 of 6
Thread: Scanner object
- 05-03-2012, 02:12 AM #1
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
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...
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 youJava 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); } }
also any resources that have helped you in the beginning stages are always welcome.
- 05-03-2012, 02:37 AM #2
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Re: Scanner object
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 thisLast edited by Gio!?; 05-03-2012 at 02:58 AM.
- 05-03-2012, 02:59 AM #3
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
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
- 05-03-2012, 03:09 AM #4
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
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
- 05-03-2012, 03:38 AM #5
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
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...
- 05-03-2012, 10:11 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
Similar Threads
-
How do I scan a photo from my scanner into an ImageIcon object
By fortwnty420 in forum Advanced JavaReplies: 6Last Post: 05-09-2013, 08:51 AM -
How do I reset my Scanner object ?
By fatabass in forum New To JavaReplies: 7Last Post: 01-13-2012, 09:09 PM -
Problem Of Scanner Object with its method nextLine()
By Cluster Storm in forum AWT / SwingReplies: 12Last Post: 06-17-2010, 05:40 PM -
Why do I need to declare a new Scanner object here?
By Chase in forum New To JavaReplies: 3Last Post: 09-24-2008, 11:59 PM -
Regarding Scanner Object -- Cannot Resolve the Symbol
By pascal45 in forum New To JavaReplies: 1Last Post: 12-21-2007, 11:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks