Dawgbizkit83
Dawgbizkit83

Reputation: 1

Why Mileage tracker gives InputMismatchException?

Given the MileageTrackerNode class, complete main() in the MileageTrackerLinkedList class to insert nodes into a linked list (using the insertAfter() method). The first user-input value is the number of nodes in the linked list. Use the printNodeData() method to print the entire linked list. DO NOT print the dummy head node.

Ex. If the input is:

3

2.2

7/2/18

3.2

7/7/18

4.5

7/16/18

the output is:

2.2, 7/2/18

3.2, 7/7/18

4.5, 7/16/18

I just need to edit the //TO DO sections, but have also added int count Error messages:

Exception in thread "main" java.util.InputMismatchException
java.base/java.util.Scanner.throwFor(Scanner.java:939)
java.base/java.util.Scanner.next(Scanner.java:1594)
java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
MileageTrackerLinkedList.main(MileageTrackerLinkedList.java:28)

Here is my code so far:

import java.util.Scanner;

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

      // 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();
      }
            
   }
}

Upvotes: -1

Views: 3611

Answers (2)

user20987503
user20987503

Reputation: 11

Thank you for the template for this, I used that to help me get this code, which ended up working. I am sure it is too late for your class, but maybe someone else can benefit from this if they are stuck.

int count = scnr.nextInt();

for (i = 0; i < count; i++) {
     miles = scnr.nextDouble();
     date = scnr.next();
     currNode = new MileageTrackerNode(miles, date);
     lastNode.insertAfter(currNode);
     lastNode = currNode;
  }

  currNode = headNode.getNext();
  for (i =0; i< count; i++) {
     currNode.printNodeData();
     currNode = currNode.getNext();
  }

Upvotes: 1

hc_dev
hc_dev

Reputation: 9418

I would add a few printlns to monitor and debug the runtime behavior.

Crucial is the parsing of numbers and dates (format, locale)

Would be safer to read the numbers a String first, then you can parseDouble to see if the format is as expected (comma or dot). If the format can not be read by your Java environment/setup (Locale), then it will raise a NumberFormatException.

Code to debug

      // TODO: Scan the number of nodes
      count = scnr.nextInt();
      // FIXME: remove debug print after development
      System.out.println("Scanned count: " + count);      
   
      // TODO: For the scanned number of nodes, scan
      //       in data and insert into the linked list
      for (i = 0; i < count; ++i) {
         // FIXME: test first, remove debug-prints later
         System.out.println("Scanning node # " + i);
         // nextDouble relies on format of input and current Scanner locale used
         milesText = scnr.nextLine();    // more forgiving to read as string
         // then try parsing as double (same as nextDouble, but with error-handling)
         try {
             miles = Double.parseDouble(milesText);
             System.out.println("Scanned miles: " + miles);
         } catch (NumberFormatException e) {
             System.out.println("Failed to parse double from: " + milesText);
             e.printStackTrace();
         }
         dateText = scnr.nextLine();
         // FIXME: remove debug-prints after development
         System.out.println("Scanned date: " + dateText);
         currNode = new MileageTrackerNode(miles, dateText);  // is the constructor accepting date as string, or as Date ?
         lastNode.insertAfter(currNode);
         lastNode = currNode;
      }

Upvotes: 0

Related Questions