Results 1 to 5 of 5
Thread: parallel arrays in java
- 02-17-2012, 06:53 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
parallel arrays in java
Ques: How to get and display the student with the least monthly savings? My coding gives me the least monthly savings but with the incorrect student names.
Please kindly advise how to fix the error.
Java Code:public class monthlySavings { public static void main(String[] args) { String[] studentNames = new String[5]; double[] monthlySavings = new double[5]; getUserInput(studentNames,monthlySavings); System.out.println("***********************************************"); getMinMonthlySavings(studentNames,monthlySavings); } public static void getUserInput(String[] studentNames, double[] monthlySavings) { Scanner console = new Scanner(System.in); try { for(int i=0; i<studentNames.length; i++) { System.out.print("Enter the student names: "); studentNames[i] = console.next(); System.out.print("Enter the monthly savings: "); monthlySavings[i] = console.nextDouble(); } } catch(InputMismatchException inputMismatchException) { System.out.println("Invalid input. Please try again."); console.nextLine(); } } public static void getMinMonthlySavings(String[] studentNames,double[] monthlySavings) { NumberFormat money = NumberFormat.getCurrencyInstance(); double minValue = monthlySavings[0]; String studNames = studentNames[0]; //get each student names for(int j=0; j<studentNames.length; j++) { //get each monthly savings for(int i=0; i<monthlySavings.length; i++) { //to determine the least monthly savings amount if(monthlySavings[i] < minValue) { studNames = studentNames[j]; minValue = monthlySavings[i]; } } } System.out.printf("Student " + studNames + " has the least monthly savings of " + money.format(minValue) + "%n"); }Last edited by pbrockway2; 02-17-2012 at 07:23 AM. Reason: code tags added
- 02-17-2012, 07:24 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: parallel arrays in java
Hi javanerd123, welcome to the forums!
When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end: that way the code will be readable when it appears here.
- 02-17-2012, 07:30 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: parallel arrays in java
By nesting two for loops like this you end up looking at quite a few things. If there are 5 students involved then the comparison "if(monthlySavings[i] < minValue)" will be evaluated 25 times. Write down 5 names and numbers and find the name associated with the lowest number. Do you need to make 25 comparisons, or only 5? Hint: you only look at the numbers once each, and you make only 5 comparisons. Your code should do the same.Java Code://get each student names for(int j=0; j<studentNames.length; j++) { //get each monthly savings for(int i=0; i<monthlySavings.length; i++) {
- 02-17-2012, 07:40 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 56
- Rep Power
- 0
Re: parallel arrays in java
Basically, the way your code is running is like that
Lets say I have 3 names and 3 savings
Jojo
Bobo
Toto
50
10
20
CURRENT STUDENT NAME: JOJO
CURRENT MIN SAVING: 50
1ST NAME LOOP
it will first run through JOJO
1ST SAVING LOOP
check if 50 < 50
FALSE!!!!
i++
check if 10 < 50
TRUE!!!
MINIMUM SAVING = 10
CURRENT STUDENT NAME = j (WHICH IS STILL 0 BECAUSE IT HAS NOT GONE OUT OF THE 2ND FOR LOOP)
i++
check if 10 < 20
FALSE!!!!!
finish 2nd for loop
goes back to 1st for loop.
2ND NAME LOOP
J=1
DOES NOTHING
2ND MIN SAVING LOOP
check if 10 < 50
FALSE!!!!
i++
check if 10 < 10
FALSE!!!
i++
check if 10 < 20
FALSE!!!!!
COMPLETES 2ND MIN SAVING LOOP
3RD NAME LOOP
j=2
DOES NOTHING
3RD MIN SAVING LOOP
J=3
check if 10 < 50
FALSE!!!!
i++
check if 10 < 10
FALSE!!!
i++
check if 10 < 20
FALSE!!!!!
So basically your student name will always be stuck at [0] because j will always be 0 when it enters the 2nd for loop, and it will never enter the 2nd for loop again. Hope I explained it well enough.The code is simple, so try to figure it out with the logic I've given. Hopefully it will help you in future for loops as well.
EDIT: ninja'd. Spent too much time typing.
Last edited by rhexis; 02-17-2012 at 07:48 AM.
- 02-17-2012, 07:47 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: parallel arrays in java
Parallel arrays beg for a small data class:
Now the name and savings are tightly coupled. Build an array of Sudent records and you're in business.Java Code:public class Student { public String name; public double savings; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Parallel arrays and classes
By Bakaouji in forum New To JavaReplies: 3Last Post: 11-24-2011, 04:23 PM -
parallel arrays
By belfast09 in forum New To JavaReplies: 6Last Post: 06-13-2011, 11:48 AM -
parallel arrays
By the beginner in forum New To JavaReplies: 9Last Post: 02-22-2011, 05:35 AM -
Parallel Arrays
By mwenchong in forum New To JavaReplies: 7Last Post: 11-17-2010, 12:20 AM -
two parallel arrays
By Adomini in forum New To JavaReplies: 12Last Post: 09-07-2010, 01:45 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks