• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Problem with code

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

}
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4372
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm,

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

Michael Grant
 
Marshal
Posts: 73267
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 73267
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Not so fast naughty spawn! I want you to know about
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic