Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I can't figure out how to fix this error in the code below: java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1656) at Main.main(Main.java:31) public class TravelTimeData {

I can't figure out how to fix this error in the code below:

java.util.NoSuchElementException: No line found

at java.base/java.util.Scanner.nextLine(Scanner.java:1656)

at Main.main(Main.java:31)

public class TravelTimeData { private int COGID; private String firstDirection; private String secondDirection; private int firstTime; private int secondTime; private int timeDifference; public TravelTimeData(int COGID, String firstDirection, String secondDirection, int firstTime, int secondTime) { this.COGID = COGID; this.firstDirection = firstDirection; this.secondDirection = secondDirection; this.firstTime = firstTime; this.secondTime = secondTime; this.timeDifference = Math.abs(firstTime - secondTime); } public int getTimeDifference() { return timeDifference; } public String toString() { return COGID + " " + firstDirection + " " + firstTime + " " + secondDirection + " " + secondTime; } }

import java.util.Scanner; import java.io.File; import java.io.IOException; import java.util.ArrayList; //https://www.w3schools.com/java/java_arraylist.asp

public class Main {

public static void main(String[] args) { ArrayList travelData = new ArrayList<>(); try { File f = new File("TravelDataPartial.csv"); Scanner s = new Scanner(f); //Strip first line s.nextLine(); while(s.hasNext()) { //TODO: Lots of your code goes here //for file reading. String line = s.nextLine(); String[] values = line.split(","); int COGID = Integer.parseInt(values[0].trim()); String firstDirection = values[1].trim(); String secondDirection = values[1].trim(); int firstTime = Integer.parseInt(values[2].trim()); int secondTime = Integer.parseInt(values[3].trim()); travelData.add(new TravelTimeData(COGID, firstDirection, secondDirection, firstTime, secondTime)); } s.close(); } catch(Exception e) { System.out.println("ERROR!"); e.printStackTrace(); } TravelTimeData largestDiff = getLargestDifferential(travelData); System.out.println("The route with the biggest differential is "); System.out.println(largestDiff); System.out.println(" with a time difference of " + largestDiff.getTimeDifference() + " minutes."); }

/* Take the data and return the route with the largest travel time differential. */ public static TravelTimeData getLargestDifferential(ArrayList data) { TravelTimeData largestDiff = data.get(0); for (int i = 1; i < data.size(); i++) { TravelTimeData curr = data.get(i); if (curr.getTimeDifference() > largestDiff.getTimeDifference()) { largestDiff = curr; } } return largestDiff; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

3. How frequently do the assessments occur?

Answered: 1 week ago