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

• (2 Pages)
• 1
• 2

## 23 Replies - 12923 Views - Last Post: 27 September 2010 - 08:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=192130&amp;s=d7f3579a45bd3fcbe350d3c2243b6385&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sampsoni

Reputation: 0
• 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

• Games, Graphs, and Auctions

Reputation: 11302
• Posts: 42,581
• 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?

### #3 g00se

• D.I.C Lover

Reputation: 2998
• Posts: 12,837
• 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

### #4 sampsoni

Reputation: 0
• 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--?

### #5 Dogstopper

Reputation: 2929
• Posts: 11,201
• 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.

### #6 sampsoni

Reputation: 0
• 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

### #7 g00se

• D.I.C Lover

Reputation: 2998
• Posts: 12,837
• 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

### #8 sampsoni

Reputation: 0
• 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.

### #9 anonymouscodder

Reputation: 126
• 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++;
}
```

### #10 g00se

• D.I.C Lover

Reputation: 2998
• Posts: 12,837
• 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]);
}
}

```

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11302
• Posts: 42,581
• 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
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

```

### #12 sampsoni

Reputation: 0
• 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.

### #13 g00se

• D.I.C Lover

Reputation: 2998
• Posts: 12,837
• 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

Iterate the int[] for 'max' and increment a count on finding it

### #14 sampsoni

Reputation: 0
• 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?

### #15 pbl

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

Reputation: 8362
• Posts: 31,955
• 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

g00se, 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