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);
}
}
toString()method in your TrafficSim class.System.out.println(sim)will invoke thetoString()method on yoursimobject (inherited from theObjectclass), since you did'nt override it, you got the default output implementation (getClass().getName() + '@' + Integer.toHexString(hashCode())).