Java Scanner won't read?

You're not reading from a file. You're reading the String "text. Txt".

You need a file first.

Up vote 1 down vote favorite share g+ share fb share tw.

I'm trying to read from a file that has information written in the format: someName:aDoubleNumber and returns the name and the double. I've written the following scanner, but it doesn't work, any ideas why? Scanner readFile = new Scanner("text.

Txt"); readFile. UseDelimiter(":"); while (fileScanner.hasNext()) { String name = readFile.next(); } java java-util-scanner link|improve this question edited Apr 30 '11 at 11:18skaffman114k8135227 asked Feb 18 '11 at 20:48James112.

(From the looks of it you are not trying to read in any doubles. ) – Jeremy Heiler Feb 18 '11 at 20:51 It says it can't find the readFile package. And I don't know how to get it to read the doubles.

– James Feb 18 '11 at 20:55 Have you seen this? Download.oracle.com/javase/6/docs/api/java/util/Scanner.html – Jeremy Heiler Feb 18 '11 at 20:56.

You're not reading from a file. You're reading the String "text. Txt".

You need a file first. Scanner readFile = new Scanner(new File("text. Txt")); // don't forget to catch FileNotFoundException!

ReadFile. UseDelimiter(":|\\n"); while (fileScanner.hasNext()) { String name = readFile.next(); double value = readFile.nextDouble(); System.out. Println(name + " " + value); } I took the code from your comment and formatted, and I get this public class MyProject { class FileInput { Scanner readFile = new Scanner(new File("text.

Txt")); // don't forget to catch FileNotFoundException! ReadFile. UseDelimiter(":|\\n"); while (fileScanner.hasNext()) { String name = readFile.next(); double value = readFile.nextDouble(); System.out.

Println(name + " " + value); } } } Now you seem to have a problem - perhaps you meant class FileInput to be public void fileInput() throws Exception? When I do this, it compiles. Now we need a main() method to run it!

So I add this: public static void main(String args) throws Exception { MyProject proj = new MyProject(); proj.fileInput(); } Now when I ran it I actually got an error. This means there was actually a problem in the code I gave you to begin with. Of course, that code was never intended to be copy/pasted, but was more to give an idea of capability.

Anyway, the error is: Exception in thread "main" java.util. InputMismatchException at java.util.Scanner. ThrowFor(Scanner.

Java:840) at java.util.Scanner. Next(Scanner. Java:1461) at java.util.Scanner.

NextDouble(Scanner. Java:2387) at MyProject. FileInput(MyProject.

Java:9) java MyProject Name: this Value: 1234.5 Name: that Value: 321.0 Name: the other Value: 0.122 C:\Documents and Settings\glowcoder\My Documents>java MyProject }.

Right now, my code looks like: public class MyProject { class FileInput { Scanner readFile = new Scanner(new File("text. Txt")); // don't forget to catch FileNotFoundException! ReadFile.

UseDelimiter(":|\\n"); while (fileScanner.hasNext()) { String name = readFile.next(); double value = readFile.nextDouble(); System.out. Println(name + " " + value); } }} but I get an exception. Sorry about this (I'm learning from a book) – James Feb 18 '11 at 21:14 1 Update your question with 1) Your new code and 2) the exact copy/paste exception notice you get.

– corsiKa Feb 18 '11 at 21:20 @James, I updated my answer with respect to your most recent comment here. Let me know if you still have any questions about this. – corsiKa Feb 18 '11 at 22:11.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions