7 Replies - 1803 Views - Last Post: 30 August 2012 - 07:05 PM Rate Topic: -----

#1 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

Read txt file, store 2d array with given dimensions using Scanner

Posted 29 August 2012 - 09:28 PM

I'm trying to read a file which contains:

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Read txt file, store 2d array with given dimensions using Scanner

#2 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Read txt file, store 2d array with given dimensions using Scanner

Posted 29 August 2012 - 11:26 PM

As far as I know, it is not possible to use strings in switches. What you can do, is using enums instead as an input argument for your method choice(). Though this needs some rewriting, this is usually the way to go when you want a String (or some other default commands) as an userinput.
More information about enums can be found here:
http://docs.oracle.c...avaOO/enum.html
http://docs.oracle.c...uage/enums.html (Also includes why you shouldn't use integers as a choice command)

It basically works like this:
public class myClass{

	enum MyEnum{
		Startup, Shutdown, Restart
	}
	
	public static void main(string[] args){
		//get userinput
	
		// Parse the enum
		//Note that the user has to give a string which matches one of the options in MyEnum
		// Else, this will result in  an IllegalArgumentException
		MyEnum userChoice = MyEnum.valueOf(userInput);
		
		//Switch
		switch (userChoice){
			case Shutdown:
				//DO
				break;
			//etc
			default: 
				break;
		}
	}
}



Might have mixed up some some C# syntax with it... :sweatdrop:

This post has been edited by karabasf: 29 August 2012 - 11:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Read txt file, store 2d array with given dimensions using Scanner

Posted 29 August 2012 - 11:37 PM

Switches using Strings is now implemented in Java 7.

Using Strings in switch Statements
Was This Post Helpful? 0
  • +
  • -

#4 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Read txt file, store 2d array with given dimensions using Scanner

Posted 29 August 2012 - 11:41 PM

View PostfromTheSprawl, on 30 August 2012 - 08:37 AM, said:

Switches using Strings is now implemented in Java 7.

Using Strings in switch Statements


Interesting, guess I am still a little bit outdate with Java 1.6. Thanks for the information. (Though I already knew it was implemented in C# though)
Was This Post Helpful? 0
  • +
  • -

#5 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

Re: Read txt file, store 2d array with given dimensions using Scanner

Posted 30 August 2012 - 07:12 AM

Anything on the readFile() method?
Was This Post Helpful? 0
  • +
  • -

#6 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Read txt file, store 2d array with given dimensions using Scanner

Posted 30 August 2012 - 09:01 AM

Do you still get the same errors after you implemented the enums or after installing Java 1.7?

This post has been edited by karabasf: 30 August 2012 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#7 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

Re: Read txt file, store 2d array with given dimensions using Scanner

Posted 30 August 2012 - 09:18 AM

Ahh nevermind, fixed it by using
Scanner foo = new Scanner(new File(fileName));
instead of
Scanner foo = new Scanner(fileName);

It was using the *name* instead of the actual file.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Read txt file, store 2d array with given dimensions using Scanner

Posted 30 August 2012 - 07:05 PM

ReadFile is a lot complicated for nothing... assuming your file is correctly formatted

The big error is

Scanner foo = new Scanner(fileName);

you actually try to scan the String "myFile.txt" not what is in the file

void readFile(String fileName) {
   try {
      Scanner scan = new Scanner(new File(fileName));   // remarch the new File() here
      int width = scan.nextInt();
      int height = scan.nextInt();
      array = new int[width][height];
      for(int i = 0; i < width; ++i) {
          for(int j = 0; j < height; ++j) {
             array[i][j] = scan.nextInt();
          }
      }


should do it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1