Can anyone help?  trying to solve logic error in program

The program below does not seem to work as expected. it always returns the highest value, but is inconsistent with the second highest value.
any help appreciated.
// 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 2 numbers entered so far.
import acm.program.*;
public class Largest2IntegersInList extends ConsoleProgram {
     public void run() {
          println("This program displays the largest 2 numbers in a list of integers.");
          println("Enter values, one per line, using " + SENTINEL);
          println("to signal the end of the list.");
          // initialize the input value
          int value = 0;
          // initialize the largest value
          int largestInteger = 0;
          // initialize the second largest value
          int secondLargestInteger = 0;
          while (true) {
               value = readInt(" ? ");
               //check to see if value is SENTINEL
               if (value == SENTINEL) break;
               //if the value entered is greater than the value
               //of the largest integer so far, assign the new
               //value to the largestInteger
               if (value > largestInteger){
                    largestInteger = value;                    
               else if((largestInteger > value ) && (value > secondLargestInteger)){
                    secondLargestInteger = value;
          println("The largest number is " + largestInteger + ".");
          println("The second largest number is " + secondLargestInteger + ".");
     /* Specifies the value of the Sentinel */
     private static final int SENTINEL = 0;
}

I get this... but I can't test it... I keep getting "Cannot find main program" from the acm framework.
I don't think there's anything "wrong" with my code, except that maybe acm doesn't support packages, which would be a big stretch... Beats me why these pork chops (the acm authors, not the OP) think that taking something perfectly simple and making it complicated is an improvement on the "core" language... think about it... they've taken a simple main method, replaced it with something that involves some funkyassed command line parsing which evidently doesn't work (reliably), and complicates even the most basic program (like HelloWorld.java) by making it inherently multi-threaded.
Acm gets my vote for the "WTF of the Week" award.
*<EDIT>*
{color:#000080}{size:20px}*This doesn't work either!*{size}{color}
What's more, I can't think of a way to do this properly "in the loop"... I'd save the values to a List; sort the list descending; and then print the first two values.
By properly I mean cater for:
1. out of order entry.
2. all in-order entry.
3. the same "max value" occurs twice.
*</EDIT>*
package forums;
* 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 2 numbers entered so far.
import acm.program.*;
public class LargestTwoIntegers extends ConsoleProgram
  private static final long serialVersionUID = 1L;
  private static final int SENTINEL = 0;
  public void run() {
    println("This program displays the largest two numbers entered.");
    println("Enter integer values, one per line.");
    println("Enter the value "+SENTINEL+" to end.");
    int biggest = Integer.MIN_VALUE;
    int secondBiggest = Integer.MIN_VALUE;
    int count = 0;
    while (true) {
      int value = readInt(" ? ");
      if (value == SENTINEL) {
        break;
      count++;
      if (value > biggest){
        secondBiggest = biggest;
        biggest = value;
    if (count<2) {
      println("At least two values are required.");
    } else {
      println("The largest number is " + biggest + ".");
      println("The second largest number is " + secondBiggest + ".");
}Cheers. Keith.
Edited by: corlettk on 23/08/2009 12:25

Similar Messages

  • I have the iPhone 4 recently became weak Wi-Fi where I can not use the Internet only when sitting Bejjani router, Can anyone help me in solving this problem please iPhone 4, iOS 7.0.3

    I have the iPhone 4 recently became weak Wi-Fi where I can not use the Internet only when sitting Bejjani router, Can anyone help me in solving this problem please
    iPhone 4, iOS 7.0.3

    There's a whole lot to read in your post, and frankly I have not read it all.
    Having said that, this troubleshooting guide should help:
    http://support.apple.com/kb/TS1538
    In particular, pay attention to the mobile device support sections near the bottom, assuming you have already done the items above it.

  • Can anyone help me wid d codes for  Generated Program for gate pass

    can anyone help me wid d codes for  Generated Program for gate pass in MM
    Message was edited by:
            Ronei Shedi

    Hi
    There is no std business process in SAP for Gate pass for the Material entry
    before GR stock entry into the stores.
    You have to write a Z program based on the details of Purchase order tables EKKO and EKPO.
    This will mainly check whether the correct PO qty was delivered or not with proper quality.
    So use the PO tables EKKO and EKPO and fetch the data and use it
    Since this is client specific there is no feneralised program for it.
    Reward points if useful
    Regards
    Anji

  • Hi  can anyone help out there in Logic Land

    When I launch logic 7.1.1 on my G5 dual 2 gig System with 3.5gig of ram, running OS 10.4.9 -ok the prelims out of the way now down to the problem.
    When I launch Logic I get this error message
    Error code-1 was returned by CoreAudio driver
    Can anyone out there in Logic land tell me what this means.

    Your Core Audio driver is not recognized or not found.
    How did you upgrade to 10.4.9?
    There have been reports here, that upgrading to 10.4.9 via Software Update created problems, mainly for people who run a MP or MBP.
    Try re-installing 10.4.9 from the combo updater available at the Apple site, or try to revert to 10.4.8, which runs nice and stable here with LP 7.1.1.
    Maybe from 10.4.9 on we have to use LP 7.2.x..., hum?

  • I can't install on uninstall iTunes on my laptop; running Windows XP.  I've tried all the steps in Apple Support as well as Windows support.  Can anyone help?  I fet the error "The feature you are trying to use is on a network resource that is unavailable

    I can't install or uninstall iTunes on my laptop.  I've followed all the steps on Apple Support as well as Microsoft support.  I get the error "The feature you are trying to use is on a network resource that is unavailable.  Pick an alternate path to a folder containing the installation package 'iTunes.msi".  This has been going on for a couple of weeks now and I'm very frustrated.  From what I can see, I'm not the only one with this problem, but I have yet to see anyone post a solution that works.  Microsoft wants to charge me $99 to troubleshoot it for me and Apple will charge $20.  I think both are ridiculous as this is obviously not "operator error".

    (1) Download the Windows Installer CleanUp utility installer file (msicuu2.exe) from the following Major Geeks page (use one of the links under the "DOWNLOAD LOCATIONS" thingy on the Major Geeks page):
    http://majorgeeks.com/download.php?det=4459
    (2) Doubleclick the msicuu2.exe file and follow the prompts to install the Windows Installer CleanUp utility. (If you're on a Windows Vista or Windows 7 system and you get a Code 800A0046 error message when doubleclicking the msicuu2.exe file, try instead right-clicking on the msicuu2.exe file and selecting "Run as administrator".)
    (3) In your Start menu click All Programs and then click Windows Install Clean Up. The Windows Installer CleanUp utility window appears, listing software that is currently installed on your computer.
    (4) In the list of programs that appears in CleanUp, select any iTunes entries and click "Remove", as per the following screenshot:
    (5) Quit out of CleanUp, restart the PC and try another iTunes install. Does it go through properly this time?

  • TS3694 can anyone help, I have a 1015 error message, trying to restore my old 3G

    error message 1015, trying torestore my old 3G phone

    I Ggogled the error code. There are numerous links here:
    https://www.google.com/search?q=iphone+error+1015&ie=UTF-8&oe=UTF-8&hl=en&client =safari

  • Can anyone help? I got this error: "TOSBTMNG.ee ha...

    HI,
    Win XP Pro 2002 SP3
    Nokia Suite 7.0.7.0
    Connecting to a E65 via bluetooth
    All was working fine up until a little while ago, then
    "tosbtmng.exe has encountered a problem".
    And the phone will no longer connect via bluetooth. Hmmph!
    I get two errors in the application log:
    1000: Faulting application tosbtmng.exe, version 5.0.0.35, faulting module lcwizard.dll, version 5.0.0.3, fault address 0x00006248.
    And:
    1000: Fault bucket 422170835.
    Now I didnt know that I have any buckets in me Sony Vaio VGN G11 XN and all the while it was syncronising with good old outlook I didnt really care.
    Now though....
    Finally, here is a dump from sysinfo.
    PC Connectivity Solution:
    Version 8.22.2.0
    [Nokia Connectivity Cable Driver:]
    Version 7.0.2.0
    Operating system:
    Microsoft Windows XP 32-bit Edition, Service Pack 3
    Language: English
    Language for non-Unicode programs: English
    Detected Internet browsers:
    - Microsoft Internet Explorer 6.0 (Default browser)
    - Mozilla Firefox
    Detected Bluetooth stacks:
    - Microsoft (Version: 5.1.2600, Build: 5512)
    - Toshiba (Version 5.00.6y25.0)
    Can any of you bright, witty and attractive peps help me out here?
    Kind Regards
    Martin
    Solved!
    Go to Solution.

    the rror is indicative of an error with the toshiba bluetooth manager. try updating your blutooth driver as the version supported by nokia pc suite is Toshiba Bluetooth stack, driver version 4.0, 5.0 and 5.10.12, for windows xp.
    although your current driver fits into the supported drivers, the SP3 has braught many changes to the OS, that i thin updating the driver might help solve the problem.
    GOOD LUCK
    Fear not those who argue but those who dodge

  • Can anyone help with Photoshop CC download error (49)?

    I'm not sure what to do with trying to get the update installed so that I can start the trial version.  I have turned off McAfee already.  Any suggestions?

    Jbsnaps please see Error downloading Creative Cloud applications - http://helpx.adobe.com/creative-cloud/kb/error-downloading-cc-apps.html for information on how to resolve Error 49.

  • Can anyone help me with a photoshop error

    I try to open a picture in photoshop to edit it. I am really new to photoshop. I have previously used photoshop elements 6 and 7 and I am trying to take a class in photoshop and I am on the first lesson.  I did okay the first night.  Then I looked at the Adobe website and there was a patch for photoshop. I downloaded the patch. Now when I try to open a picture to edit it I get a photoshop.exe.-application error message.  The instruction at "0x0bb82c5b" referenced memory at "0x00000028". The memory could not be read.  Click on Ok to terminate the program.  Click on cancel to debug the program.  No matter what I click on I get kicked out of photoshop.  I am currently on hold for technical support but it sounds like I have to pay for it.  I have edited exactly 2 pictures in a program that I payed 600.00 plus dollars for.  This doesn't really right.  Help please.

    You should really post this in the Photoshop Windows forum. This is the scripting forum.
    -X

  • Can anyone help me to solve this code , its in a swing , jseparator...

    hey
    can any one solve this code ??
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class File1 extends JFrame
              public static void showgui()
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   JLabel emptyLabel = new JLabel("");
         emptyLabel.setPreferredSize(new Dimension(640, 480));
         frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
                   JButton b1= new JButton("JENIS");
                   add(b1);
                   b1.setBounds(350,220,120,20);
                   JSeparator jE =new JSeparator(JSeparator.HORIZONTAL);
                   jE.setBounds(350, 205, 120, 20);
                   add(jE);
                   add(new JSeparator(JSeparator.HORIZONTAL));
                   JButton b2= new JButton("MODI");
                   add(b2);
                   b2.setBounds(100,210,100,100);
                   frame.pack();
         frame.setVisible(true);
    public static void main(String[] args)
         javax.swing.SwingUtilities.invokeLater(new Runnable()
              public void run()
                   showgui();
    actually i want to use jseparator , so if this cant be solved , send me another code that uses jseparator and gives the vertical / horizontal line between two buttons??
    Its urgent ,
    waiting for reply............

    Hi,
    Is this ok?
    public class NewJFrameSep extends javax.swing.JFrame {
            public NewJFrameSep() {
            initComponents();
        private void initComponents() {
            jSeparator1 = new javax.swing.JSeparator();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            getContentPane().setLayout(null);
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            getContentPane().add(jSeparator1);
            jSeparator1.setBounds(110, 120, 170, 100);
            jButton1.setText("jButton1");
            getContentPane().add(jButton1);
            jButton1.setBounds(160, 80, 71, 23);
            jButton2.setText("jButton2");
            getContentPane().add(jButton2);
            jButton2.setBounds(160, 140, 71, 23);
            pack();
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrameSep().setVisible(true);
       private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JSeparator jSeparator1;
    }regards
    dina

  • Can anyone help me with this 1120 error? So frustrated...

    I have those two objects homeS and galleryS and two corresponding functions to change the size of their layer masks. The structure of the two are
    exactly the same except for their names and positions. However, only one of them functions.
    The instance's name is the same in the code and there is only one key frame in the layer where movieClip "galleryMsk" is.
    But Flash always through this 1120 error saying "undefined property galleryMsk"...
    I don't know why it happens since part I is functioning perfectly.
    The only thing is that object "homeMsk" and object"galleryMsk" are in the same layer mask, does that cause the trouble?
    I am so frustrated and I cannot focus on anything else in the whole day...couldn't even sit down and watch tv happily...
    Here are the two parts of the code. Part I is working Part II is not...
    homeS.addEventListener(MouseEvent.MOUSE_OVER,f_homeDisplay);
    function f_homeDisplay(event:MouseEvent):void
              homeBtn.gotoAndPlay("home_pass");
              changeContent(homeMsk,-62.8,0);
              changeContent(homeS, -62.8,0);
              homeS.removeEventListener(MouseEvent.MOUSE_OVER,f_homeDisplay);
         }  //Part I
    galleryS.addEventListener(MouseEvent.MOUSE_OVER,f_galleryDisplay);
    function f_galleryDisplay(event:MouseEvent):void
              galleryBtn.gotoAndPlay("gallery_pass");
              changeContent(galleryMsk,-56.55,160);
              changeContent(galleryS, -56.55,160);
              galleryS.removeEventListener(MouseEvent.MOUSE_OVER,f_galleryDisplay);
         } //Part II

    never mind the paragraph at the bottom of this reply, I just got it. I found the instance galleryMsk,
    I can't upload the image now because of some strange reason,
    but it is basically a symbol with a red dot under and it says "mask3, <galleryMsk>", like this:
    Scene1
    - Symbol Definition(s)
          - buttons
              - mask3, <galleryMsk>
    It only showed the objects which are in my major scene, but all those layers are actually inside a movie clip called "buttons", I see "buttons" in the movie explorer, but I don't know how to search for the content inside...

  • SOS, can anyone help me to solve the following question?

    sorry to post it again.
    when I install iplanet web server 6 sp1 on my windows2000 professional version, I put my jsp in the default /docs directory. all my jsps are ok. I can visit the jsp correctly.
    but when I append the %81 or %82 or .....or %99 to the url of my jsp, it shows my jsp source code in IE browser.
    for example, I has a jsp named test.jsp,
    when I visit http://localhost/jsp/test.jsp , everything is all right. and then I visit http://localhost/jsp/test.jsp%81, then the source code of test.jsp is displayed !!!
    It's a really serious security problem.
    how can I do to close the bug ? who can do me a favor ? I expect your help!

    Search fo a patch on the iplanet's web site.
    Fred

  • Can anyone help me to solve my problem?

    my N93i when i turn the cover to camera mode, it cant show out everything in the screen. it just black colour at all, but it got respon. i need to wait so long time and cant capture too. so how? anymore problem of it? izit my cable line problem?

    Does it function ok when you select phone mode ?
    and did you remove the camera lens cover before you turn the display to camera mode
    Message Edited by babdi on 08-Apr-2008 11:04 AM
    If my post has helped you please click the white star on the right

  • Can anyone help me to solve a problem..my ipad2 is currently shutting down and on back itself and after few minutes happen it again..what to do?

    help!help!

    Hey Jan,
    [[How do I upgrade my graphics drivers?]] - see if that article helps. If nothing else works, try [[How do I upgrade my graphics drivers?#w_disabling-webgl|disabling WebGL]].
    --Tobbi

  • Can anyone help me with this StackOverFlow error? Running mergesort

    * Write a description of class Sorter here.
    * @author (your name)
    * @version (a version number or a date)
    public class Sorter
         public static long comparisons = 0;
        public static long exchanges   = 0;
        static int M = 10;
        static double aux[];
        static boolean less(double v, double w) {
           comparisons++;
           return (v < w);
        static void exch(double[] a, int i, int j) {
            exchanges++;
            double t=a; a[i]=a[j]; a[j] =t;
    private static void compExch(double[] a, int i, int j)
    {if (less(a[j], a[i])) exch(a,i,j);}
    static int partition(double a[], int l, int r){
    int i = l-1, j=r; double v = a[r];
    for(;;){
    while (less(a[++i],v));
    while (less(v,a[--j])) if (j==l) break;
    if (i >= j) break;
    exch(a,i,j);}
    exch(a,i,r);
    return i;
    static void insertion(double[] a, int l, int r)
    {int i;
            for(i=r; i>l; i--) compExch(a, i-1, i);
            {int j = i; double v = a[i];
    while(less(v, a[j-1]))
    {a[j] = a[j-1]; j--;}
    a[j] = v;
    static void quicksort(double[] a, int l, int r){
    if (r <= l) return;
    int i = partition(a,l,r);
    quicksort(a,l,i-1);
    quicksort(a,i+1,r);
    static void Iquicksort(double[]a, int l, int r){
    if(r-l <= M) return;
    exch(a, (l+r)/2, r-1);
    compExch(a,l,r-1);
    compExch(a,l,r);
    compExch(a,r-1,r);
    int i = partition(a,l+1,r-1);
    Iquicksort(a,l,i-1);
    Iquicksort(a,i+1,r);
    static void hybridsort(double a[], int l, int r){
    Iquicksort(a,l,r); insertion(a,l,r);}
    static void mergeAB(double[] c, int cl, double[] a, int al, int ar, double[] b ,int bl, int br){
    int i = al, j=bl;
    for (int k = cl; k < cl+ar-al+br-bl+1; k++){
    if (i>ar) {c[k] = b[j++]; continue;}
    if (j>br) {c[k] = a[i++]; continue;}
    c[k] = less(a[i], b[j]) ? a[i++] : b[j++];
    static void mergesort(double[] a, int l, int r){
    if (r<= 1) return;
    int m=(r+1)/2;
    mergesort(a,l,m);
    mergesort(a,m+1,r);
    merge(a,l,m,r);
    static int min(int A, int B){return (A<B) ? A : B;}
    static void mergesort2(double[] a, int l, int r){
    if (r<=1) return;
    aux = new double[a.length];
    for (int m=1; m <= r-l; m=m+m)
    for(int i = l; i <= r-m; i += m+m)
    merge(a,i,i+m-1, min(i+m+m-1, r));
    static void merge(double[]a, int l, int m, int r){
    int i,j;
    for (i=m+1; i>l; i--) aux[i-1] = a[i-1];
    for (j=m; j<r; j++) aux[r+m-j] = a[j+1];
    for (int k = 1; k <= r; k++)
    if (less(aux[j], aux[i]))
    a[k]=aux[j--]; else a[k]=aux[i++];
    * Write a description of class SortClient here.
    * 10-15-06
    public class SorterClient
         public static void main(String[] args) {
    int N = 1000;
    int times = 1;
    System.out.println("ALGORITHM comp=COMPARISONS exch=EXCHANGES");
    System.out.println("---------------------------------------------------------------------------------------------");
    System.out.println();
    System.out.println("Selection: ");
    while(times<4){
    if(times==2){N=3000;}
    if(times==3){N=10000;}
    // generate N random real numbers between 0 and 1
    long start = System.currentTimeMillis();
    double[] a = new double[N];
    for (int i = 0; i < N; i++)
    {a[i] = Math.random();}
    long stop = System.currentTimeMillis();
    double elapsed = (stop - start) / 1000.0;
    // sort them
    Sorter test = new Sorter();
    start = System.currentTimeMillis();
    test.mergesort(a, 0, a.length - 1);
    stop = System.currentTimeMillis();
    elapsed = (stop - start) / 1000.0;
    System.out.print(elapsed + "secs ");
    System.out.print("N=" + N + "= ");
    System.out.print(" " + Sorter.comparisons + "comp");
    System.out.println(" " + Sorter.exchanges + "exch");
    times++;

    I get it in the mergesort method while program is running line,
    mergesort(a,l,m);
    quicksort runs fine.

Maybe you are looking for