Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 30th, 2012, 07:29 PM
addisongabe addisongabe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 addisongabe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 48 sec
Reputation Power: 0
Homework - Java ArrayList / File Input Help

Hi, I'm writing a program for a Computer Science project and am having trouble. The program is dealing with creating a custom class (Height) with two private int variables (pounds, ounces). When working with the main file (HeightsSorter) I am trying to read a file for input, create Height objects from each line of input and add them to an existing ArrayList that I have created. I can't seem to figure this out. For some reason it doesn't seem to be adding any of my Height objects to my ArrayList. If anyone could help me it would be much appreciated!

Here is the code I have for my main class:

Scanner keyb = new Scanner(System.in);

System.out.print("Enter the file name> ");

String dataFile = keyb.next();

Scanner readFile = new Scanner(new File(dataFile));

int ft, in;

ArrayList<Height> heights = new ArrayList<Height>();

while (readFile.hasNextInt())
{
ft = readFile.nextInt();

in = readFile.nextInt();

heights.add(new Height(ft,in));
}

readFile.close();

int i;

System.out.println("Unsorted Names from "+dataFile+":");
for (i=0; i<heights.size();i++)
{
System.out.println(heights.get(i));
}



The output is this:

Enter the file name> heights.txt
Unsorted Names from heights.txt:


I am pretty sure that the file is right. I know that the name is. But I just don't know what I'm doing wrong!

Please help,
Addison

Reply With Quote
  #2  
Old December 1st, 2012, 06:29 AM
Aurum84 Aurum84 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 74 Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
Your code works perfectly here, after adding a try/catch around the File argument of the Scanner.

I do advise you however to put the declaration of ft and in inside the while loop, as you now have the risk to end up with Height objects with the same attribute values.

I also changed the last for-loop to:
Code:
for (i=0; i<heights.size();i++)
{
    System.out.println(heights.get(i).toString());
}


and have the Height class override the toString method to for example:

Code:
public String toString()
{
	return "["+this.feet+","+this.inch+"]";

}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Java ArrayList / File Input Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap