473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

scanner class

53
I have this bit of code...

System.out.println ("Enter the name of book: ");
bookName = scan.nextLine();


then i have code after that that prints out what i want it to. But when it gets to this part of my code it just jumps the println methods i have called. What little code snipet do i need for it to let me type in the name of the book before it moves on?


Thanks!
Sep 19 '08 #1
5 1618
Laharl
849 Expert 512MB
That sounds like you have leftover data in the input buffer that the Scanner grabs, assuming it's the next line. Can we see more code? (I know there's a way to fix this, but can't remember it offhand...)
Sep 19 '08 #2
cdm2883
53
here is some more code...


Expand|Select|Wrap|Line Numbers
  1. .        System.out.println ("Enter the quantity: ");
  2.         numInStock = scan.nextInt();
  3.  
  4.         System.out.println ("Enter the unit price: ");
  5.         priceEach = scan.nextDouble();
  6.  
  7.         System.out.println ("Enter the name of book: ");
  8.         bookName = scan.nextLine();
  9.  
  10.  
  11.         // create a variable named grossCost that is the
  12.         // value of the numInStock times the priceEach
  13.         double grossCost = (numInStock * priceEach); 
  14.  
  15.  
  16.  
  17.  
  18.         System.out.println ("There are " + numInStock + 
  19.         " copies of the book " + bookName + " ! in stock which" +
  20.         " cost " + priceEach + " each for a total value of "
  21.         + fmt1.format(grossCost) + "");
  22.  
and as well if you could help with the formating of currency. i have this and eclipse says it can not be resolved as a type...

Expand|Select|Wrap|Line Numbers
  1. NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
  2.  
Thanks!
Sep 19 '08 #3
cdm2883
53
ok so i figured out the currency part, i forgot to import the numberFormat. but i still can not figure out the scanner class part



Thanks
Sep 19 '08 #4
cdm2883
53
ok i figured out the scanner part. now i am having a problem the printing part. here is all of my code...

Expand|Select|Wrap|Line Numbers
  1. package mooredMod2;
  2.  
  3. import java.util.Scanner;
  4. import java.text.NumberFormat;
  5.  
  6. public class Ex4Expressions {
  7.  
  8. public static void main(String[] args) {
  9.  
  10.         // we need the following line to use the scanner class    
  11.         Scanner scan = new Scanner(System.in);
  12.  
  13.         NumberFormat nf = NumberFormat.getCurrencyInstance();
  14.  
  15.         // define the following variables.  You decide
  16.         // the data types (but remember my comments from module 1).
  17.         // Write your line of code after
  18.         // my comment and LEAVE my comments in for
  19.         // documentation. Do this in all of your submissions!!!!
  20.  
  21.         // a variable named numInStock that represents the
  22.         // number of books in stock
  23.         int numInStock ; 
  24.  
  25.         // a variable named priceEach that represents the 
  26.         // cost for each book
  27.  
  28.         double priceEach ;
  29.  
  30.         // a variable named bookName that represents the name 
  31.         // of the book
  32.  
  33.         String name; 
  34.  
  35.  
  36.  
  37.         // ask the user for the value of each of these 
  38.         // variables and read their answer
  39.         //     from the keyboard using the Scanner class.
  40.         // you will need 6 total lines of code.
  41.         // one for the prompt and one for the response for 
  42.         // each of the three variables
  43.  
  44.         System.out.println ("Enter the quantity: ");
  45.         numInStock = scan.nextInt ();
  46.  
  47.         System.out.println ("Enter the unit price: ");
  48.         priceEach = scan.nextDouble ();
  49.  
  50.         System.out.println ("Enter the name of book: ");
  51.          name = scan.nextLine ();
  52.  
  53.  
  54.  
  55.  
  56.         // value of the numInStock times the priceEach
  57.         double grossCost = (numInStock * priceEach); 
  58.  
  59.         scan.nextLine();
  60.  
  61.  
  62.  
  63.         System.out.println ("There are " + numInStock + 
  64.         " copies of the book " + name + "!  in stock which" +
  65.         " cost " + nf.format(priceEach) + " each for a total value                         of "+ nf.format(grossCost) + "");
  66.  
  67.     }
  68. }

there is the code. the problem i am having is where the name is. im tryin to print out the name of the book the person typed in. but when i run it nuthing comes up for the name. any help on that?


Thanks
Sep 19 '08 #5
Ganon11
3,652 Expert 2GB
Try using next() instead of nextLine(). It seems that the last time you use the scanner (scan.nextDouble()), you get the double value, but it leaves the newline character '\n' in the scanner buffer. When you call nextLine(), scan immediately sees that '\n', thinks it is done, gets rid of that '\n', and continues.
Sep 19 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Dan =o\) | last post by:
Hey guys, I wonder if you could please provide me with some ideas as to how to get around this problem. Symbol MC9000-k, Pocket PC 2003... With a scanner application I've written, data is...
7
by: DemonWasp | last post by:
I've been having some trouble getting the Scanner class to operate the way I'd like. I'm doing some fairly basic file IO and I can't seem to get the class to load the last line/token any way I try....
1
madhoriya22
by: madhoriya22 | last post by:
Hi, My JVM does not support java.util.Scanner class. Is it too new or too old for Scanner class. Currently I am working on jdk1.5.0_04. While using Scanner class java compiler shows this message :-...
14
tolkienarda
by: tolkienarda | last post by:
hi all i read the scanner class documentation from sun's website and i thought i would have some fun trying to write a program that stores a line from the user to var input and then parses it on...
3
by: thename1000 | last post by:
Hi, I'm trying to create this output: Input team 1's name: Team 1 Input team 1's ranking: 90.4 etc.
3
by: Dameon99 | last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the...
7
by: kidosai | last post by:
hi i need a guide on how to use the scanner class... i need to use Java Scanner class to read a text file and be able to print the text inside of it... example : i have a text file named...
2
by: bvav22 | last post by:
Hey guys, im trying to finish an assignment for a java class, and i have finished the program except for one part. The program requires that i take 3 values inputted from the user, the first 2 must...
6
by: rotaryfreak | last post by:
Hi everyone, ive had this problem for a while and i cant seem to figure out why. I am using eclipse to create my java code. When import the Scanner class, create a new object and so on... ...
1
by: Aycex | last post by:
I am taking my first java class as i am a Database person this is quite interesting and here is my problem. We are supposed to be learning to overload a class and i believe i have my code correct...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.