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 6 of 6

Thread: How to correct this code?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to correct this code?

    Description of the program: read a sequence of positive integers from terminal. When user types
    -1, the program will print out the largest number and exit.

    Program mid.java

    import java.util.Scanner;
    public class test
    {
    public static int num;
    public static void main (String[] args)
    {
    Scanner in = new Scanner(System.in);
    num = in.nextInt();
    int large = num;
    while(num > 0);
    {
    if(0 > num && num > large)
    large = num;
    }
    System.out.println("The largest number is : " + large);
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to correct this code?

    What does this program do? What do you expect it to do? What happened when you stepped through it line-by-line to figure out when its execution differs from your expectation?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to correct this code?

    Description of the program: read a sequence of positive integers from terminal. When user types -1, the program will print out the largest number and exit.

    i get "the largest number is" only when i type -1 in the beginning but it should be displayed anytime i type -1 to bring up the largest number i previously entered.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to correct this code?

    What happened when you stepped through this code with a debugger, or at least added some print statements? When does the execution of this code differ from what you expect it to do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to correct this code?

    im a beginner but when i ran the code it lets me input numbers right. so i want to enter as many numbers, and then when i type -1 the program should give me the largest number in the sequence. right now it only gives me largest number when i first and only type in -1. this is not what i want.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to correct this code?

    Right, you have to debug this code. That's what you're asking us to do, and it's something you have to get into the habit of doing.

    You do that by tracing through the code, either with a piece of paper and a pencil, or with a debugger, or just by adding print statements. Go through each line and figure out what you expect it to do, then figure out what it's actually doing. One of those lines isn't doing what you think it's doing, and you have to narrow it down to figure out which one it is.

    Hint: when do you read in the second number?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help please i keep getting a runtime error with code that seems correct.
    By jorys22 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 8th, 2013, 04:59 AM
  2. is my code correct?
    By leonne in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 12th, 2012, 04:36 PM
  3. Can anybody correct the code?
    By ur2cdanger in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 24th, 2011, 12:50 AM
  4. Can anybody correct the code?
    By ur2cdanger in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2011, 07:07 PM
  5. I still have errors. Can you PLEASE correct my code?!
    By sam30317 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2011, 06:10 AM