#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Exclamation Simple java code not working right (Programming Noob)


    I'm a freshman majoring in computer science, and I wrote out this code for a programming lab we had. Only it doesn't work quite as I had expected and I'm not sure why things aren't working out. This is probably elementary stuff for you guys, but im stumped. So if anybody could help tell me where I messed up as well as what I can do to make this code work as I want would be greatly appreciated. An explanation as to why my way doesn't work and another way does would also be very helpful.

    The program is supposed to read a series of integers, find the largest one, and count how many times it occurred. The code I wrote is as follows:

    Code:
    import java.util.Scanner;
    public class Lab4 {   
    public static void main(String[] args) {       
    Scanner input = new Scanner(System.in);          
    int max;    
    int count = 1;     
    int intinput;          
    System.out.print("Enter a series of integers. 0 will signal the end of the sequence."); 
    max = input.nextInt();          
    do {     System.out.print("Enter a series of integers. 0 will signal the end of the sequence.");    
    intinput = input.nextInt();    
    if (intinput == max); {     
    count++; }    
    if (intinput > max); {     
    max = intinput;    
    count = 1; }     } 
    while (intinput != 0);   
                
    System.out.print("The largest number is " + max);     System.out.print("\nThe occurance count for the largest number is " + count);     
    } 
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    Only it doesn't work quite as I had expected and I'm not sure why things aren't working out.
    To be honest, I think it would be good to tell exactly what you expected. You know - people have not seen your assignment and have only your code (which is not formatted well).

    With this program here are several issues:

    1. Use println instead of print so that caret moves to new line after print.
    2. It would be good to put first message out of the loop so it is not repeated.
    3. You are trying to count "max" elements before you find what "max" is. Problem is you are trying to do this in one loop (which is not possible).

    You'd better use one loop to input numbers and store them all in prepared array.

    Then use the second loop to go through array and find what maximum is.

    Then in the third loop go through array once more and count how many times "max" is encountered.

    I am not going to give you ready solution, since it looks like your school/university assignment and it would not be good to spoil your education.
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    One thing I notice about your code is this

    Code:
    if (intinput == max); {     
            count++; 
            }
    The question to is does this only increment count if intinput == max?

IMN logo majestic logo threadwatch logo seochat tools logo