Using max to find two numbers

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 987 Views - Last Post: 06 May 2013 - 05:08 PM Rate Topic: -----

#31 cbennett  Icon User is offline

  • New D.I.C Head

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

Re: Using max to find two numbers

Posted 06 May 2013 - 04:38 PM

View Posthhman1, 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

Was This Post Helpful? 1
  • +
  • -

#32 hhman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#33 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,104
  • 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.
Was This Post Helpful? 0
  • +
  • -

#34 cbennett  Icon User is offline

  • New D.I.C Head

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

Re: Using max to find two numbers

Posted 06 May 2013 - 04:51 PM

View Posthhman1, 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.
Was This Post Helpful? 1
  • +
  • -

#35 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,104
  • Joined: 19-March 11

Re: Using max to find two numbers

Posted 06 May 2013 - 04:54 PM

View Postcbennett, on 06 May 2013 - 06:51 PM, said:

View Posthhman1, 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.
Was This Post Helpful? 0
  • +
  • -

#36 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,104
  • 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!
Was This Post Helpful? 0
  • +
  • -

#37 cbennett  Icon User is offline

  • New D.I.C Head

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

Re: Using max to find two numbers

Posted 06 May 2013 - 05:00 PM

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

View Postcbennett, on 06 May 2013 - 06:51 PM, said:

View Posthhman1, 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.


:oops: Now I'm embarrassed, I haven't been that great at analogies
Was This Post Helpful? 0
  • +
  • -

#38 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,104
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3