so i have a class that creates a file:
java.io.*; import java.lang.*; import java.util.*; public class CreateFile { private Formatter x; public void openFile(){ try{ x=new Formatter("NikolaiLavroff.txt"); } catch(Exception e){ System.out.println("You have an error"); } } public void addRecords(){ x.format("%s%s%s", "15\t", "N\t", "L\n"); x.format("%s%s%s", "18\t", "F\t", "L\n"); x.format("%s%s%s", "53\t", "K\t", "T\n"); x.format("%s%s%s", "53\t", "J\t", "L\n"); //there were names in place of letters but ive changed them. } public void closeFile(){ x.close(); } }
What i wanted to see in the file was:
15 N L
18 F L
53 K T
53 J L
and i that is what i saw.
I have another class to read the file:
import java.io.*; import java.lang.*; import java.util.*; public class ReadFile { private Scanner x; public void openFile(){ try{ x=new Scanner(new File("NikolaiLavroff.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(); } }
and then my main:
import java.io.File; import java.util.*; import javax.swing.JFrame; class Apples{ public static void main (String args[]){ ReadFile r=new ReadFile(); r.openFile(); r.readFile(); r.closeFile(); } }
now what i was expecting to get in the program was
15 N L
18 F L
53 K T
53 J L
but instead i got all of the above (^^^) in one line and no spaces between them. I think the problem lies in the way i added the data (added records) in my createfile class because i didnt know how to make the data start on a new lign without using '\n'. I think it is simple problem as i am a java rookie