Currently I am trying to read from a file using java and it is just producing errors. I need help with this as I have been unable to fix this myself and have come to a wall with this. Could you just take a quick glance over this and see what I am doing wrong here. I'll mark any errors encountered in the code.
import java.util.*;
import java.io.*;
public class Dictionary
{
private ArrayList<Phrases> phrase;
private ArrayList<Phrases> pirate;
private ArrayList<Phrases> english;
public Dictionary() {
phrase=new ArrayList<Phrases>(30);
}
public void add(Phrases p){
phrase.add(p);
}
public void printout(){
System.out.println("The phrases in the dictionary are: ");
for (Phrases p:phrase) {
System.out.println(p.toString());
}
}
public void search(String english){
for (Phrases p:phrase) {
if (pirate.equals(p.getEnglish())) {
System.out.println( p.toString());
return;
}
}
System.out.println("Not in dictionary, bad search");
}
public void load(String pirateTxt) throws IOException{
Scanner infile=new Scanner(new InputStreamReader(
new FileInputStream(pirateTxt)));
int num=infile.nextInt();
infile.nextLine();
for(int i=0;i<num; i++){
String pi=infile.next();
String e=infile.next(); NoSuchElementException:null (in java.util.Scanner)
Phrases p=new Phrases(pi,e);
phrase.add(p);
}
infile.close();
}
public void save(String pirateTxt) throws IOException{
PrintWriter outfile = new PrintWriter
(new OutputStreamWriter
(new FileOutputStream(pirateTxt)));
outfile.println(phrase.size());
for (Phrases p:phrase) {
outfile.println(p.toString());
}
outfile.close();
}
}
This is the text file I am trying to read from called pirate.txt
5
ahoy there
hello
me hearties
dear friends
well, shiver me timbers
gosh - how exciting
arrrrrrgh!
goodness!
you're a bilge rat
you are a horrid person