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

  • Home
  • SEARCH
  • 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 7794105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:41:33+00:00 2026-06-01T22:41:33+00:00

I am currently working on a program for one of my java class and

  • 0

I am currently working on a program for one of my java class and I keep running into a wall with file reading! I am using a JTable to display the information so when the information is read from a file it is added to a row. Whenever there is a blank row the scanner cannot read it and throw an error! i get a java.util.NoSuchElementException on the last scanning line! As of right no i am suing two separate scanners. I had attempted to use the String.split method that that also gave me an error (ArrayIndexOutOfBoundsException: 0). I will post the reading a saving methods below (both two scanner version and split).

private void read() {

    try {
        Scanner scanner = new Scanner(new File("games.dat"));


        scanner.useDelimiter(System.getProperty("line.separator"));


        while (scanner.hasNext()) {
      String[] tableRow = new String[6];
            Scanner recIn = new Scanner(record);
            recIn.useDelimiter("\\s*\\|\\s*");
            tableRow[0] = recIn.next();
            tableRow[1] = recIn.next();
            tableRow[2] = recIn.next();
            tableRow[3] = recIn.next();
            tableRow[4] = recIn.next();
            //recIn.next();
            recIn.close();
            model.addRow(new Object[]{tableRow[0],
     tableRow[1], tableRow[2],
     tableRow[3], tableRow[4]});

}scanner.close();
scanner = null;

    } catch (Exception ex) {
        JOptionPane.showConfirmDialog(null, "Could not connect to file! Make sure you are not in        zipped file!",
                "Warning!", JOptionPane.OK_OPTION,
                JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();

    }
}



private void save() {

    for (int i = 0; i < model.getRowCount(); i++) {


        String data = model.getValueAt(i, 0) + "|" + model.getValueAt(i, 1)
                + "|" + model.getValueAt(i, 2) + "|" + model.getValueAt(i, 3)
                + "|" + model.getValueAt(i, 4) + "|";
        games.add(data);

}

 try {
        for (int i = 0; i < games.size(); i++) {
            fileOut.println(games.get(i));
        }
        fileOut.close();

    } catch (Exception ex) {
        JOptionPane.showConfirmDialog(null, "Could not connect to file! Make sure you are not in zipped file!",
                "Warning!", JOptionPane.OK_OPTION,
                JOptionPane.ERROR_MESSAGE);

    }
}
  • 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-01T22:41:34+00:00Added an answer on June 1, 2026 at 10:41 pm

    recIn.next(); will fail if the line is empty, guard it with hasNext():

    Scanner recIn = new Scanner(record); 
    recIn.useDelimiter("\\s*\\|\\s*");
    if (recIn.hasNext()) {
      tableRow[0] = recIn.next(); 
      tableRow[1] = recIn.next(); 
      tableRow[2] = recIn.next(); 
      tableRow[3] = recIn.next(); 
      tableRow[4] = recIn.next();
    }
    

    This assumes that when there is one element on the record, all of them are there. If this cannot be guaranteed, you need to protect each next() call with hasNext() and decide what to do when you run out of elements in the middle of a record.

    Also, you seem to have an infinite loop:

    while (scanner.hasNext()) {
      // no calls to scanner.next()
    }
    

    Did you leave out String record = scanner.next(); from the top of that loop?

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

Sidebar

Related Questions

I am currently in a Java class using Java: How to Program, Ninth Edition
i'm working with a multi-threaded program (using pthreads) that currently create a background thread
I'm using JNI to access a native C file through my java program. I've
I'm back with another similar question. I am currently working on a Java program
Currently I'm working on a program that parses through a file and takes characters
I'm currently working on a program that analyses a wav file of a solo
I'm currently working with Java to write a program that does an EAI between
I'm currently working on a Program, that presses buttons for me. I'm working on
I'm currently working on a C++ program in Windows XP that processes large sets
I am currently working on a VB.Net program where there are two parallel multiline

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.