Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: parallel arrays in java

  1. #1
    javanerd123 is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: parallel arrays in java

    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++)
            {
    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.

  4. #4
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default 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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: parallel arrays in java

    Parallel arrays beg for a small data class:

    Java Code:
    public class Student {
       public String name;
       public double savings;
    }
    Now the name and savings are tightly coupled. Build an array of Sudent records and you're in business.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Parallel arrays and classes
    By Bakaouji in forum New To Java
    Replies: 3
    Last Post: 11-24-2011, 04:23 PM
  2. parallel arrays
    By belfast09 in forum New To Java
    Replies: 6
    Last Post: 06-13-2011, 11:48 AM
  3. parallel arrays
    By the beginner in forum New To Java
    Replies: 9
    Last Post: 02-22-2011, 05:35 AM
  4. Parallel Arrays
    By mwenchong in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 12:20 AM
  5. two parallel arrays
    By Adomini in forum New To Java
    Replies: 12
    Last Post: 09-07-2010, 01:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •