aspose file tools*
The moose likes Beginning Java and the fly likes Problem with code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with code" Watch "Problem with code" New topic
Author

Problem with code

Michael Grant
Greenhorn

Joined: Jul 31, 2008
Posts: 6
I am trying to modify this code to println the largest integer and the next largest integer. Right now it is set to show the largest integer after the SENTINEL value of 0 is entered. I am using the ACM package from http://jtf.acm.org/

Here is the code:

/*
* 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;

}
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
First let's clarify your requirements.

What should happen with the following inputs:

input(1, 2);//Two positive numbers entered
input(1);//Less than two numbers entered
input(2, 2);//The two highest numbers entered are the same
input(-2, -1);//All numbers entered are below 0


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
To modify the code, you need to add another variable: secondLargest.
Everytime you are going to change largest, copy its value to secondLargest first.
Michael Grant
Greenhorn

Joined: Jul 31, 2008
Posts: 6
Norm,

How do I copy it to the new variable? I am new at Java programming.

Michael Grant
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You have managed to get the value into "largest"; you can get the value of "largest" into "nextLargest" similarly. Just beware; if you get the statements in the wrong order it won't work at all.

Please look at these FAQ: don't simply call a thread "problem with code" and please indent your code with the "CODE" button; it makes it much easier to read.
Michael Grant
Greenhorn

Joined: Jul 31, 2008
Posts: 6
I tried the nextlargest everywhich way I can think I can't figure how to get it to show the next largest number. Do you use the less than operator? I tried and I get errors when compiling.

/*
* 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;

}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Originally posted by myself:
Just beware; if you get the statements in the wrong order it won't work at all.


You have got the statements right, but in the wrong order. You only want to update nextLargest if the input is larger than the old value of nextLargest. So the test has to precede assigning nextLargest.
You will want a different set of assignments if your input is larger than largest.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with code
 
Similar Threads
Simple Java problem
The Art & Science of Java Chapter 4 Exercise 12 FindLargest.java
The Art & Science of Java Chapter 4 Exercise 5 AverageList.java
Art and Science of Java Chapter 4 Exercise 7
Calculating the average of an unknown amount of numbers