Trying to write to a file with a scanner, lots of errors
I'm trying to put a Name, number, and any notes to a 'person' in a file.
However I always get an error, when i "fix" one thing it comes up as a different error and I'm not sure what to do. The first if statement is where it pops up, I haven't even started to mess with the output.
Any help would be greatly appreciated.
(and I have a file made for it)
Code:
import java.io.*;
import java.util.Scanner;
public class Phonebook {
public static void main(String[] args) {
String rawr = "";
String Find = "";
File F = new File("MyPhoneBook.txt");
Scanner S = null;
Scanner stdin = new Scanner(System.in);
Phonebook_data[] P = new Phonebook_data[100];
System.out.println("Use e for enter, f for find, l for list, q to quit.");
System.out.println("");
for (int z = 0; z < 100; z++) {
System.out.print("Enter Command: ");
rawr = stdin.next();
rawr = rawr.toLowerCase();
if (rawr.equals("e")) {
try {
P[z] = new Phonebook_data();
S = new Scanner(F);
System.out.print("Enter Name: ");
P[z].Name = S.next();
System.out.print("Enter Number: ");
P[z].Number = S.next();
System.out.print("Enter Notes: ");
P[z].Notes = S.next();
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
if (rawr.equals("f")) {
FileOutputStream out = null;
try {
out = new FileOutputStream("MyPhoneBook.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
PrintStream ps = new PrintStream(out);
for (int i = 0; i < 100; i++) {
Find = stdin.next();
if (P[i].equals(Find))
ps.print(P[i].Name);
}
}
if (rawr.equals("q")) {
System.out.println("Goodbye.");
System.exit(1);
}
}
}
}
There's also
Code:
public class Phonebook_data{
public String Name;
public String Number;
public String Notes;
}
Code:
Enter Command: e
Enter Name: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Phonebook.main(Phonebook.java:47)