0

I'm trying to add this file into an arraylist which I will later compare to another arraylist. So far I have this but it is giving me a compiling error. It is saying that it cannot find the symbol .hasNext and . readLine.

ArrayList<String> america = new ArrayList<String>();
    while((infile2.hasNext()))
    {
        america.add(infile2.nextLine());
    }

Can someone help me figure out how to fix these errors?

3
  • At a guess you're infile2 is probably something like a BufferedReader when it should be a Scanner Commented Apr 16, 2014 at 2:37
  • What kind of object/class is infile2? Commented Apr 16, 2014 at 2:37
  • Yes it is a bufferedReader but my teacher says not to change it. So I can't make it into a scanner Commented Apr 16, 2014 at 2:38

3 Answers 3

1

Yes it is a bufferedReader

BufferedReader doesnt have a hasNext method, therefore you can use readLine

String line;
while((line = infile2.readLine()) != null) {
    americaList.add(line);
}
...

Or if you could use Files

List<String> americaList = 
         Files.readAllLines(Paths.get("list.txt"), StandardCharsets.UTF_8);
Sign up to request clarification or add additional context in comments.

Comments

1

Here is one possible way to do it.

import java.util.Scanner;

Scanner inFile2 = new Scanner(new File("INPUT_FILE_NAME"));

Then, change your loop to use hasNextLine() instead of hasNext(). For Java's Scanner you should always pair the has with the corresponding type of next.

You can wrap the BufferedReader that someone hands you inside a Scanner.

 Scanner inFile = new Scanner(myBufferedReader);

4 Comments

I'm not allowed to change the BufferedReader file into a scanner.... Is there another way?
@Ayoshna, See the update and let me know whether you are allowed to wrap it.
Nope. He has given us this line and we are not allowed to change his work. This is what it says: BufferedReader infile2 = new BufferedReader( new FileReader("allPresidents.txt") );
@Ayoshna, Can you add a line below it, that says Scanner infile3 = new Scanner(infile2);? That does not change his original line.
0
       /**
 * one time read all
 * 
 * @param location
 * @return
 */
public static List<String> readLine(String location){
    BufferedReader is = null;
    List<String> result = new ArrayList<String>();
    try {
        is = new BufferedReader(new FileReader(new File(location))); 
        String line = null;
        while((line = is.readLine())!=null){
            result.add(line);
        }
    } catch (FileNotFoundException e) {
        throw Exceptions.unchecked(e);
    } catch (IOException e) {
        throw Exceptions.unchecked(e);
    } finally{
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                throw Exceptions.unchecked(e);
            }
        }
    }

    return result;
}

3 Comments

A simple text scanner which can parse primitive types and strings using regular expressions. I think BufferedReader will be better.
Can you explain the line where you assigned is. What do you mean by location?
location is the file's path, example c:\\a.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.