Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default [SOLVED] Help with arrays, printing highest and lowest value of the array.

    Thanks for the help. :)
    Last edited by Sophiie; 11-16-2008 at 11:15 PM. Reason: d

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    is this line called:
    Java Code:
    min = sales[0];
    before the sales data has been entered into the sales array? If so, please understand that sales[0] and in fact sales[i] for any valid i will be zero. You will want to likely initialize your min and max variables just before your for loop and certainly after your sales array has been filled with the appropriate data.

  3. #3
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Can you explain a little further?

    Do I set min to 0 when I declare it a double?

    Would I have to change min = sales[0]?

    Thanks for your help.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Here, look at and run this example. It should be self-explanatory:
    Java Code:
    import java.util.Arrays;
    
    class Fubar
    {
      private static final int ARRAY_SIZE = 5;
    
      public static void main(String[] args)
      {
    
        int[] intArray = new int[ARRAY_SIZE];
        int min1 = intArray[0]; // initialize min1 *before* filling the array
        int min2;
    
        // fill the array
        for (int i = 0; i < intArray.length; i++)
        {
          intArray[i] = 5 + i;
        }
        
        min2 = intArray[0]; // initialize min2 *after* filling the array
        for (int i = 0; i < intArray.length; i++)
        {
          // check for mins here
          if (intArray[i] < min1)
          {
            min1 = intArray[i];
          }
          if (intArray[i] < min2)
          {
            min2 = intArray[i];
          }
        }
        
        System.out.println("min1 = " + min1);
        System.out.println("min2 = " + min2);
        System.out.println("intArray = " + Arrays.toString(intArray));
      }
    
    }

  5. #5
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Thumbs up

    Thanks for ur help.
    Last edited by Sophiie; 11-16-2008 at 11:16 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    To debug your code, add: System.out.println("min=" +min); inside your loop to show the value of min during every loop. Then you will see why min is always 0!

    Hint:
    When searching for min/max values, set the initial values of those variables to the end most value in the other direction. max = smallest possible value, min = largest possible value
    Last edited by Norm; 11-02-2008 at 09:43 PM.

  7. #7
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    am sorry sophiie i need the whole program ,I just started learning about arrays too and realy want to know whats wrong with ur program..

  8. #8
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Thank you Norm!!! The hint was exactly what I needed to figure this thing out!! Thanks again.

    Thanks for everyone that tried to help! :)

    Still, the second part of my help is not addressed. The min and max values are in reference to an array also. How do I make the min and max values in reference to that certain #?

    1 - 23 min --- In the print out I want to say that 1 has the lowest value
    but cant get it to work, also with max.
    2 - 32
    3 - 55 max
    4 - 31

    The array was initialized very early on and Im not sure how to make reference to it now with the min and max values.

    Thanks for the help!! :)

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    make the min and max values in reference to that certain #?
    Not sure what that means? Are you asking how to have min and max be indexes into the array with the values vs containing the values?
    One way: put the initial min(big number) and max(small number) values into the array before putting in the other values to be searched and set the min and max indexes accordingly. Then start any searchs at index = 2 vs 0.

  10. #10
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    My program is about having a sales amount entered for several employees. When running the program, it prompts for the user to enter the sales amount for each employee. Then I needed to find the max and min sales and print that with the employee that it is in reference to. If employee 1 made the highest sales, I want it to print that that employee made the highest. If employee 4 made the lowest sales, I want it to print that employee made the lowest sales also while printing the sales amount they made. I've tried but I still dont understand what to do.

    Thanks for the help!

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    You need a way of keeping each employee's data together. Create a small class to hold the data for each employee. As you read in the data, create an instance of that class with the data and store the instance in the array.

  12. #12
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Would this class be the one that goes without the public static void main(String[] args) statement?

    I am recently starting to learn about programs with more than 1 class.

    Can you explain to me how programs work with more than 1 class?

    I hope I am not asking to much. I really appreciate all the help given!! It's a life safer!

    Thanks again, I will get to my project.

  13. #13
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Smile

    Would this class be the one that goes without the public static void main(String[] args) statement?
    no ur program runs either way with or without [public static void main(String[] args) statement]

    Can you explain to me how programs work with more than 1 class?
    u can put ur program inside a method in one class and call it by the main class 4 example
    Java Code:
     
    class Account {
    	
    String name;
    double balance;
    
    void display() {
    System.out.print(name);
    System.out.print(" has $");
    System.out.print(balance);
    }
    double getInterest(double percentageRate) {
    return balance * percentageRate / 100.00;
    }
    }
    this is the main class which will use the fields & methods in class Account
    Java Code:
    class UseAccount {
    public static void main(String args[]) {
    Account myAccount = new Account();
    Account yourAccount = new Account();
    myAccount.name = "gamilah"; //here we used the name field in Account class
    
    myAccount.balance = 24.02;
    
    yourAccount.name = "sophiie";
    
    yourAccount.balance = 55.63;
    
    myAccount.display(); //we used display method in Account class
    
    System.out.print(" plus $");
    System.out.print(myAccount.getInterest(5.00));
    System.out.println(" interest ");
    yourAccount.display();
    double yourInterestRate = 7.00;
    System.out.print(" plus $");
    double yourInterestAmount =
    yourAccount.getInterest(yourInterestRate);
    System.out.print(yourInterestAmount);
    System.out.println(" interest ");
    }
    }
    i hope it helped u:)

  14. #14
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Im still figuring it all out but I really do appreciate you helping me out!! Thanks for your time and effort put into helping me!

  15. #15
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Aaah, I cant get it to work. I have been following different examples in the effort to do it myself but I dont know what Im doing wrong. Where would I start? Which names and methods would I have to use for the second class to hold the data?

    Thanks for the help!

  16. #16
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    don't mention it ,just tell me if u need a simpler example
    for now i can tell u that any class or a methos is like a car if u don't call it it still standing once u call it (drive it) it moves
    so always always put ur program in a separate class and in the main class the object that calls that class
    Java Code:
    public class Print{//this class alone is like a stopping car
    
    
    	public  void toPrint(){
    	System.out.println("I used Print class");
    }
    
    
    }
    [CODE] 
    public class UsePrint{
    	public static void main(String[]args){
    
    		Print x= new Print();//here the car starts moving
    		x.toPrint();
    
    	}
    
    
    	}
    [/CODE]

    i hope its clear now:)

  17. #17
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    I know the min max thing was an exercise for school, but in "real life" there is an easier way.

    Java Code:
      int[] bogus = { 4, 6, 1, 5, 3 };
      Arrays.sort(bogus);
      System.out.println("min: " + bogus[0]);
      System.out.println("max: " + bogus[bogus.length - 1]);

  18. #18
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Thank you very much for your help.

    Back to making references...um, I dont think Im suppose to create another class to hold the data...Is there another way to do it?

    Im refering to the part where I have to print the sales amount and the employee it is in reference to.

    THANKS!!

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    If you have more than one value to save for each employee, you could have parallel arrays to hold the data. The data for each employee would be found at the same index in each array.

  20. #20
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Thanks again for the help!
    Last edited by Sophiie; 11-16-2008 at 11:17 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 03:40 PM
  2. Printing Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:41 PM
  3. The highest number
    By I-Shine in forum Java Applets
    Replies: 3
    Last Post: 02-13-2008, 05:05 AM
  4. [Java Printing]
    By remnahush in forum Advanced Java
    Replies: 1
    Last Post: 11-08-2007, 03:28 PM

Posting Permissions

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