Hi, does anyone know what is wrong here? The program is supposed to find the largest number entered, and the 2nd largest number entered. The process for assigning the 2nd largest is not working, I do not think I am evaluating it correctly, and suggestions?

package random;
import java.util.Scanner;
public class random23 {
public static void main(String[] args) {
        int number;
        int largest = 0;
        int largestSoFar = 0;
        int secondLargest = 0;
        Scanner input = new Scanner(System.in);
        for(int count = 0; count < 10; count++){
            System.out.print("Enter a number: ");
            number = input.nextInt();
            if (number > largestSoFar){
                largestSoFar = number;
            }
            if (count == 9) {
                largest = largestSoFar;
                System.out.println(largest);
            }
            
            if (number <= largest){
               if (number > secondLargest){
                   secondLargest = number;
               }
            }
            System.out.print("------------------------ \n");
        }
    System.out.printf("Largest# = " + largest + "\n");
    System.out.printf("Second Largest# = " + secondLargest + "\n");
    }
}

You want to assign "largestSoFar" to "secondLargest" before you assign "number" to "largestSoFar", but within the same if block.

if I assign largestSoFar to secondLargest, this program occurs and only returns zero, are you saying that I should do this:

if (number > largestSoFar){
                largestSoFar = secondLargest;
                number = largestSoFar;
            }

or this?

package random;
import java.util.Scanner;
public class random23 {
public static void main(String[] args) {
        int number;
        int largest = 0;
        int largestSoFar = 0;
        int secondLargest = 0;
        Scanner input = new Scanner(System.in);
        for(int count = 0; count < 10; count++){
            System.out.print("Enter a number: ");
            number = input.nextInt();
            if (number > largestSoFar){
                largestSoFar = secondLargest;
            }
            if (count == 9) {
                largest = largestSoFar;
            }

            if (number < largest){
               if (number > secondLargest){
                   number = secondLargest;
               }
            }
            System.out.print("------------------------ \n");
        }
    System.out.printf("Largest# = " + largest + "\n");
    System.out.printf("Second Largest# = " + secondLargest + "\n");
    }
}
if (number > largestSoFar){
        secondLargest = largestSoFar;
        largestSoFar = number;
      }

of course

And this, of course, will be removed

if (number < largest){
  if (number > secondLargest){
    number = secondLargest;
  }
}

You can also remove all current references to "largest" and then rename "largestSoFar" to "largest".

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.