I am trying to read in a group of numbers from a users input through a console after the sentinel value of 0 is entered then I need to output the largest integer put in and the next largest. I can get the largest but not the next largest. What should I do? See the code for the largest integer below. All numbers entered should be positive.

/*
* File: LargestInt.java
* -----------------------
* This program reads integers one per line until the
* user enters a special sentinel value to signal the
* end of the input. At that point, the program
* prints the largest integer entered.
*/

import acm.program.*;

public class LargestInt extends ConsoleProgram {

public void run() {
println ("This program prints the largest integer entered.");
println ("Enter values, one per line, using "+SENTINEL);
println ("to signal the end of the list.");
int largest = 1;
int number = readInt(" ? ");
while (number != SENTINEL) {
number = readInt(" ? ");
if (number>largest){largest=number;}

}
println("The largest number was: "+largest);

}

/* Specifies the value of the sentinel */
private static final int SENTINEL = 0;

}

Thanks in advance,
Michael

next largest = largest before changing the largest

next largest = largest before changing the largest

I guess I don't quite understand what you are saying. Here is what I got. I can't get it to tell me the next largest integer. It only copies the value from the largest integer.

/*
* File: NextLargestInt.java
* -----------------------
* This program reads integers one per line until the
* user enters a special sentinel value to signal the
* end of the input. At that point, the program
* prints the largest and next largest integer entered.
*/

import acm.program.*;

public class NextLargestInt extends ConsoleProgram {

public void run() {
println ("This program prints the largest integer entered.");
println ("Enter values, one per line, using "+SENTINEL);
int largest = 1;
int nextlargest = 1;
int number = readInt(" ? ");
while (number != SENTINEL) {
number = readInt(" ? ");
nextlargest= largest;
if (number>largest){largest=number;}


}
println("The largest number was: "+largest);
println("The next largest number was: "+nextlargest);

}

/* Specifies the value of the sentinel */
private static final int SENTINEL = 0;

}

the next largest = largest line should be in the if statement before the largest=number line


oh, i can't believe i forgot to say this in my first post, please use code tags

I guess I don't quite understand what you are saying. Here is what I got. I can't get it to tell me the next largest integer. It only copies the value from the largest integer.

int largest = 1;
int nextlargest = 1;
while (number != SENTINEL) 
{
    number = readInt(" ? ");
    nextlargest= largest;
    if (number>largest)
    {
        largest=number;
    }
}

Lines 1 and 2: If you can guarantee that you'll have at least two numbers and that they'll both be greater than or equal to 1, you can use this initialization. Otherwise it is problematic.

line 6 - You are assigning largest to equal nextlargest too early. What if largest equals 6 and nextlargest equals 5 and the user types in 4? You then are assigning 6 to be nextlargest and losing the 5 forever. Don't change anything till you do a comparison. When largest and nextlargest are set and the user enters a new number, there are three possibilities:

1. The new number is larger than largest .
2. The new number is larger than nextlargest but not larger than largest .
3. The new number is not greater than nextlargest .

Before changing anything, you need to decide which of these three categories occurs and change or not change largest and nextlargest accordingly.

wow VernonDozer and I must have pushed submit at almost the same time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.