Hopefully somebody can help me out with this I did something wrong.
Code:
import java.io.*;
import java.util.Scanner;
public class DataSet
{
public static void main(String args[]) throws IOException
{
FileReader inFileReader = new FileReader("c://testdata.txt");
double NextDouble;
double x;
double y;
while(inFileReader.hasNext())
{
NextDouble = inFileReader.Next();
x = 0.0 + NextDouble;
y = 1.0 * NextDouble;
}
System.out.println("The sum of all numbers is " + x + ".");
System.out.println("The product of all numbers is " + y + ".");
}
}
//
I'm not really sure what I did wrong here but BlueJ is yelling at me.
Java error: cannot find symbol - method hasNext?
The API is your best friend. More important than the text book or your .... teacher! :P If you look at the Java API, you will see that the class FileReader does not have a method called hasNext. My Java is rusty, but I think hasNext() is in the Iterator interface. It has nothing to do with the FileReader.
Also I think you're reading the file wrong. You're supposed to pass an instance of FileReader to the BufferedReader's constructor. This is how you would read in a file:
http://www.exampledepot.com/egs/java.io/...
It seems like you have a file of numbers and you're trying to read them. You need to use the readLine() method of the BufferedReader class to read an entire line. But before you do that, do you know how you are delimiting the numbers? Spaces? Commas? Hyphens?
Then you could use the split method of the String class to split the numbers up into an array by their delimiter. Once the numbers are in an array you can work with them.
Once you do all that the method can get pretty large. As a rule of thumb, you should never have more than 20-25 of code lines per method. More than that, and it's probably bad design. If you have too many lines, decompose some things into other methods.
EDIT: Try Paul's method! I didn't think of the Scanner. Scanner has a nextInt() method. Should be much easier for your purposes :-)
Reply:OK, I see this a lot. Consider this...
1. We write source code files then compile
2. No problem if the text data file, the source file and the .class file all end up in one folder and ...
3. when we use the command-line, we "cd" to that mother-of-all-folders. Doing a "cd" makes the OS point to that folder. If your java path is set up with ./ in it, then the JRE will look first in the project folder.
In short, I use an IDE called NetBeans. The IDE creates a folder for .class files and a folder for my .java files. Any external stuff like .txt, images will require careful handling of the paths -- both to the source efforts and to the .class results.
That said:
Your FileReader as input for Scanner -- OK
Your path to C:// -- not OK
test that pathname with this:
File file = new File( "c://testdata.txt" );
System.out.println(
"I've got a file and it's so pretty," +
" I think so, and java says: "
+ file.isFile() );
Reply:I think that you want to use a Scanner to iteratate through the contents of the file rather than the FileReader directly. So, where you create inFileReader, it should be:
Scanner inFileReader = new Scanner(new FileReader("c://testdata.txt")...
(I am not sure if FileReader implements Readable, so you might need to use a different class). Also, the first line of you while loop should have a lowercase 'n' for next.
Reply:That error simply means that the FileReader class has no method called hasNext. Scanner does, however, so that's probably what you meant to write.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment