Java, help please.

Soldato
Joined
27 Aug 2004
Posts
17,111
Location
Geordieland
Hi guys,

Im having a bit bother. Ive got 2 lines of code that are refusing to compile, both are scanners giving a 'cannot find symbol' error. The 2 lines are (highlited in code also):

Scanner writeGraduate = new Scanner(gradAccounts);
and
Scanner writeNe = new Scanner(newAccounts);

Code:
import java.io.*;
import java.util.*;

class StudentSet
{
	public static void main (String args[]) throws IOException
	{
		TreeSet oldAccounts = new TreeSet();
		TreeSet currAccounts = new TreeSet();
		
		//Define updated information
		TreeSet gradAccounts = new TreeSet();
		TreeSet newAccounts = new TreeSet();
		
		
		Scanner scOld = new Scanner(new FileReader("C:\\Documents and Settings\\Paul Steele\\My Documents\\Junk\\Java\\Hmk2\\oldAccounts.txt"));
		String oldAcc;	
		
		while (scOld.hasNext())
		{
			oldAcc = scOld.next();
			oldAccounts.add(oldAcc);
			System.out.println(oldAcc);
		}
		
		Scanner scCurr = new Scanner(new FileReader("C:\\Documents and Settings\\Paul Steele\\My Documents\\Junk\\Java\\Hmk2\\currAccounts.txt"));
		String currAcc;	
		while (scCurr.hasNext())
		{
			currAcc = scCurr.next();
			currAccounts.add(currAcc);
			System.out.println(currAcc);
		}


		//COMPARE
		
		File writeGrad = new File("C:\\Documents and Settings\\Paul Steele\\My Documents\\Junk\\Java\\Hmk2\\gradAccounts.txt");
		
		PrintWriter writeGr = new PrintWriter(writeGrad);
		
		[B][I]Scanner writeGraduate = new Scanner(gradAccounts);[/I][/B]
		
		while(writeGraduate.hasNext())
		{
			writeGr.println(writeGraduate.next());
		}
		writeGr.close();
		
		
		File writeNew = new File("C:\\Documents and Settings\\Paul Steele\\My Documents\\Junk\\Java\\Hmk2\\newAccounts.txt");
		PrintWriter writeN = new PrintWriter(writeNew);

		[B][I]Scanner writeNe = new Scanner(newAccounts);[/I][/B]
		
		while(writeNe.hasNext())
		{
			writeN.println(writeNe.next());
		}
		writeN.close();
	}
}

Cheers for any help, its got me pulling my hair out.
 
Scanner needs some sort of a stream, your passing a treeset so it won't work.
 
Hi,

If you want to loop through the items in a TreeSet you can either use an iterator like:

Code:
        TreeSet myset = new TreeSet();

        Iterator i = myset.iterator();
        
        while (i.hasNext())
            System.out.println(i.next());

Or if you're using generics and Java 5 use a for loop like:

Code:
    TreeSet<String> myset = new TreeSet<String>();
    for (String s: myset)
            System.out.println(s);

AFAIK scanners are usually used for tokenizing, using a default delimiter of newline although this can be specified in your code so you could delimit on a regular expression etc. Using a scanner you can also take appropriate action depending on the type of the next element to read e.g hasNextInt() .

Hope that helps (and that I've got the above right :) )

Jim
 
As everyone else said, a java.util.Scanner can't take a java.util.TreeSet as a parameter. What your trying to do is use a Scanner to feed data from a TreeSet into a PrintWriter. As JIMA said, you need to use some form of iterator, I have modified your original code to use an enhanced for-loop (foreach loop) so that it iterates over the TreeSet and feeds each element to the PrintWriter which is what you were trying to do with the Scanner.

This needs Java 5 or above to compile because the use of the enhanced for-loop, a standard for-loop could be used. Also if your using Java 5 or above, it is good practice to specify types on collections, for example:

TreeSet<String> myset = new TreeSet<String>();

This allows the compiler to detect any attempts to use the collection to store an invalid type.

What are you trying to do with this code? I think you may be making the code a lot more complicated than it needs to be.

Code:
import java.io.*;
import java.util.*;

class StudentSet
{
	public static void main (String args[]) throws IOException
	{
		TreeSet oldAccounts = new TreeSet();
		TreeSet currAccounts = new TreeSet();
		
		//Define updated information
		TreeSet gradAccounts = new TreeSet();
		TreeSet newAccounts = new TreeSet();
		
		
		Scanner scOld = new Scanner(new FileReader("C:\\Documents and Settings\\Paul Steele\\My Documents\\Junk\\Java\\Hmk2\\oldAccounts.txt"));
		String oldAcc;	
		
		while (scOld.hasNext())
		{
			oldAcc = scOld.next();
			oldAccounts.add(oldAcc);
			System.out.println(oldAcc);
		}
		
		Scanner scCurr = new Scanner(new FileReader("C:\\Documents and Settings\\Paul Steele\\My Documents\\Junk\\Java\\Hmk2\\currAccounts.txt"));
		String currAcc;	
		while (scCurr.hasNext())
		{
			currAcc = scCurr.next();
			currAccounts.add(currAcc);
			System.out.println(currAcc);
		}


		//COMPARE
		
		File writeGrad = new File("C:\\Documents and Settings\\Paul Steele\\My Documents\\Junk\\Java\\Hmk2\\gradAccounts.txt");
		
		PrintWriter writeGr = new PrintWriter(writeGrad);
		
		for (String s : gradAccounts)
		{
			writeGr.println(s);
		}

		writeGr.close();
		
		
		File writeNew = new File("C:\\Documents and Settings\\Paul Steele\\My Documents\\Junk\\Java\\Hmk2\\newAccounts.txt");
		PrintWriter writeN = new PrintWriter(writeNew);

		for (String s : newAccounts)
		{
			writeN.println(s);
		}
		
		writeN.close();
	}
}
 
Yes, thanks a lot guys, much appreciated. Got it working.

Never knew you had to use the iterator to do this, so I would never have figured it out had it not been for you guys.

Many thanks.
 
Back
Top Bottom