scanner class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cdm2883
    New Member
    • Sep 2007
    • 53

    scanner class

    I have this bit of code...

    System.out.prin tln ("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!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    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...)

    Comment

    • cdm2883
      New Member
      • Sep 2007
      • 53

      #3
      here is some more code...


      [code=java]
      . System.out.prin tln ("Enter the quantity: ");
      numInStock = scan.nextInt();

      System.out.prin tln ("Enter the unit price: ");
      priceEach = scan.nextDouble ();

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


      // create a variable named grossCost that is the
      // value of the numInStock times the priceEach
      double grossCost = (numInStock * priceEach);




      System.out.prin tln ("There are " + numInStock +
      " copies of the book " + bookName + " ! in stock which" +
      " cost " + priceEach + " each for a total value of "
      + fmt1.format(gro ssCost) + "");
      [/code]

      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...

      [code=java]
      NumberFormat fmt1 = NumberFormat.ge tCurrencyInstan ce();
      [/code]

      Thanks!
      Last edited by Nepomuk; Sep 19 '08, 07:37 AM. Reason: Added [CODE] tags

      Comment

      • cdm2883
        New Member
        • Sep 2007
        • 53

        #4
        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

        Comment

        • cdm2883
          New Member
          • Sep 2007
          • 53

          #5
          ok i figured out the scanner part. now i am having a problem the printing part. here is all of my code...

          [CODE=java]package mooredMod2;

          import java.util.Scann er;
          import java.text.Numbe rFormat;

          public class Ex4Expressions {

          public static void main(String[] args) {

          // we need the following line to use the scanner class
          Scanner scan = new Scanner(System. in);

          NumberFormat nf = NumberFormat.ge tCurrencyInstan ce();

          // define the following variables. You decide
          // the data types (but remember my comments from module 1).
          // Write your line of code after
          // my comment and LEAVE my comments in for
          // documentation. Do this in all of your submissions!!!!

          // a variable named numInStock that represents the
          // number of books in stock
          int numInStock ;

          // a variable named priceEach that represents the
          // cost for each book

          double priceEach ;

          // a variable named bookName that represents the name
          // of the book

          String name;



          // ask the user for the value of each of these
          // variables and read their answer
          // from the keyboard using the Scanner class.
          // you will need 6 total lines of code.
          // one for the prompt and one for the response for
          // each of the three variables

          System.out.prin tln ("Enter the quantity: ");
          numInStock = scan.nextInt ();

          System.out.prin tln ("Enter the unit price: ");
          priceEach = scan.nextDouble ();

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




          // value of the numInStock times the priceEach
          double grossCost = (numInStock * priceEach);

          scan.nextLine() ;



          System.out.prin tln ("There are " + numInStock +
          " copies of the book " + name + "! in stock which" +
          " cost " + nf.format(price Each) + " each for a total value of "+ nf.format(gross Cost) + "");

          }
          }[/CODE]


          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

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Try using next() instead of nextLine(). It seems that the last time you use the scanner (scan.nextDoubl e()), 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.

            Comment

            Working...