javaoopstaticnosuchelementexception

How do i iterate and print out the next Line from my Text.txt to my output console in NetBeans?


Im trying to read out my text note from a text File, but it seems it fails to iterate towards the next line of code due to a NoSuchElementException. How do i fix this?

I do apologize in advance, due to the lack of writing etiquette i have , due to me being new to this website. But nevertheless here is my code :

public class Praktikum11_4 {
    private Scanner input;
   
    public void membukaFile()
    {
        try {
            input = new Scanner(new File("favouriteplace.txt"));
        }   catch(FileNotFoundException e)
        {
            System.err.print("File yang anda cari" +
                    "Tidak ditemukkan");
            System.exit(1);
        }   catch(IllegalStateException e)
        {
            System.err.print("Error membaca File");
        }
        
    }
    
    public void membacaData()
    {
        try{
            while(input.hasNext())
            {   
                for (int i = 1 ; i <= 7 ; i++ )
                {
                    // Scan the line
                    String location = input.nextLine();
                    double rating = input.nextDouble();
                    System.out.printf(i+". " + "%s dengan rating %f\n", location, rating);
                }
            }
        }catch (NoSuchElementException e)
        {
            System.err.println("Element not found ERROR 101 ");
        }
    }
    
    public void menutupFile()
    {
        if (input != null)
        {
            input.close();
            System.out.print("File berhasil untuk ditutup");
        }
    }
    
    public static void main(String[] args)
    {
        
    }
    

}

This is the file im trying to iterate over and print towards the output Console:

File that consists of the data](https://i.sstatic.net/dJ2KC.png)

And this is the output i got :

Five favourite place for UMN Student's

  1. Bungkushin dengan rating 4.300000

Element not found ERROR 101 File berhasil untuk ditutup

Hence, this is the output that i am expecting:

Expected Output](https://i.sstatic.net/NtKv4.png)


Solution

  • The NoSuchElementException is occurring because you are calling input.nextDouble() inside your for loop without checking if there is a next double value available in the input file. You should check if there is a next double value before trying to read it. Additionally, you should handle exceptions properly and close the file regardless of whether an exception occurs or not.

    public void readData() {
    try {
        int i = 1;
        while (input.hasNext()) {
            String location = input.nextLine();
            if (input.hasNextDouble()) {
                double rating = input.nextDouble();
                input.nextLine(); // Consume the newline character after the double
                System.out.printf("%d. %s with rating %.2f\n", i, location, rating);
                i++;
            } else {
                System.err.println("Invalid rating format for location: " + location);
                input.nextLine(); // Consume the entire line to move to the next line
            }
        }
    } catch (NoSuchElementException e) {
        System.err.println("Element not found ERROR 101");
    }
    }
    

    Make sure you call openFile() to open the file before calling readData(), and closeFile() to close the file when you're done. Also, don't forget to add your main method to execute your program.