2
 import java.io.*;
    import java.util.*;

    public class Readfilm {

    public static void main(String[] args) throws IOException {

        ArrayList films = new ArrayList();
        File file = new File("filmList.txt");
        try {
            Scanner scanner = new Scanner(file);

            while (scanner.hasNext())
            {
                String filmName = scanner.next();
                System.out.println(filmName);
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
    }}

Above is the code I'm currently attempting to use, it compiles fine, then I get a runtime error of:

java.util.NoSuchElementException  
    at java.util.Scanner.throwFor(Scanner.java:907)  
    at java.util.Scanner.next(Scanner.java:1416)  
    at Readfilm.main(Readfilm.java:15)  

I've googled the error and not had anything that helped (I only googled the first 3 lines of the error)

Basically, the program I'm writing is part of a bigger program. This part is to get information from a text file which is written like this:

Film one / 1.5
Film two / 1.3
Film Three / 2.1
Film Four / 4.0

with the text being the film title, and the float being the duration of the film (which will have 20 minutes added to it (For adverts) and then will be rounded up to the nearest int)

Moving on, the program is then to put the information in an array so it can be accessed & modified easily from the program, and then written back to the file.

My issues are:

I get a run time error currently, not a clue how to fix? (at the moment I'm just trying to read each line, and store it in an array, as a base to the rest of the program) Can anyone point me in the right direction?

I have no idea how to have a split at "/" I think it's something like .split("/")?

Any help would be greatly appreciated!

Zack.

1
  • 1
    Your code works fine for me. I don't get a NoSuchElementException, and I don't see how your code could have generated one. Commented Apr 6, 2012 at 7:17

3 Answers 3

1

Your code is working but it reads just one line .You can use bufferedReader here is an example

import java.io.*;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (strLine);
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

And here is an split example

class StringSplitExample {
        public static void main(String[] args) {
                String st = "Hello_World";
                String str[] = st.split("_");
                for (int i = 0; i < str.length; i++) {
                        System.out.println(str[i]);
                }
        }
}

Sign up to request clarification or add additional context in comments.

Comments

1

I wouldn't use a Scanner, that's for tokenizing (you get one word or symbol at a time). You probably just want to use a BufferedReader which has a readLine method, then use line.split("/") as you suggest to split it into two parts.

Comments

0

Lazy solution :

Scanner scan = ..;
scan.nextLine();

Comments

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.