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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:21:54+00:00 2026-05-27T11:21:54+00:00

how do I get the read txt file into the main class? //main class

  • 0

how do I get the read txt file into the main class?

//main class
public class mainClass {

public static void main(String[]args) {
    load method = new load("Monster");
}
}





//scanner class

public class load {

  public static void loader(String... aArgs) throws FileNotFoundException {
    load parser = new load("resources/monsters/human/humanSerf.txt");
    parser.processLineByLine();
    log("Done.");
  }

  public load(String aFileName){
    fFile = new File(aFileName);  
  }

  public final void processLineByLine() throws FileNotFoundException {
    //Note that FileReader is used, not File, since File is not Closeable
    Scanner scanner = new Scanner(new FileReader(fFile));
    try {
      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
        processLine( scanner.nextLine() );
      }
    }
    finally {

      scanner.close();
    }
  }

  public void processLine(String aLine){
    //use a second Scanner to parse the content of each line 
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter("=");
    if ( scanner.hasNext() ){
      String name = scanner.next();
      String value = scanner.next();
      log("Stat is : " + quote(name.trim()) + ", and the value is : " + quote(value.trim()) );
    }
    else {
      log("Empty or invalid line. Unable to process.");
    }
  }

  public final File fFile;

  public static void log(Object aObject){
    System.out.println(String.valueOf(aObject));
  }

  public String quote(String aText){
    String QUOTE = "'";
    return QUOTE + aText + QUOTE;
  }
} 

Which method do I call from the main class and what variables do I return from that method if I want the text from the file. If anyone has a website that can help me learn scanner(got this source code of the internet and only sort of understand it from JavaPractises and the sun tutorials) that would be great. thanks

  • 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-27T11:21:54+00:00Added an answer on May 27, 2026 at 11:21 am

    First, you probably want to follow standard Java naming conventions – use public class MainClass instead of mainClass.

    Second, for your methods, the public has a specific purpose. See here and here. You generally want to label methods as public only as necessary (in jargon, this is known as encapsulation).

    For your question – in the Load class, you can append all the text from the file to a String, and add a public getter method in Load which will return that when called.

    Add this at the start of Load:

    public class Load {
        private String fileText;
        // ... rest of class
    

    And add this getter method to the Load class. Yes, you could simply mark fileText as public, but that defeats the purpose of Object-Oriented Programming.

    public getFileText(String aFileName){
        return fileText;
    }
    

    Finally, use this new method for log. Note that there is no need to use Object.

    private static void log(String line) {
        System.out.println(line);
        fileText += aObject;
    }
    

    You can now get the read file into the main class by calling method.getFileText()

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

Sidebar

Related Questions

I am trying to read file by following code void main() { int i=0;
I'm trying to read a list of words from a txt file into a
Task 1: Read each row from one csv file into one seprate txt file.
I'm trying to get Python to a read line from a .txt file and
I would like to read a (fairly big) log file into a MATLAB string
I've read Get File Icon used by Shell and the other similar posts -
Can JQuery/JavaScript read/get the file content on the client side, where the file is
How do I get Perl to read the contents of a given directory into
using Fast CGI I can't get it to read the php.ini file. See my
Consider the following simple C program that read a file into a buffer and

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.