Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Why is my code not working?

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is my code not working?

    Hi guys, im learning java and i have an assigment to to input 100 number and then show the highest number and when it was inputed (exe, 1,5,2 shows the number 5 and 2 (2 means second input) now ive writting the code and the result is
    Input a number
    15
    1
    1
    1
    1
    15 Was inputed 1
    This is ok,but when i try to put the highest number last it shows as follows:
    Input a number
    1
    2
    3
    4
    5
    4 Was inputed 4
    I understand that the problem is that when the last number is inputed it dosent enter the loop so value dosent get its new "value",but i dont know how to work around it,something is wrong with while part i gusses but i cant figure it out

    The code:

    package page25;
     
    import java.util.Scanner;
     
    public class Drill9 {
     
    	public static void main(String[] args) {
    		System.out.println("Input a number");
     
    		Scanner scan = new Scanner(System.in);
    		int num = scan.nextInt();
    		int counter = 1, value = 1, max = 0;
     
    		while (counter < 5) {
    			if (num >= max) {
    				max = num;
    				value = counter;
    					counter++;
     
    			}
     
    	num = scan.nextInt();
     
    		}
     
    		System.out.println(max + " Was inputed " + value);
     
    	}
     
    }

    Edited*
    edited**
    Last edited by mike512; June 15th, 2019 at 07:57 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is my code not working?

    why it dosent work
    What does that mean?
    Can you copy the full contents of the console window from when you execute the program and paste it here?
    Add some comments explaining what is wrong with the current output
    and show what the desired output is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is my code not working?

    Edited

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is my code not working?

    Ok. How are you trying to debug the code to find the problem?
    One way is to add some print statements that show the values of variables as the program executes so you can see what the computer sees.
    For example print out the values of num and num2 immediately after the while and before the if statement.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is my code not working?

    In contex to how im trying to debug...im pretty much changing stuff and hoping it works..
    After trying what you suggested ive changed it a bit and it somwhat works,but there is still a problem.
    If the last number i input is higher than the rest it dosent go inside the while loop so my "value" dosent get a new value,meaning
    Input a number
    1
    2
    3
    4
    5
    4 Was inputed 4
    I understand the problem,but i just cant seem to think of a solution

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is my code not working?

    changing stuff and hoping it works
    Often that is not the best approach. Having a good design for a program then debugging the code to see why it is not following the design can be a better approach.

    What are the values of the variables as the code executes?
    What happens in the loop when the 5 is read?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is my code not working?

    Agien,i understand why it dosent work,but the only fix i can think of is adding a if (num>max) {value=5}. which makes it the last value inputed.
    But its a workaround,it dose fix the problem but its not pretty and im sure there is a better way.
    as for what happens?this~


    Input a number
    1
    Current counter1
    2
    Current counter2
    3
    Current counter3
    4
    Current counter4
    5
    4 Was inputed 4

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is my code not working?

    Current counter4
    5
    What happens in the loop after the 5th number is read? What is the value of counter? How does the while loop condition evaluate with that value in counter? Will the loop go another time allowing for the value of the 5th number to be compared and saved if appropriate?

    Also change the values that are entered so they are not ascending. For example: 11, 2, 33, 4, 55
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is my code not working?

    Fixed...so...easy..yet i got confused
    Thanks for the help

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is my code not working?

    Glad you got it working. It often helps people to see the logic if there are comments:
                 counter++;                  //  set index for next number
                 int num = scan.nextInt(); // read the next number

    Another hint when searching for max (or min) set the initial value to be at the end of the range:
    For example max at -999999999
    and min to 9999999999
    Then the first compare will always be true and will be saved as the max/min value
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. help i cant get my code working
    By alexrox27 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 23rd, 2013, 11:27 AM
  2. Please why is this code not working
    By Petrejonn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2013, 10:18 AM
  3. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  4. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 33
    Last Post: March 12th, 2012, 03:23 PM
  5. I don't get why this code isn't working
    By tai8 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: February 20th, 2012, 12:38 PM