Question: Instructor note: Use the next ( ) method when scanning the date. date = scnr . next ( ) ; Given the MileageTrackerNode class, complete

Instructor note:
Use the next() method when scanning the date.
date = scnr.next();
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
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;
// Front of nodes list
headNode = new MileageTrackerNode();
lastNode = headNode;
try {
// Scan the number of nodes
numNodes = scnr.nextInt();
// For the scanned number of nodes, scan
// in data and insert into the linked list
for (int i =0; i < numNodes; i++){
miles = scnr.nextDouble();
date = scnr.next();
currNode = new MileageTrackerNode(miles, date);
lastNode.insertAfter(currNode);
lastNode = currNode;
}
// Call the printNodeData() method
// to print the entire linked list
currNode = headNode.getNext();
while (currNode != null){
currNode.printNodeData();
currNode = currNode.getNext();
}
} catch (Exception e){
System.out.println("Input error: "+ e.getMessage());
} finally {
scnr.close();
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!