java.util.scanner - Java Scanner won't read -
i'm trying read file has information written in format: somename:adoublenumber , returns name , double. i've written following scanner, doesn't work, ideas why?
scanner readfile = new scanner("text.txt"); readfile.usedelimiter(":"); while (filescanner.hasnext()) { string name = readfile.next(); }
you're not reading file. you're reading string "text.txt". need file first.
scanner readfile = new scanner(new file("text.txt")); // don't forget catch filenotfoundexception! readfile.usedelimiter(":|\\n"); while (filescanner.hasnext()) { string name = readfile.next(); double value = readfile.nextdouble(); system.out.println(name + " " + value); }
i took code comment , formatted, , this
public class myproject { class fileinput { scanner readfile = new scanner(new file("text.txt")); // don't forget catch filenotfoundexception! readfile.usedelimiter(":|\\n"); while (filescanner.hasnext()) { string name = readfile.next(); double value = readfile.nextdouble(); system.out.println(name + " " + value); } } }
now seem have problem - perhaps meant class fileinput
public void fileinput() throws exception
? when this, compiles. need main()
method run it! add this:
public static void main(string[] args) throws exception { myproject proj = new myproject(); proj.fileinput(); }
now when ran got error. means there problem in code gave begin with. of course, code never intended copy/pasted, more give idea of capability. anyway, error is:
exception in thread "main" java.util.inputmismatchexception @ java.util.scanner.throwfor(scanner.java:840) @ java.util.scanner.next(scanner.java:1461) @ java.util.scanner.nextdouble(scanner.java:2387) @ myproject.fileinput(myproject.java:9) <--- happened in our code @ myproject.main(myproject.java:16)
the line happened on parsedouble. so, instead, let's try doing different way. can double raw piece of text, , parse actual double this:
while (readfile.hasnext()) { string name = readfile.next(); string valuestr = readfile.next(); double value = double.parsedouble(valuestr); system.out.println(name + " " + value); }
so whole completed program is:
import java.util.*; import java.io.*; public class myproject { public void fileinput() throws exception { scanner readfile = new scanner(new file("text.txt")); // don't forget catch filenotfoundexception! readfile.usedelimiter(":|\\n"); while (readfile.hasnext()) { string name = readfile.next(); string valuestr = readfile.next(); double value = double.parsedouble(valuestr); system.out.println("name: " + name); system.out.println("value: " + value); system.out.println(""); // blank line } } public static void main(string[] args) throws exception { myproject proj = new myproject(); proj.fileinput(); }
so input file text being:
this:1234.5 that:321.0 other:0.122
the output was
c:\documents , settings\glowcoder\my documents>java myproject name: value: 1234.5 name: value: 321.0 name: other value: 0.122 c:\documents , settings\glowcoder\my documents>java myproject }
Comments
Post a Comment