Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: problem with Scanner nextLine() method

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default problem with Scanner nextLine() method

    I'm working on a code that involves a scanner, and it uses nextLine() method for reading input, but when that lines executes, it jumps to the next line, not giving opportunity to enter the String....... here's the code

    System.out.println("Description:");
    String description = myscan.nextLine();
    System.out.println("Price:");
    double price = myscan.nextDouble();
    System.out.println("Quantity:");
    int qty = myscan.nextInt();

    any help appreciated


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: problem with nextLine() method...

    Hello mia_tech,

    Take a look at this code. It solves your problem:

    import java.util.Scanner;
     
    public class Mia_tech {
     
        public static void main(String[] args) {
     
            System.out.println("Description:");
     
            Scanner myscan = new Scanner(System.in);
            String description = myscan.nextLine();
     
            System.out.println("Price:");
            double price = myscan.nextDouble();
     
            System.out.println("Quantity:");
            int qty = myscan.nextInt();
     
            System.out.println("Description: " + description);
            System.out.println("Price: " + price);
            System.out.println("Quantity: " + qty);
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM
  2. How to display the contents of my queue?
    By rocafella5007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2009, 11:46 AM
  3. Replies: 2
    Last Post: March 4th, 2009, 06:32 AM
  4. Replies: 1
    Last Post: December 30th, 2008, 07:30 AM

Tags for this Thread