• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with code

 
Michael Grant
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm,

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

Michael Grant
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic