0

I am supposed to receive an integer input first, followed by a double, followed by a string. I am then supposed to insert it into a linked list. The //TODO sections are where I implemented the code. I really don't know why I am getting these errors. I saw suggestions that said to use the useLocal() method, but it didn't help with the errors.

Here is the code:

import java.util.*;

public class MileageTrackerLinkedList {
   public static void main (String[] args) {
      Scanner scnr = new Scanner(System.in);
      scnr.useLocale(Locale.US);

      // References for MileageTrackerNode objects
      MileageTrackerNode headNode;                                           
      MileageTrackerNode currNode;
      MileageTrackerNode lastNode;

      double miles;
      String date;
      int i;
      int count;
      
      // Front of nodes list                                                                         
      headNode = new MileageTrackerNode();
      lastNode = headNode;

      // TODO: Scan the number of nodes
      count = scnr.nextInt();
      
      // TODO: For the scanned number of nodes, scan
      //       in data and insert into the linked list
      for (i = 0; i < count; ++i)
      {
         miles = scnr.nextDouble();
         date = scnr.nextLine();
         currNode = new MileageTrackerNode(miles, date);
         lastNode.insertAfter(currNode);
         lastNode = currNode;
      }

      // TODO: Call the printNodeData() method 
      //       to print the entire linked list
      for (i = 0; i < count; ++i)
      {
         headNode.printNodeData();
         headNode.getNext();
      }
            
   }
}

This is the input:

3
2.2
7/2/18
3.2
7/7/18
4.5
7/16/18

And here are the error messages:

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
    at MileageTrackerLinkedList.main(MileageTrackerLinkedList.java:29)
Allen B
  • 5
  • 3
  • When you have problems with input, it helps to see the input you're actually using. You should edit your question now, by pasting the output and input of your console so that people don't have to waste time guessing what you did. – MarsAtomic Mar 27 '21 at 04:02
  • @MarsAtomic you're right. I updated it. Thanks for the advice, I'm new to this. – Allen B Mar 27 '21 at 04:16

1 Answers1

0

You can't mix nextLine and nextAnythingElse.

The easy fix is to use next() in order to read a line's worth of text. In order to do so, first tell scanner that it should feed you answers a line at a time. This is usually what you want (you ask user for some data, and then they hit enter. voila: A line).

After new Scanner, always run: scanner.useDelimiter("\r?\n");. Then use .next() for text, .nextInt() for ints, etc.

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37
  • You should delete this answer, but add it to the [duplicate](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) instead. – Andreas Mar 27 '21 at 04:09
  • Instead of `useDelimiter("\r?\n")`, perhaps `useDelimiter("\\R")` would be better? See [Linebreak matcher](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#lineending). – Andreas Mar 27 '21 at 04:11
  • I'm still getting the same errors. I updated my post to show the input. – Allen B Mar 27 '21 at 04:32
  • 1
    @Andreas that's perfect, thanks! One of the reasons I'm a bit hesitant to start a crusade against the commonly given dumb advice of adding additional `.nextLine()` calls to 'eat up the newline character' is that `.useDelimiter("\r\n?")` seems like it needs explanation, but `.useDelimiter("\\R")` is a clear winner! – rzwitserloot Mar 27 '21 at 16:32
  • @AllenB most likely your system default locale expects `2,2` and not `2.2` (so, commas, not dots). You can either change your input, or, you can initialize the scanner: Just like you update the delimiter, also update the locale, using `scanner.setLocale(Locale.ENGLISH);` right after making it. – rzwitserloot Mar 27 '21 at 16:34