Results 1 to 7 of 7
  1. #1
    rocky86 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default finding max value

    hi guys! need help to find max value!! for a list of user input! but unable to use ARRAY for it!!

    currently my code is using alot of IF CONDITION!
    Java Code:
               Scanner sc=new Scanner(System.in);
    
                          int input=0,count=0, data1=0, data2=0, data3=0, data4=0, data5=0;
                          double max=0;
    
    
                          System.out.print("Enter number of input lines: ");
    		      input = sc.nextInt();
    
                          for(count=1; count<=input; count++)
                          {
    
                          System.out.println("Enter input line " +count +":");
    		      data1 = sc.nextInt();
                          data1 = Integer.parseInt( in.readLine());
    
                         
                    if(data1>=data2)
                     {
                          if(data1>=data3)
                          {
                            max=data1;
                          }
                          else{
                              max=data3;
                         }
                     }
                     else if(data2>=data3)
                     {
                          max=data2;
                     }
                     else
                     {
                          max=data3;
    
                     }
                       System.out.println("Maximum: "+max);
    
                          }//for loop
    i noe there is a way call max = Math.max(max, current); to do it! however i do not know how to implement it!! can someone show me how to do it!?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Mhmm, for what do you need data2 to data5? :confused:
    You must only check if data1 greater than your current maximum, if it is, set the new maximum = data1 ...
    Java Code:
    			if (data1 > max)
    				max = data1;
    or I've misunderstood you?

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

    Default

    Hey Rocky and welcome to the Java-Forums.org!

    One small request: you might want to fix your code indentation a bit as it is very hard to read and follow your code as currently presented. Remember that the easier it is to read your code the greater the chances that someone smart (like eRaaaa) will take the time to read it and help you.

    Best of luck, and again -- welcome!

  4. #4
    rocky86 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    hi tis is what i need to do![IMG][/IMG]

    i wana ask is it possible to read in just 1 user input and find the max of it?
    example:

    Java Code:
    int a=0;
    System.out.println("Enter input line " );
    a = sc.nextInt();
    Input: 1 2 3 4 5
    Max: 5

    as u can see from the output!, is just show the max for this line! I noe array can do it, however I am not allowed to use any array!!

    and I also noe I can use Math.max() function! but this function required at least 2 input for it to compare, whereby I only have 1 input!! any suggestion??

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    392
    Rep Power
    5

    Default

    Hello rocky,

    Assign the first int to a variable then perform the check as each additional int is read in, updating the value where necessary. Simple.

    Regards.

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

    Default

    "noe"?

    Please edit your post to use standard English. Many volunteers here don't speak English as a primary language or even a second language, and let's not make it difficult for them. Also, since programming is an exercise in precision, you'll want to be as precise as possible when communicating your problem.


    Myself, I'd use a for loop to repeat the request for a line of numbers x-times. I'd use a Scanner to scan in the line of numbers, and then perhaps use a second Scanner inside of the for loop to extract the numbers from the String obtained. A while loop would be good for this,...
    Java Code:
    // inside of the for loop...
    // line holds the String of numbers scanned in by nextLine()
    String line = sc.nextLine(); // the main Scanner gets the user input line
    
    // create a second new Scanner object to scan this line for ints:
    Scanner lineScanner = new Scanner(line); // create a new scanner here
    while (lineScanner.hasNextInt()) {
      int nextInt = lineScanner.nextInt();
      //... do what you need to do with it to check for max
    }
    lineScanner.close();
    // still inside of for loop...

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by rocky86 View Post
    hi tis is what i need to do![IMG][/IMG]

    i wana ask is it possible to read in just 1 user input and find the max of it?
    example:

    Java Code:
    int a=0;
    System.out.println("Enter input line " );
    a = sc.nextInt();
    Input: 1 2 3 4 5
    Max: 5

    as u can see from the output!, is just show the max for this line! I noe array can do it, however I am not allowed to use any array!!

    and I also noe I can use Math.max() function! but this function required at least 2 input for it to compare, whereby I only have 1 input!! any suggestion??
    greatest post ever :D

Similar Threads

  1. Ip address finding
    By anilkumar_vist in forum New To Java
    Replies: 6
    Last Post: 04-10-2010, 03:06 AM
  2. Finding web links
    By ishdevil in forum New To Java
    Replies: 2
    Last Post: 02-11-2010, 11:42 AM
  3. Finding the largest value.
    By Blacky777 in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 08:52 PM
  4. Finding where to draw
    By flok in forum AWT / Swing
    Replies: 4
    Last Post: 11-24-2009, 05:55 PM
  5. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 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
  •