0

I'm doing a project for a class, but for the life of me I'm having the hardest time figuring out how to read text from a file. We have to create a traffic light that queues trucks and cars coming from North, South, East, and West. It's been a long time since I've done any coding, so I'm struggling immensely. I think it just reads the memory location. Here's my code for reading in a file.

package Project1;
import java.io.*;
import java.util.*;


public class TrafficSim {

public String input;

public TrafficSim(String input)
{
    this.input = input;
    readFromFile();
}



private boolean readFromFile()
{
    File inputText = new File("input1.txt");

    try
    {
        Scanner scan = new Scanner(inputText);

        while(scan.hasNextLine())
        {
            String direction = scan.nextLine();
            int num = scan.nextInt();
        }
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }
    return false;
}



public static void main(String[] args) {
    // TODO Auto-generated method stub      
    TrafficSim sim = new TrafficSim("input1.txt");
        System.out.println(sim);    
}

}
3
  • You need to override the toString() method in your TrafficSim class. Commented Feb 21, 2014 at 20:25
  • So I have to make a toString() method? Something like public String toString() { str = ""; System.out.println()} Commented Feb 21, 2014 at 20:26
  • 1
    System.out.println(sim) will invoke the toString() method on your sim object (inherited from the Object class), since you did'nt override it, you got the default output implementation (getClass().getName() + '@' + Integer.toHexString(hashCode())). Commented Feb 21, 2014 at 20:29

2 Answers 2

2

Your method readFromFile sure enough reads from a file, but then it doesn't do anything. All you do is read line by line, storing a line of text and an int in variables which are forgotten after each iteration of your while loop.

Your code System.out.println(sim) prints out whatever the toString method of your class returns, and since you didn't override that method it will print out the result of Object.toString, which is not what you want.

To put it simply, you're reading from a file but you're not doing anything with the contents that you read.

Sign up to request clarification or add additional context in comments.

5 Comments

I hate to be a pain, but I'm not exactly sure on how to fix this. I'm completely lost.
The gist is, your readFromFile method reads from a file, line-by-line, but that's all it does. It reads each line and forgets each line. It doesn't process each line to do something with it.
Ok. I understand that, but how do I go about fixing it?
What are you trying to do with the information from the file?
Start by defining some data structure to hold the information from the file, whatever it is. Then, have readFromFile populate that data structure with the direction and num values it reads from the file.
0

Here is what I would do....

public class TrafficSim {

    private String input;
    private String content;

    public TrafficSim(String input) {
        this.setInput(input);
        this.setContent(readFromFile());
    }

    private String readFromFile() {
        File inputText = new File(input);
        StringBuilder sb = new StringBuilder();

        try {
            Scanner scan = new Scanner(inputText);
            while (scan.hasNextLine()) {
                sb.append(scan.nextLine());
            }
            scan.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public static void main(String[] args) {
        TrafficSim sim = new TrafficSim("input1.txt");
        System.out.println(sim.getContent());
    }
}

The issue I see though is that you're not following the comments and suggestions already made. ktm5124 was pretty clear on what the problem is. At some point you're going to have to understand what is going on here and how to fix it.

1 Comment

Thanks for your help. I understand what ktm5124 was saying, I just didn't know how to fix it. To better understand this, could you walk me through what you did and compare that to where I screwed up?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.