Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:27:24+00:00 2026-06-15T18:27:24+00:00

Information : when I execute my program, a simple file reader, I get an

  • 0

Information :

when I execute my program, a simple file reader, I get an exception.

I have my file saved next to my .java files.

Output :

run:
name : Koen
score : 44
Exception in thread “main” java.util.NoSuchElementException: No line found
name : Kevin
score : 55
at java.util.Scanner.nextLine(Scanner.java:1585)
at tetris.FileIO.loadHighscores(FileIO.java:41)
at tetris.FileIO.getLineScores(FileIO.java:28)
at tetris.FileIO.main(FileIO.java:62)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Code:

package tetris;

import java.io.File;
import java.io.InputStream;
import java.io.Writer;
import java.util.Scanner;

public class FileIO   {
    private File file;
    private Scanner filescScanner, lineScanner;
    private Writer fileWriter, lineWriter;
    private String[][] data;

    public FileIO () {
        String[][] data = new String[100][1];
    }
    public String[][] getLineScores(){
        return this.loadHighscores(this.getClass().getResourceAsStream("LineHighscores.txt"));
    }
    public String[][] getTimeScores(){
        return this.loadHighscores(this.getClass().getResourceAsStream("TimeHighscores.txt"));
    }

    public String[][] loadHighscores( InputStream resourceStream){

        int x=0;
        String test = "";

        filescScanner = new Scanner(resourceStream);

        while((test=filescScanner.nextLine())!=null) {

            lineScanner = new Scanner(test);
            lineScanner.useDelimiter("-/-");
            System.out.println("name : "+lineScanner.next());
            System.out.println("score : " +lineScanner.next());

            x++;
        }
        lineScanner.close();
        filescScanner.close();
        return data;    
    }

    public static void main(String[] args){
        FileIO file = new FileIO();
        System.out.println(file.getLineScores());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:27:26+00:00Added an answer on June 15, 2026 at 6:27 pm

    When you are calling nextLine():

     while((test=filescScanner.nextLine())!=null)
    

    it is throwing an exception to indicate that the scanner has no more lines of input. Thus, it will never return null.

    Try instead to use hasNextLine() to check if there is a next line.

    while(filescScanner.hasNextLine())
    {
        test=filescScanner.nextline();
        ...
    }
    

    hasNextLine

    public boolean hasNextLine()

    Returns: true if and only if this scanner has another line of input (source)

    Therefore, when the scanner do not have any more lines of input to read, hasNextLine() will return false. Hence, terminating the cycle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple program that aks questions then call a php file saying
I'm trying to run R using Java. I have R installed on my Mac
I have a program that I need to execute at certain intervals. For instance,
Am trying to get some order information from XML file and trying to store
I'm using a CreateProcess call within a C++ program to execute a JAR file
I'm writing a combination Java/Perl program that parses XML files into an Oracle SQL
I am trying to run my first Java program, an example script that interacts
I'm trying to get the local 'Program files' dir path and append some extra
i'm trying to create a simple program that will upload a few files for
I'm working on a Java program that incorporates Process and Runtime to run several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.