-1

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();
      }
            
   }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • Also mark the line of your code that the exception was thrown (line 28 in `MilageTrackerLinkedList`) with a comment. – hc_dev May 23 '22 at 18:25
  • `nextLine` reads an entire line of input. Your input has no newlines, so I presume you want to read something shorter than that – Silvio Mayolo May 23 '22 at 18:29
  • Yes, please provide the input as plain-text (each entered line separately). Thus I have edited the text as code-block before you reverted. – hc_dev May 23 '22 at 18:32
  • 1
    A fellow of you raised same question and got an [answer already](https://stackoverflow.com/questions/66827877/why-am-i-getting-java-util-inputmismatchexception-when-using-my-scanner). – hc_dev May 23 '22 at 18:34
  • I doubt, that there is a newline between each input record. Wouldn't it be clearer to use [code-formatting](https://stackoverflow.com/help/formatting), see also [When to use code-formatting for non-code](https://meta.stackoverflow.com/a/254995/5730279) – hc_dev May 23 '22 at 19:13

2 Answers2

1

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();
  }
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 13 '23 at 13:04
0

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;
      }
hc_dev
  • 8,389
  • 1
  • 26
  • 38