Find 2 largest numbers program! 10 import java.util.Scanner; 11 12 public class TwoLargest { 13 14 public static void main(String [] args) { 15 16 /*Construct a Scanner that produces values scanned from 17 * standard input* 18 */ 19 System.out.println("Enter a series of integers; EOF to Quit."); 20 Scanner input = new Scanner(System.in); 21 int max1 = 0; 22 int max2 = 0; 23 int counter = 0; 24 while ( input.hasNext() )//while there is more input (user has not hit EOF) 25 { 26 counter++; 27 int number1 = input.nextInt(); 28 if(number1 > max1 && number1 > max2){ 29 max2 = max1; 30 max1 = number1; 31 } 32 if(number1 <= 0){ 33 34 } 35 if(number1 < max1 && number1 > max2){ 36 max2 = number1; 37 } 38 } 39 if(counter == 1){ 40 System.out.println("Largest distinct number entered was "+max1); 41 } 42 if(counter > 1){ 43 System.out.println("Largest distinct number entered was "+max1); 44 System.out.println("Second largest distinct number entered was "+max2); 45 } 46 if(counter == 0){ 47 System.out.println("No numbers entered."); 48 } 49 } 50 } ~ ~ ~ ~
But my program isn't able to carry out these examples, so how do I fix it? I know why it doesn't work, but I don't know how to fix it.
tell us why it doesn't work.
Try putting in print statements to follow what is happening Something on debugging practice http://www.greenteapress.com/thinkapjava/html/thinkjava003.html#toc4 That said, this would be my strategy (end of file indicated by eof() ) # read in first two and order them -if not eof() : max1 = read in first number ; counter = 1 -if not eof() : max2 = read in the second number : counter = 2 - if max2 > max 2 then swab them # read in next and drop the smallest of the three - while not eof()''' counter = counter + 1 input = read next number if input > max1 then max2 = max1 ; max 1 = input else if input > max2 : max2 = input etc
Also, when pasting stuff here, use the accent on the same key with the tilde, ~, to make code blocks. Three above and three below on a separate line. \(\text{```}\). See, that makes: ``` import java.util.Scanner; public class TwoLargest { public static void main(String [] args) { ``` It makes things so much easier to read. OR Use a code paste site like dpaste or pastebin. Again, they make code format properly. Also, without something like that we can not easily copy your code to test and see the error messages.
I'll try it, I didn't get that till now ``` # read in first two and order them counter = 0 -if not eof() : max1 = read in first number ; counter = 1 -if not eof() : max2 = read in the second number : counter = 2 if max2 > max 2 then swab them # read in next and drop the smallest of the three - while not eof() counter = counter + 1 # not really required any more input = read next number if input > max1 then max2 = max1 ; max 1 = input else if input > max2 : max2 = input etc ``` Alright! This is more pseudocode, it has to be converted to c++
It would be much easier to have a sentinel value that wasn't a string. Unless you want any non-number to terminate the program with a try/catch block. note: a sentinel value is a value that the user enters to stop the loop. This is because, if you input a string, and the scanner expects to read in an integer, you will get an error and the program will crash.
Actually, you could test for string == "eof" in the catch section of the try/catch block... If you're still interested in solving this puzzle, let me know. -woodrow
Join our real-time social learning platform and learn together with your friends!