3 5
2 3 4 5 10
4 5 2 3 7
-3 -1 0 1 5
and store it into a 2d array with given dimensions in the first line. I keep returning this error:
java.lang.NumberFormatException: For input string: "i"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at Driver0.readFile(Driver0.java:39)
at Driver0.main(Driver0.java:18)
import java.io.*;
import java.util.*;
public class Driver0 {
//public static String fileName; // declare so it may be used outside of main
public static int[][] array; // declare as empty until given dimensions
public static int dimensionA, dimensionB; // dimensions used in methods
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is the name of the data file?");
System.out.print("> ");
String fileName = input.nextLine();
readFile(fileName);
String nextCommand = "";
while (!(nextCommand.equals("quit"))) {
System.out.println("\nNext command");
System.out.print("> ");
nextCommand = input.nextLine();
choice (nextCommand);
}
}
public static void choice(String command) {
switch (command) {
case "help": System.out.println("show array\nrows sorted\ncols sorted"+
"increase row i by n\nincrease col j by n\nquit\n");
break;
default: showArray();
}
}
public static void readFile(String fileName) {
try {
Scanner foo = new Scanner(fileName);
String tempD = foo.nextLine();
dimensionA = Integer.parseInt("" + tempD.charAt(0));
dimensionB = Integer.parseInt("" + tempD.charAt(2));
array = new int[dimensionA][dimensionB];
while (foo.hasNextInt()) {
for (int row = 0; row < dimensionA; row++) {
String temp = foo.nextLine();
//for (int column = 0; column < dimensionB; column++) {
//array[row][column] = foo.nextInt();
//}
}
}
foo.close();
}
catch(Exception e) {
e.printStackTrace(); // displays type of error
}
}
public static void showArray() {
for (int ro = 0; ro < dimensionA; ro++) {
for (int col = 0; col < dimensionB; col++) {
System.out.print(array[ro][col]);
}
System.out.println();
}
}
}
My first attempt at this problem was using:
public static void readFile(String fileName) {
try {
Scanner foo = new Scanner(fileName);
dimensionA = foo.nextInt();
dimensionB = foo.nextInt();
array = new int[dimensionA][dimensionB];
while (foo.hasNext()) {
for (int row = 0; row < dimensionA; row++) {
for (int column = 0; column < dimensionB; column++) {
array[row][column] = foo.nextInt();
}
}
}
foo.close();
}
catch(Exception e) {
System.out.println("Error: file not found or insufficient requirements.");
}
}
which quickly turned this error:
Java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Driver0.readFile(Driver0.java:38)
at Driver0.main(Driver0.java:18)
Am I better off with the original method I was attempting or am I on the right track with the second one, or neither?

New Topic/Question
Reply



MultiQuote






|