Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Running into a problem with my program.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Running into a problem with my program.

    Hello, im having trouble with getting this program I am writing for one of my courses to run. The program has a 4 different classes. I am having trouble getting the program to read input from a file, having it take those items and creating vehicle objects, and then putting those vehicle objects into an array. The code I am providing below is only part of the program but should be enough to help me with the problem im having so far.
    the file it is reading from(fleetCars.txt) contains this information. There is a tab between each item for each car.

    Hyundai Sonata 2010 ABC236347NM2N2NW2 18455.34 8765 7567
    Chevy Blazer 1998 1234H32343LMN3423 29556.65 38559 38559


    public class Fleet
    {
    	Vehicle[] fleetVehicles = new Vehicle[4];
    	public Fleet()
    	{
    		super();
    	}
    	public Fleet(String fileName) throws IOException
    	{
    		Scanner inputFile = new Scanner(fileName);
    		for (int i = 0; i < fleetVehicles.length; i++)
    		{
    			Vehicle vehobj = new Vehicle();
    			vehobj.readRecord(inputFile);
    			fleetVehicles[i] = vehobj;
    		}
    	}
     
    	public String toString()
    	{
    		Vehicle obj = new Vehicle();
    		for (int i = 0; i < fleetVehicles.length; i++)
    		{
    			fleetVehicles[i] = obj;
    			return obj.toString();
    		}
    		return obj.toString();
    	}

    public class Vehicle
    {
    	private String vin;
    	private String make;
    	private String model;
    	private int year;
    	private double value;
    	private int milesDriven;
    	private int lastOilChange;
     
    	public Vehicle()
    	{
    		super();
    	}
     
    	public Vehicle(String vin, String make, String model, int year,
    			double value, int milesDriven, int lastOilChange)
    	{
    		this(vin, make, model, year, value);
    		this.milesDriven = milesDriven;
    		this.lastOilChange = lastOilChange;
    	}
     
    	public String toString()
    	{
    		return "Make:\t\t" + this.make + "\nModel:\t\t" + this.model
    				+ "\nYear:\t\t" + this.year + "\nValue:\t\t" + this.value
    				+ "\nVIN:\t\t" + this.getVIN() + "\nMiles Driven: "
    				+ this.milesDriven + "\nNext oil change at "
    				+ this.getMilesToNextOilChange() + " miles";
    	}
     
    	public void readRecord(Scanner inputFile) throws IOException
    	{
    	make = inputFile.next();
    	model = inputFile.next();
    	year = inputFile.nextInt();
    	vin = inputFile.next();
    	value = inputFile.nextInt();
    	milesDriven = inputFile.nextInt();
    	lastOilChange = inputFile.nextInt();
    	}

    The last bit of code is the part that runs the whole program. This part asks the user for the name of the file. It then is supposed to open the file and use it to build the fleet object.

    public class FleetInterfaceApp
    {
    	private Fleet fleet;
    	private Menu menu;
     
    	private void createMenu()
    	{
    		String[] choices =
    		{ "Add a vehicle", "Delete a vehicle", "Update miles driven",
    				"Record oil change", "Print listing of all fleet vehicles",
    				"Print listing of all vehicles to be replaced",
    				"Print listing of all vehicles that need and oil change",
    				"Quit" };
     
    		menu = new Menu(choices);
    	}
     
    	public void run() throws IOException
    	{
     
    		Scanner keyboard = new Scanner(System.in);
    		System.out
    				.println("What is the name of the file containing the fleet vehicle information?");
    		String fileName = keyboard.nextLine();
    		File file = new File(fileName);
    		Scanner inputFile = new Scanner(file);
    		Fleet fleet = new Fleet(fileName);
     
    		int choice;
    		this.createMenu();
    		choice = this.menu.getChoice();
    		System.out.println("You entered choice " + choice);
    		if (choice == 1)
    			choice1();
    		if (choice == 2)
    			choice2();
    		if (choice == 3)
    			choice3();
    		if (choice == 4)
    			choice4();
    		if (choice == 5)
    			choice5();
    		if (choice == 6)
    			choice6();
    		if (choice == 7)
    			choice7();
    	}

    This is the error I get when I run fleetInterFaceApp

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at Vehicle.readRecord(Vehicle.java:285)
    at Fleet.<init>(Fleet.java:51)
    at FleetInterfaceApp.run(FleetInterfaceApp.java:47)
    at FleetInterfaceApp.main(FleetInterfaceApp.java:166)

    Thanks in advance for any help!
    Last edited by letsgetlifted; December 9th, 2011 at 04:38 PM. Reason: accidently pasted it twice


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Running into a problem with my program.

    Code practices aside, I'd be willing to bet that the reason you're getting that error is because of the blank line in-between your data in the file.
    Remove all empty lines from the file and then return detailing what other errors you get .
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Running into a problem with my program.

    Thats not how it is in the file. I just realized i hit paste twice sorry. What else could it be?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Running into a problem with my program.

    In that case, you've only got 2 lines of data, but attempting to retrieve four lines worth of data.
    		//fleetVehicles.length is 4!
    		for (int i = 0; i < fleetVehicles.length; i++)
    		{
    			//readFile
     
    		}

    You should really be incorporating the hasNext() methods of the Scanner API, so it checks whether there are more tokens to process before trying to retrieve them.
    Have a look:http://docs.oracle.com/javase/1.5.0/...l/Scanner.html
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Running into a problem with my program.

    It still does it even if I change the array to 2.

    Should I be using the has next inside the Fleet object constructor or where at?

    I tried this but it still didnt work.

    public Fleet(String fileName) throws IOException
    	{
    		Scanner inputFile = new Scanner(fileName);
    		int counter = 0;
    		while (inputFile.hasNext())
    		{
    			Vehicle vehobj = new Vehicle();
    			vehobj.readRecord(inputFile);
    			fleetVehicles[counter] = vehobj;
    			counter++;
    		}

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Running into a problem with my program.

    The 5th input you read in, is as follows, 18455.34

    The line of code used to load that is,

    value = inputFile.nextInt();

    Do you see the potential problem?

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Running into a problem with my program.

    Thanks Newbie and Freaky Chris. I did not realize that I had put nextInt and not nextDouble. I fixed that and the program is still giving me this error.

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at Vehicle.readRecord(Vehicle.java:296)
    at Fleet.<init>(Fleet.java:50)
    at FleetInterfaceApp.run(FleetInterfaceApp.java:47)
    at FleetInterfaceApp.main(FleetInterfaceApp.java:166)

    Any other suggestions?

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Running into a problem with my program.

    How far exactly does it get, does it read one line then crash on the second?

    Which is the exact line of code it crashes on, there are multiple .next() calls in that method and I don't know which it is.

    Chris

  9. #9
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Running into a problem with my program.

    Well now you see, it still tries to access data which isn't there.
    Using while (inputFile.hasNext()), when it reaches the very last token, it will still call your readRecord() method, where it will invoke .next() 7 times.
    Last edited by newbie; December 9th, 2011 at 05:44 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. #10
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Running into a problem with my program.

    I don't believe that it even starts to read them. I tried using the debugger and it ends up saying source not found with a button that says edit source look up path...

    Im really stumped on this... any other ideas?

Similar Threads

  1. Problem running .jar in Windows 7
    By SpiceProgrammer in forum Java Theory & Questions
    Replies: 3
    Last Post: December 21st, 2011, 01:28 AM
  2. NullPointerException when running program...
    By RiskyShenanigan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 12th, 2011, 04:30 PM
  3. Replies: 3
    Last Post: February 23rd, 2011, 01:27 AM
  4. Problem running Apache tomcat 6.0 using i.p. address
    By nakul in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: April 8th, 2010, 07:58 AM
  5. Textpad Program Running Problems
    By Paddy in forum Java IDEs
    Replies: 4
    Last Post: January 27th, 2010, 09:14 PM