3 Replies - 309 Views - Last Post: 09 October 2012 - 06:49 PM Rate Topic: -----

#1 dryEraseMarker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-October 12

Parsing a text file of ints line by line

Posted 09 October 2012 - 11:57 AM

Hi I need to be able to parse a text file containing ints
ie.
5 // 5 tests cases within file

9 // first test case has 9 locations
100 500 //lab1 coordinates
0 0 // lab2 coordinates
300 200
400 500
500 100
0 400
500 200
100 200
500 300
0 6 6 6 6 6 6 6 6 6 // amount of files at each lab

I need to be able to use each line separate
I'm a little rusty at java but i have so far
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class A1 {
	public static void main(String[] args) throws IOException{
		file_parser();
	}
	public static void file_parser() throws IOException{
		String line,file_name;
		Scanner filescan;
		int[] intArray = new int[1024];
		Scanner user_input = new Scanner(System.in);
		System.out.println("enter file name");
		file_name = user_input.next();
		filescan = new Scanner(new File(file_name));
		while(filescan.hasNext()){
			int i = 0;
			line = filescan.nextLine();
			intArray=int_parser(line);
			System.out.println(intArray[i]);
		}
		
	}
	public static int[] int_parser(String parse){
		int[] intArray = new int[parse.length()];
		for(int i = 0; i < parse.length(); i++){
			intArray[i] = Character.digit(parse.charAt(i), 10);
		}
		return intArray;
	}

}


any help would be awsome

Is This A Good Question/Topic? 0
  • +

Replies To: Parsing a text file of ints line by line

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Parsing a text file of ints line by line

Posted 09 October 2012 - 12:48 PM

Be a little more specific when posting questions so we know what we are looking at. Does it not do what you want it to do, or are you getting errors? If your getting errors, can you post them please.
Was This Post Helpful? 0
  • +
  • -

#3 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Parsing a text file of ints line by line

Posted 09 October 2012 - 01:18 PM

Line 19, you are re-assigning the whole array in every iteration, in essence, at the end of the program, the array will hold the values from the last line in the file.

Your intParser is returning the digits of the line in an array????? Not sure what you are trying to do there

Line 20, i will always be 0, and you are printing intArray[i], which is the first digit on the line.


Not sure if this helps.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Parsing a text file of ints line by line

Posted 09 October 2012 - 06:49 PM

this is a non sense
       while(filescan.hasNext()){  
             int i = 0;  
             line = filescan.nextLine();  


match the Scanner hasXXX() methof with the method that effectively do the read
so hasNextLine() with nextLine() and hasNext() with next()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1