Display the largest number in an integer input using a loop and output

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 10721 Views - Last Post: 27 September 2010 - 08:46 PM Rate Topic: -----

#1 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Display the largest number in an integer input using a loop and output

Posted 26 September 2010 - 11:59 AM

I'm having trouble with this assignment. It's included in the attachment.

Here's my code for that one assignment:

import java.util.*;
import java.text.*;

public class OccuranceNumbers
{
    public static void main (String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter numbers: ");
        
        int num1 = input.nextInt();
        int max = num1;
        int count = 1;
        
        while (count < 10){
            if (num1 > max){
                num1 = max;        
            }
            else if (num1 == max){
                count++;    
            }
        }
        
        // Output largest.
        System.out.println("The largest number is " + max);
        System.out.println("the occurrence count of the largest number is " + count);
    }
}



What I'm trying to do is to write a program that reads multiple integers, finds the largest of them, and counts the occurrences of the largest integer. This is not using Strings or arrays, this is loops. Can you please help me?

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Display the largest number in an integer input using a loop and output

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,113
  • Joined: 27-December 08

Re: Display the largest number in an integer input using a loop and output

Posted 26 September 2010 - 12:18 PM

Hint- once you find a new max, you have to reset the count to 1. Other than that, what problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,448
  • Joined: 20-September 08

Re: Display the largest number in an integer input using a loop and output

Posted 26 September 2010 - 12:20 PM

You need to read the input in a loop. Currently you're only reading one number
Was This Post Helpful? 0
  • +
  • -

#4 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Re: Display the largest number in an integer input using a loop and output

Posted 26 September 2010 - 12:23 PM

I don't know how to reset the count to 1. Can you please show me what code, like count++ or count--?
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Display the largest number in an integer input using a loop and output

Posted 26 September 2010 - 12:43 PM

count = 1;



Yep...that simple.
Was This Post Helpful? 0
  • +
  • -

#6 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Re: Display the largest number in an integer input using a loop and output

Posted 26 September 2010 - 12:59 PM

I did, but still, the output is not right:

Enter numbers:
3 5 2 5 5 5 0
The largest number is 3
the occurrence count of the largest number is 10

When it's supposed to be:

Enter numbers:
3 5 2 5 5 5 0
The largest number is 5
the occurrence count of the largest number is 4


Can anyone please help me?
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,448
  • Joined: 20-September 08

Re: Display the largest number in an integer input using a loop and output

Posted 26 September 2010 - 01:00 PM

You need to call nextInt in a loop as i mentioned

This post has been edited by g00se: 26 September 2010 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Re: Display the largest number in an integer input using a loop and output

Posted 26 September 2010 - 02:00 PM

I did, here's my code:

import java.util.*;
import java.text.*;

public class OccuranceNumbers
{
    public static void main (String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter numbers: ");
        
        int count = 1;
        
        while (count < 10){
            int num1 = input.nextInt();
            int max = num1;
            if (num1 > max){
                num1 = max;        
            }
            else if (num1 % 2 == max){
                count++;    
            }
            System.out.println("The largest number is " + max);
            System.out.println("the occurrence count of the largest number is " + count);
        }
    }
}


However, the output is now this nonsense:

Enter numbers:
3 5 2 5 5 5 0
The largest number is 3
the occurrence count of the largest number is 1
The largest number is 5
the occurrence count of the largest number is 1
The largest number is 2
the occurrence count of the largest number is 1
The largest number is 5
the occurrence count of the largest number is 1
The largest number is 5
the occurrence count of the largest number is 1
The largest number is 5
the occurrence count of the largest number is 1
The largest number is 0
the occurrence count of the largest number is 2


Help me out here?

My problem is that I can't visualize this nextInt() being put inside a loop. My brain doesn't seriously work that way.
Was This Post Helpful? 0
  • +
  • -

#9 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Display the largest number in an integer input using a loop and output

Posted 26 September 2010 - 03:13 PM

int num1 = input.nextInt();
int max = num1;
if (num1 > max){
    num1 = max;      
}


After setting the user input to num1, you also set the same value to max. That means num1 and max will always be equal when reaching if(num1 > max).


And I don't get it what you doing with:
else if (num1 % 2 == max){
    count++;
}

Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,448
  • Joined: 20-September 08

Re: Display the largest number in an integer input using a loop and output

Posted 27 September 2010 - 06:37 AM

You can't determine the frequency of occurance without reading all the numbers first. This is a partial implementation
import java.text.*;

import java.util.*;


public class OccuranceNumbers {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter numbers: ");

        String[] sNums = null;

        if (input.hasNextLine()) {
            sNums = input.nextLine().split(" ");
        }

        int[] nums = new int[sNums.length];

        for (int i = 0; i < nums.length; i++) {
            nums[i] = Integer.valueOf(sNums[i]);
        }

        java.util.Arrays.sort(nums);
        System.out.println("The largest number is " + nums[nums.length - 1]);
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,113
  • Joined: 27-December 08

Re: Display the largest number in an integer input using a loop and output

Posted 27 September 2010 - 09:28 AM

Actually, it's quite possible for only the largest numbers. Basically, once you find a new max, all the numbers before it can be discarded b/c none of them are the max. But if you find something == max, increment a counter.
max, count <-- 1
max <-- read in number
while there are more ints to read in
    if read in int > max
        max = that int
        count <-- 1
    else if the int read in == max
        count <-- count+1
end while


Was This Post Helpful? 0
  • +
  • -

#12 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Re: Display the largest number in an integer input using a loop and output

Posted 27 September 2010 - 11:49 AM

I used your code, g00se, but I'm still want to figure out how to output all the occurrences. Can you please help me.
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,448
  • Joined: 20-September 08

Re: Display the largest number in an integer input using a loop and output

Posted 27 September 2010 - 12:01 PM

Quote

Can you please help me.


Iterate the int[] for 'max' and increment a count on finding it
Was This Post Helpful? 0
  • +
  • -

#14 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Re: Display the largest number in an integer input using a loop and output

Posted 27 September 2010 - 01:46 PM

I still can't see the result visually, like where am I supposed to put it without getting an error from my IDE that it failed to compile?

Here's my code:

import java.text.*;

import java.util.*;


public class inputtest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter numbers: ");

        String[] sNums = null;

        if (input.hasNextLine()) {
            sNums = input.nextLine().split(" ");
        }

        int[] nums = new int[sNums.length];

        for (int i = 0; i < nums.length; i++) {
            nums[i] = Integer.valueOf(sNums[i]);
        }
        
        int count = 1;
        
        while (count < nums[count]){
            int[] max = nums;
            if (nums != max){
                count = 1;
            }
            else if (nums == max){
                count++;    
            }
        }

        
        java.util.Arrays.sort(nums);
        System.out.println("The largest number is " + nums[nums.length - 1]);
        System.out.println("The occurrence of the largest number is " + count);
    }
}



I need massive help involving System.out.println("The occurrence of the largest number is " + count); It only prints out a result of 2 according to this code. Can you please give me what I need to put in there in code?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Display the largest number in an integer input using a loop and output

Posted 27 September 2010 - 04:23 PM

View Postg00se, on 27 September 2010 - 07:37 AM, said:

You can't determine the frequency of occurance without reading all the numbers first. This is a partial implementation
import java.text.*;

import java.util.*;


public class OccuranceNumbers {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter numbers: ");

        String[] sNums = null;

        if (input.hasNextLine()) {
            sNums = input.nextLine().split(" ");
        }

        int[] nums = new int[sNums.length];

        for (int i = 0; i < nums.length; i++) {
            nums[i] = Integer.valueOf(sNums[i]);
        }

        java.util.Arrays.sort(nums);
        System.out.println("The largest number is " + nums[nums.length - 1]);
    }
}


g00se while you are at it why not using a Map to count the occurences ?
You are providing a Cadillac for the OP to deliver a pizza :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2