dfl2m's Profile
Reputation: 0
Apprentice
- Group:
- New Members
- Active Posts:
- 11 (0.07 per day)
- Joined:
- 05-December 12
- Profile Views:
- 60
- Last Active:
Feb 25 2013 06:16 PM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Read a file into ArrayList
Posted 24 Feb 2013
I changed my code to the following:
public void loadDept(File file) throws Exception { Scanner scanner = new Scanner(file); while(scanner.hasNext()){ String line = scanner.nextLine(); String [] parts = line.split(" "); if (parts[0].equals("Store")){ s = Integer.parseInt(parts[2]); } else if (parts[0].equals("Dept")){ d = Integer.parseInt(parts[2]); } else { String dName = ""; for (int i = 0; i < parts.length; i++){ dName += parts[i] + " "; } days = scanner.nextInt(); Store entry = new Department(s,dName,d,days); depts.add(entry); scanner.nextLine(); } } print(depts); }
But now I get the following error:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at LD.loadDept(LD.java:33)
at LD.<init>(LD.java:12)
at Main.main(Main.java:8)
do i need to add a blank line at the end of the output?? i tried tracing and debugging and eclipse would not tell me if the array is being filled. -
In Topic: Read a file into ArrayList
Posted 24 Feb 2013
pbl, on 24 February 2013 - 09:00 PM, said:Sorry, might work seen that way... still an horrible design but it will work
so your question: why this one
days = scanner.nextInt();
fails ?
Yes, because the code will work fine if I just create one object and no more, but when it tries to create the second object it fails. I would assume since there are more lines in the text file, that the code will be able to translate it.
When you say "horrible design" what exactly do you mean? Is there a way to recognize the integer in the first line without splitting it into an array of strings? -
In Topic: Read a file into ArrayList
Posted 24 Feb 2013
The input file is as follows:
Store ID: 1
Dept ID: 1030
House Wares Department
3
Store ID: 1
Dept ID: 1035
Mens
4
Store ID: 2
Dept ID: 1030
House Wares Department
3 -
In Topic: Count frequency of numbers in a phone number using ArrayList
Posted 30 Jan 2013
Okay it seems i figure it out
for (int i = 0; i < frequencySize; i++){ int count = 0; //reset this after every iteration of the outer loop for (int j = 0; j < arraySize; j++){ if (i == array.get(j)){ //for example, if i = 0, and array.get(j) = 0, then count++; //incremement count by 1 frequency.set(i, count); // set frequency(0, 1) } } System.out.println("The number of " + i + " is " + frequency.get(i)); } -
In Topic: Count frequency of numbers in a phone number using ArrayList
Posted 29 Jan 2013
CasiOo, on 29 January 2013 - 10:29 AM, said:
dfl2m, on 29 January 2013 - 04:20 PM, said:
farrell2k, on 29 January 2013 - 09:48 AM, said:You have an array of integers and you can't imagine how to substitute its values instead???
I think I have use the .set option after creating the frequency array.
so it would have to be something like
ArrayList<Integer>frequency = new ArrayList<Integer>(size); int count = 0; for (int element: originalArray){ frequency.set(element, count++); }
i think.
i know how to do it when building an array using int [] whatever = new int[10], etc. but just not using ArrayList.
Almost. The ArrayList should contain how many times a number has occurred
So if the number 1 occurs 5 times, the follow should be true
frequency.get(1) == 5
Will this be the case with your own code? No!
You are using the same count variable for all the numbers
What you are currently counting is how many numbers there are, not the individual count of them =o
okay, i will have to look at my code later b/c i'm at work.
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Private
Friends
dfl2m hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
dfl2m has no profile comments yet. Why not say hello?