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?

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.

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".

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.

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: <identifier> expected
System.out.println(numNodes);
^
Assignment5.java:18: <identifier> expected
System.out.println(numNodes);
^
Assignment5.java:21: illegal start of type
while(i <= numNodes);
^
Assignment5.java:21: <identifier> expected
while(i <= numNodes);
^
Assignment5.java:21: <identifier> expected
while(i <= numNodes);
^
5 errors

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.

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

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);
}

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.

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)

Comments
Stright between the eyes. Nice one ;)

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!

This article has been dead for over six months. Start a new discussion instead.