I am trying to figure out why my file will not output. It is a data file including monitors and their refresh rates, response times, model of the monitor and brand. Whenever I run it I get at.util.Scanner errors and at com.company.MonitorsTest.loadMonitors error. Is the way the data file set up okay?
class Monitors{
public String brandName;
public int modelNumber;
public int refreshRate;
public int responseTime;
}
public class MonitorsTest {
public static void main(String[] args) {
Monitors[] gaming = new Monitors[100];
int dataFile;
int choice;
System.out.println("\nDisplay Monitors\n");
System.out.println(" Enter one of the following commands:");
System.out.println("(1)- Display the entire data file ");
System.out.println("(2) - Display certain information on a monitor");
System.out.println("(3) - Display a histogram");
Scanner keyboard = new Scanner(System.in);
System.out.println();
System.out.println("Enter 1, 2, or 3 ");
choice = keyboard.nextInt();
dataFile = (loadMonitors(gaming));
while ( choice != 3){
if (choice < 1 || choice > 3) {
System.out.println("Enter 1, 2, 3 ");
choice = keyboard.nextInt();
}
else if (choice == 1){
System.out.println(dataFile);
}
}
}
private static int loadMonitors(Monitors[] gaming) {
int nMonitors = 0;
try {
File file = new File("C:\\Users\\kento\\IdeaProjects\\Program3\\src\\monitors.txt");
Scanner scan = new Scanner(file);
do {
gaming[nMonitors] = new Monitors();
gaming[nMonitors].brandName = scan.next();// Error here
gaming[nMonitors].modelNumber = scan.next();// Error here
gaming[nMonitors].responseTime = scan.nextDouble();
gaming[nMonitors].refreshRate = scan.nextInt();// Error here
nMonitors;
}
while (gaming[nMonitors - 1].refreshRate != 0);
--nMonitors;
}
catch (IOException ioe){
System.out.println(" File access error" ioe);
nMonitors = 0;
}
return nMonitors;
}
List of Errors:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at com.company.MonitorsTest.loadMonitors(MonitorsTest.java:62)
at com.company.MonitorsTest.main(MonitorsTest.java:39)
Text file
Sceptre DCIP3 1 165
AOC C24G1A 1 165
ASUS VG278QR 05 144
Sceptre E22 5 75
Alienware AW2521HF 1 240
CodePudding user response:
The question code keeps changing, so it's not 100% possible to point a finger on the exact issue, but, I suspect that the data doesn't match the parsing workflow.
For me, I'd read the next line of data and then parse it separately, for example:
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
System.out.println(loadMonitors(new Monitors[100]));
}
class Monitors {
public String brandName;
public String modelNumber;
public int refreshRate;
public double responseTime;
}
private int loadMonitors(Monitors[] gaming) {
int nMonitors = 0;
try (InputStream is = getClass().getResourceAsStream("/stackoverflow/monitors.txt"); Scanner scan = new Scanner(is)) {
while (scan.hasNextLine()) {
String line = scan.nextLine();
Scanner parser = new Scanner(line);
System.out.println(line);
Monitors monitor = new Monitors();
monitor.brandName = parser.next();
monitor.modelNumber = parser.next();
monitor.responseTime = parser.nextDouble();
monitor.refreshRate = parser.nextInt();
gaming[nMonitors] = monitor;
nMonitors;
}
} catch (IOException ioe) {
System.out.println(" File access error" ioe);
nMonitors = 0;
}
return nMonitors;
}
}
This will then output
Sceptre DCIP3 1 165
AOC C24G1A 1 165
ASUS VG278QR 0.5 144
Sceptre E22 5 75
Alienware AW2521HF 1 240
5
*nb: Please note, I'm treating the monitors.txt
file as an embedded resources, since your original code has it stored in the src
directory, the above example is a more correct way for reading it.