954,224 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading numbers from a text file

I need to read numbers from a text file and then store them in variables to use as input for the Java code I am writing.
The text file will be in this format:
--------------------------------------------------------
5------------- this is the number of nodes
0------------- starting node
0,2,1---------node a,node b, distance from a-b
3,4,5--------- " , " , "
2,6,5
1,4,2
1,5,3
----------------------------------------------------------

Any ideas?

baltazar
Light Poster
46 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

What do you have so far, and what problem is it giving you?

FileReader with BufferedReader and String.split,
or Scanner are good starting points, though.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I recommend Scanner highly. This will work nicely if:
a. you replace the commas w/spaces and use nextInt()
b. you keep the commas, use next() , and set up an elaborate system to trim the commas out of the String s made and convert said String s to ints
I'd go with "a".

venomlash
Junior Poster
143 posts since Oct 2006
Reputation Points: 86
Solved Threads: 2
 
I recommend Scanner highly. This will work nicely if: a. you replace the commas w/spaces and use nextInt() b. you keep the commas, use next() , and set up an elaborate system to trim the commas out of the String s made and convert said String s to ints I'd go with "a".


You can set the delimeter to any pattern you wish with the useDelimeter() method. You don't need to convert anything prior to scanning.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Ok, so I used Scanner to read input from a text file. I'm trying to test this by just reading in input from a text file and printing it out. But I'm getting some weird errors.

import java.lang.*;
import java.util.*;

public class Assignment5
{
	int i = 0;

	Scanner st = new Scanner(new File("testCase")).useDelimiter();
	int numNodes = st.nextInt();
	System.out.println(numNodes);


	while(i <= numNodes)
	{
		Scanner st = new Scanner(new File("testCase")).useDelimiter();
		int k = st.nextInt();
		System.out.println(k);
	}


	public static void main(String[] args)
	{
		Assignment5 A5 = new Assignment5();
	}//end of main


}//end of Assignment5 class


My testCase file is:
5
55
555
5555
55555

The errors I can't seem to figure out are:

Assignment5.java:18: expected
System.out.println(numNodes);
^
Assignment5.java:18: expected
System.out.println(numNodes);
^
Assignment5.java:21: illegal start of type
while(i <= numNodes);
^
Assignment5.java:21: expected
while(i <= numNodes);
^
Assignment5.java:21: expected
while(i <= numNodes);
^
5 errors

baltazar
Light Poster
46 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

You do not have much of your code inside a method - it's in the class file. Place that code inside a method, either main or another method that you call from main to execute like A5.run() or something.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Ah yes, I saw my error. It was a noob mistake.

baltazar
Light Poster
46 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

I am still confused about how to write the usDelimiter in the scanner.
I need it to read number seperated by commas.
What would the useDelimiter parameter be?
I have tried a couple of things (.useDelimiter(",") and .useDelimiter(,)) but they didn't work
Here is my code:

import java.lang.*;
import java.util.*;
import java.io.File;

public class Assignment5
{
	public Assignment5()
	{
		int numNodes = 0;
		Scanner st = null;
		try
		{
			st = new Scanner(new File("testCase.txt")).useDelimiter(",");
			numNodes = st.nextInt();
			System.out.println(numNodes);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}



		while(st.hasNextInt())
		{
			int k = st.nextInt();
			System.out.println(k+",");
		}
	}


	public static void main(String[] args)
	{
		Assignment5 A5 = new Assignment5();
	}//end of main


}//end
baltazar
Light Poster
46 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

You need to specify the delimiter keeping in mind that its a regular expression pattern. If your input from the file is nothing but numbers separated by ',' without any space in between them, the code posted by you must work.

I am assuming that your code won't work since the delimiter you gave was only a single ',' while your input must have had spaces between the number and comma. (eg. 1, 3, 3). You should have input in this format (1,2,3,4).

If you want your code to work for something like(1, 2, 3,4, 5) :

String sample = "1 ,  2 , 3 ,4,    5";
st = new Scanner(sample).useDelimiter("\\s*,\\s*");
int k;
while(st.hasNextInt())
{
    k = st.nextInt();
    System.out.println(k);
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

You should use a BufferedReader to read the lines and then use either String.split() or the Scanner to separate the items for your use. This is exactly what Masijade told you in the first response to your post by the way.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Hey Baltazar

Another solution to your problem would be to talk with your 225 TA and attending the labs. :) I went over this exact piece of code two weeks in a row now (using Scanners as other posters have nicely pointed out).

Just a thought. Sorry to bother the rest of you.

Cheers,
David (225 TA)

dwsprague
Newbie Poster
1 post since Jul 2007
Reputation Points: 14
Solved Threads: 0
 

Hey Baltazar

Another solution to your problem would be to talk with your 225 TA and attending the labs. :) I went over this exact piece of code two weeks in a row now (using Scanners as other posters have nicely pointed out).

Just a thought. Sorry to bother the rest of you.

Cheers, David (225 TA)


Hehe!
Busted!

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Bleh, good luck OP. :)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You