# Using max to find two numbers

• (3 Pages)
• 1
• 2
• 3

## 37 Replies - 1513 Views - Last Post: 06 May 2013 - 05:08 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=320212&amp;s=7e060d47c218256b7f13b5e8f7e60a70&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #31 cbennett

Reputation: 6
• Posts: 31
• Joined: 13-February 12

## Re: Using max to find two numbers

Posted 06 May 2013 - 04:38 PM

hhman1, on 06 May 2013 - 03:55 PM, said:

So looking at my current code what is wrong?
```import java.util.Scanner;

public class Unit4PT1 {
public static void main (String [] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter numbers: ");

int max = 0;
int maxcount = 0;
int nextmax;
int nextmaxcount;
int number;
while((number = in.nextInt()) != 0){
if(number > max){

maxcount = 1;
} else if (number == max){
maxcount++;
}else if (number > max){
number = nextmax;
nextmaxcount = 1;
}else if (number == nextmax){
nextmaxcount++;
}
}
System.out.println("The largest number is " + max);
System.out.println("The occurence count of the largest number is " + maxcount);
System.out.println("The second largest number is " + nextmax);
System.out.println("The occurence count of the second largest number is " + nextmaxcount);
}
}

```

Lines 11-13 have not been initialized and can't be used within the loop, you should ask for the number for the number variable at line 14 before you use it for checking if it equals zero.

```Scanner input = new Scanner(System.in);
System.out.println("Hey there lady, gimme yo numba");
int number = input.nextInt();

while(number !=0)
{
....

```

Other than that your logic is kinda wonky, try and write out what should happen on paper cause things are getting kinda weird inside the loop. I would also change the while to an if statement, or you will never get out of the loop unless you add a return statement or exit command of sort.

I moved some things around to put you in the right direction, but it isn't done yet. I will not help anymore as this is probably too much.

```	public static void main (String [] args)
{
int max = 0;
int maxcount = 0;
int nextmax=0;
int nextmaxcount=0;
int number=0;

Scanner input = new Scanner(System.in);
System.out.print("Enter numbers: ");
number = input.nextInt();

if(number != 0){
if(number > max){

maxcount = 1;
}
else if (number == max){
maxcount++;
}
else if (number > max){
number = nextmax;
nextmaxcount = 1;
}
else if (number == nextmax){
nextmaxcount++;
}
}
System.out.println("The largest number is " + max);
System.out.println("The occurence count of the largest number is " + maxcount);
System.out.println("The second largest number is " + nextmax);
System.out.println("The occurence count of the second largest number is " + nextmaxcount);
}
}

```

All I did was make it compile and run

This post has been edited by cbennett: 06 May 2013 - 04:49 PM

### #32 hhman1

Reputation: 0
• Posts: 23
• Joined: 02-May 13

## Re: Using max to find two numbers

Posted 06 May 2013 - 04:49 PM

How do I go about initializing?

### #33 jon.kiparsky

• Pancakes!

Reputation: 9539
• Posts: 16,490
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 06 May 2013 - 04:50 PM

I see two errors that jump right out at me. I can tell you what the symptoms are - next time, you give me the symptoms and I'll point you to the code.

First symptom: you're never registering a max value. When you fix that, you'll find that you're also not registering a nextmax value. You should be able to find those.

### #34 cbennett

Reputation: 6
• Posts: 31
• Joined: 13-February 12

## Re: Using max to find two numbers

Posted 06 May 2013 - 04:51 PM

hhman1, on 06 May 2013 - 04:49 PM, said:

How do I go about initializing?

Give it a value, usually 0 unless it's a constant, but that's beyond the scope of this question. You have to initialize it or Java doesn't think it exists. See, a variable is a piece of data, and if you don't put anything in that variable it doesn't exist.

### #35 jon.kiparsky

• Pancakes!

Reputation: 9539
• Posts: 16,490
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 06 May 2013 - 04:54 PM

cbennett, on 06 May 2013 - 06:51 PM, said:

hhman1, on 06 May 2013 - 04:49 PM, said:

How do I go about initializing?

Give it a value, usually 0 unless it's a constant, but that's beyond the scope of this question. You have to initialize it or Java doesn't think it exists. See, a variable is a piece of data, and if you don't put anything in that variable it doesn't exist.

Better to say that java doesn't know what value to give it. Some languages will assume default values for your variables, and Java will do this in some cases, but for the most part it's best to set the initial values for yourself. This way you know what the values are, and you don't get confused.

### #36 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11798
• Posts: 44,322
• Joined: 27-December 08

## Re: Using max to find two numbers

Posted 06 May 2013 - 04:56 PM

Also, properly indent your code. It makes it a lot easier to read and debug!

### #37 cbennett

Reputation: 6
• Posts: 31
• Joined: 13-February 12

## Re: Using max to find two numbers

Posted 06 May 2013 - 05:00 PM

jon.kiparsky, on 06 May 2013 - 04:54 PM, said:

cbennett, on 06 May 2013 - 06:51 PM, said:

hhman1, on 06 May 2013 - 04:49 PM, said:

How do I go about initializing?

Give it a value, usually 0 unless it's a constant, but that's beyond the scope of this question. You have to initialize it or Java doesn't think it exists. See, a variable is a piece of data, and if you don't put anything in that variable it doesn't exist.

Better to say that java doesn't know what value to give it. Some languages will assume default values for your variables, and Java will do this in some cases, but for the most part it's best to set the initial values for yourself. This way you know what the values are, and you don't get confused.

Now I'm embarrassed, I haven't been that great at analogies

### #38 jon.kiparsky

• Pancakes!

Reputation: 9539
• Posts: 16,490
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 06 May 2013 - 05:08 PM

Never fear, you're doing grand. If you say anything totally wrong, we'll let you know.