Home  >  Article  >  How to iterate and print the next line in Text.txt to the output console in NetBeans?

How to iterate and print the next line in Text.txt to the output console in NetBeans?

王林
王林forward
2024-02-22 13:40:11950browse

java Q&A brought by php editor Banana, today we will discuss how to implement iteration in NetBeans and print the next line of content in the Text.txt file to the output console. This question involves the skills of file reading and iteration, which can be easily accomplished through reasonable code implementation. Let’s take a look at the specific solutions!

Question content

I'm trying to read my text comments out of a text file, but it can't seem to iterate to the next line of code due to a nosuchelementexception. How can I solve this problem?

I apologize in advance for my lack of writing etiquette as I am new to this site. 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 I'm trying to iterate over and print to the output console:

File composed of data](https://i.stack.imgur.com/dj2kc.png)

This is the output I get:

umn Five favorite places among students

  1. bungkushin dengan Rating 4.300000

Element not found error 101 file berhasil untuk ditutup

So this is my expected output:

Expected output](https://i.stack.imgur.com/ntkv4.png)

Solution

nosuchelementexception occurs because you call input.nextdouble inside a for loop () without checking whether there is the next available double value in the input file. You should check if the next double value exists before trying to read it. Also, whether an exception occurs or not, the exception should be handled correctly and the file closed.

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 to call openfile() to open the file before calling readdata(), and call closefile() to close the file when finished. Also, don't forget to add the main method to execute the program.

The above is the detailed content of How to iterate and print the next line in Text.txt to the output console in NetBeans?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete