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 7561037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:03:50+00:00 2026-05-30T13:03:50+00:00

I have the following piece of code. I do not understand why its not

  • 0

I have the following piece of code. I do not understand why its not working.

I’d really appreciate help on this.

import java.util.Scanner;
import java.io.*;

class ReadFiles {
    String [] codes = new String[99];
    int i = 0;

    private Scanner readCodes;

    public void openCodesFile() {
        try {
            readCodes = new Scanner(new File("C:/Users/Carlo/Desktop/Files/codes.txt"));
        } catch (Exception e) {
            System.out.println("Could not locate the data file!");
        }
    }

    public void readCodesFile() {

        while(readCodes.hasNext()) {
            codes[i] = readCodes.nextLine();
            i++;
            System.out.println(codes[i]);
        }
    }

    public void closeCodesFile() {
        readCodes.close();
    }
}

class NewHardware {
    public static void main(String[] args) {
        ReadFiles codesRead = new ReadFiles();
        codesRead.openCodesFile();
        codesRead.readCodesFile();
        codesRead.closeCodesFile();
    }
}

The output prints out “null” a bunch of times.

Also, I want to be able to not only print out the codes but use the codes array in the class NewHardware and manipulate it (print it out, truncate it, etc).

I was thinking of doing the following with readCodesFile():

public String readCodesFile() {

        while(readCodes.hasNext()) {
            codes[i] = readCodes.nextLine();
            i++;
            System.out.println(codes[i]);
        }
                    return (codes[i]);
    }

Or something but it hasn’t worked just yet. Am I on the right track?

Oh, just wanted to add that the text contains the following:

G22
K13
S21
I30
H15
N23
L33
E19
U49

EDIT:

Thanks to Tony and Churk below to help me with my idiocy. I am accepting Tony’s answer basically because he challenged me to think but Churk’s answer is just as valuable.

For the second part of my question (where I asked about being able to use it in class NewHardware), I did the following:

class NewHardware {
    public static void main(String[] args) {
        ReadFiles codesRead = new ReadFiles();

        codesRead.openCodesFile();

        codesRead.readCodesFile();

        for (int i = 0; i < 9; i++) {
            System.out.println("\n\n" + codesRead.codes[i]);
        }

        codesRead.closeCodesFile();
    }
}

This is of course not the final program code but this has helped me get the basic idea. Hope this helps others too.

  • 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-05-30T13:03:51+00:00Added an answer on May 30, 2026 at 1:03 pm

    Look carefully at your readCodesFile() method. Look at every line. What is it doing? Can you explain it to us?

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

Sidebar

Related Questions

I have the following piece of code which is not working the way I
I have the following piece of Mootools 1.11 code (not upgradable as this is
I have the following piece of code from a book. There is this function
I have the following piece of code which is not running as I expected:
I have the following piece of code: while current is not problem.getStartState(): print Current:
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have following piece of code : public Hashmap<String,String> tempmap = new HashMap<String,String>(); and
I have the following piece of code pattern: void M1(string s, string v) {
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have the following piece of code taken from the PHP manual on the

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.