I created a .txt file named Test.txt using the code below and it worked. Now i want java to read the information back to me and when I do i receive the error INBETWEEN THE TWO PARAGRAPHS OF CODE. I simply want to write to a file, save it, and be able to read it so I can create a small database. Thanks!
package textFile;
import java.io.*;
import java.lang.*;
import java.util.*;
public class createFile {
private Formatter x;
public void openfile(){
try{
x = new Formatter("Test.txt");
}
catch(Exception e) {
System.out.println("you have and error");
}
}
public void addRecords(){
x.format("%s%s%s", "Coty", "xcallidus", "Password");
}
public void closefile(){
x.close();
}
}
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at readToMe.read.readFile(read.java:21)
at readToMe.Main.main(Main.java:11)
package readToMe;
import java.lang.*;
import java.io.*;
import java.util.*;
public class read {
private Scanner x;
public void openFile(){
try{
x = new Scanner(new File("Test.txt"));
}
catch(Exception e){
System.out.println("Could not find file");
}
}
public void readFile(){
while(x.hasNext()){
String a = x.next();
String b = x.next();
String c = x.next();
System.out.printf("%s %s %s", a,b,c);
}
}
public void closeFile(){
x.close();
}
}
The problem is in
public void addRecords(){
x.format("%s%s%s", "Coty", "xcallidus", "Password");
//This creates CotyxcallidusPassword - Without spaces
}
You are adding three strings without spaces and are trying to read them with spaces. It should be
public void addRecords(){
x.format("%s %s %s", "Coty", "xcallidus", "Password");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.