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

Where is class Phrases code? It will be easier for me to find your mistakes having all your project's code.

Where is class Phrases code? It will be easier for me to find your mistakes having all your project's code.

Did you even look at his code? The problem is obviously with the way he is reading in the text file.

@OP:

Scanner's next() method reads in input between the delimiter pattern, which by default matches whitespace. In English: when you call next(), it is searching for something that has a space before and after it. Since your text file has things in it like "ahoy there" that contain multiple words, scanner.next() would've only read in ahoy, not ahoy there. Don't you want to read in the whole line? Use scanner.nextLine().

Scanner scanner = null;
		try {
			 scanner = new Scanner(new File("pirate"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int num;
		System.out.println(num = scanner.nextInt());
		scanner.nextLine();
		for(int i=0;i<num; i++){
			System.out.println(scanner.nextLine());
			System.out.println(scanner.nextLine());
		}

Edited 7 Years Ago by BestJewSinceJC: n/a

This article has been dead for over six months. Start a new discussion instead.