Results 1 to 7 of 7
  1. #1
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default input nextLine error

    Someone please help me with my delemma. My following code works fine but there is one major flaw that I am seeing. I am sure there are many if you are advanced level coder. This is for a beginner course. So please help me with the simplest solution possible because the class has not dealt with advance level syntax.

    My input is supposed to be "band name 6", "band name 8" and so forth.
    In the first line within the While loop, I really need to use nextLine because the band name may be 2 words. When I do change it to nextLine, I get input mistmatch exception error.

    Java Code:
    public class Main
    {
        private static int totalCD;
    
        public static void main(String[] args)
        {
            Map<String, Integer> listBand = new TreeMap<String, Integer>();
            Scanner in = new Scanner(System.in);
            boolean done = false;
            System.out.print("Enter band name and # of CD (q to quit): ");
    
            while (!done)
            {
                String bandName = in.next();  [B]//if I change it to in.nextLine(), I get runtime error when I run the program. It is a input mismatch exception error[/B]
                if (bandName.equalsIgnoreCase("Q"))
                {
                    listBand.remove("q");
                    done = true;
                }
                else
                {
                    int numberOfCD = in.nextInt();
                    listBand.put(bandName, numberOfCD);
                }
    
            }
    
            Set<String> keySet = listBand.keySet();
            int count = 0;
            for (String key : keySet)
            {
                int value = listBand.get(key);
                System.out.printf("%-9s%d\n", key, value);
                count++;
                totalCD = totalCD + value;
            }
            System.out.println("There are a total of " + count + " bands");
            System.out.println("total number of CDs " + totalCD);
        }
    
    }
    Last edited by scoobyrox; 02-19-2011 at 11:04 PM.

  2. #2
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    please put in code form! its hard to read.

    use this , but without spaces. [code] your code here [/ code]

  3. #3
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default thanks

    I just changed it to code format. Thanks. I am new to this forum so I didn't know.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Do you understand why you get that runtime error? It's because nextLine() eats the entire line (including the number which for all it knows is part of the band name, like U2 or whatever).

    If you ask me (or even if not) your teacher is being a bit of a b@stard here.

    next() won't work - it will fail for band names with a space in them. (It is too timid)
    nextLine() won't work - it will fail because it grabs the number. (It is too greedy)

    You want some sort of Goldilocks solution... Suppose you read the entire line but, instead of assigning this to the band name, you attempt to break it up into two pieces.

    Try and find a precise step by step recipe you could use to get the split "just right". It will probably involve some String methods - but try and express it in plain English first.

  5. #5
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default my changes

    So here is my new code but I am getting this new runtime error which I am not sure why. The first while loop runs fine but during the 2nd loop run, both questions are asked at the same time.

    Enter band name: Springsteen
    Enter # of CD for the band: 3
    Are you done with your entry? (Y or N) n
    Enter band name: Enter # of CD for the band:


    Java Code:
    public class Main
    
    {
        private static int totalCD;
    
    
        public static void main(String[] args)
        {
            Map<String, Integer> listBand = new TreeMap<String, Integer>();
            Scanner in = new Scanner(System.in);
            boolean done = false;
    
            while (!done)
            {
                System.out.print("Enter band name: ");
                String bandName = in.nextLine();
    
                System.out.print("Enter # of CD for the band: ");
                int numberOfCD = in.nextInt();
    
                listBand.put(bandName, numberOfCD);
    
                System.out.print("Are you done with your entry? (Y or N) ");
                String answer = in.next();
                if (answer.equalsIgnoreCase("Y"))
                {
                    done = true;
                }
            }
    
            Set<String> keySet = listBand.keySet();
            int count = 0;
            for (String key : keySet)
            {
                int value = listBand.get(key);
                System.out.printf("%-9s%d\n", key, value);
                count++;
                totalCD = totalCD + value;
            }
            System.out.println("There are a total of " + count + " bands");
            System.out.println("Total of" + totalCD + "CDs");
        }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Your new problem is beacause nextInt doesn't eat the newline. So if you type 6ENTER nextInt reads the 6 but leaves the ENTER. Then a call to nextLine returns the empty String between 6 and ENTER. Solution: simply add a call to nextLine after the call to nextInt and throw away the return value.

  7. #7
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default

    Thanks. I took your advice and it did work. It seems kind of dumb to have to write a code line to just treat the enter key entry. I guess I am a novice. There is probably a better way to solve this problem.

Similar Threads

  1. Need help with input/output program error
    By stefan2892 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 07:57 PM
  2. Input stream error
    By Johnny68 in forum New To Java
    Replies: 10
    Last Post: 08-05-2010, 06:20 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. input.nextLine();
    By Cass29 in forum New To Java
    Replies: 3
    Last Post: 12-21-2009, 07:43 PM
  5. .nextLine(); only picks up first word
    By ethanemc505 in forum New To Java
    Replies: 1
    Last Post: 10-08-2009, 07:30 PM

Posting Permissions

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