Results 1 to 3 of 3

Thread: JAVA task.

  1. #1
    DEA is offline Member
    Join Date
    Apr 2010
    Rep Power

    Default JAVA task.

    Hello to everyone! I am new here and also new at this computer world.

    I have a small problem on this homework assignment:

    Write a program that receives a list of positive integers and shows on the screen the smallest number(the minimum) and the second smallest number.
    The number of numbers is unknown. The list of numbers ends with a negative number.

    What I have done is:
    PHP Code:
    import java.util.Scanner;
            public class solex4 {
        public static void main(String[] args) {
     Scanner input= new Scanner(;
            int num;
    int num1;
    System.out.println("num , num1");
    ...but this is not working... :(

    Thank you all!

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Rep Power


    1. Format your code properly. If this is how it looks in your editor, then it is not surprising that you find it confusing. If it is properly formatted in your editor, then take the time to format it properly for the forum too -- please don't waste our time.

    2. Think about your requirements before you start coding. Try working it through with pencil and paper, or with real objects -- for instance, shuffle a deck of playing cards and take the first five cards off the top, putting the Smallest and the SecondSmallest in a designated spot on the table, and discarding the rest. Think about each step as you do it, and try to write the steps down.

    3. It's a good idea to write out your thinking in pseudo-code, something like this:
    Java Code:
    // establish variables for smallestValue and secondSmallestValue
    //        and initialize them both to Integer.MAX_VALUE
    // set up a Scanner for user input
    // print out instructions for the user
    // loop
            // prompt user for next number
            // read number entered by user
            // if number is negative (less than zero)
                    // print out smallestValue and secondSmallestValue
                    // exit program
            // if number is less than secondSmallestValue
                    // set secondSmallestValue to number
            // if number is less than smallestValue
                    // set secondSmallestValue to smallestValue
                    // set smallestValue to number entered by user
    // end loop
    By writing your pseudo-code in comments like that, you can leave them in your program to remind yourself what each piece of the program should be doing.


  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    I second everything stated above. Also, you'll want to give your variables names that have meaning, that make sense. This way if anyone looks at the variable, they can almost guess immediately what it's supposed to do. For instance instead of num and num1, how about inputNumber, smallestNumber, and nextSmallestNumber.

    Much luck!

Similar Threads

  1. Java Task
    By Sokox in forum NetBeans
    Replies: 4
    Last Post: 12-13-2009, 09:57 AM
    By marcellis in forum Introductions
    Replies: 0
    Last Post: 09-28-2009, 10:46 AM
  3. Replies: 15
    Last Post: 01-07-2009, 04:42 AM
  4. isolated task in java
    By sardare in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-07-2008, 08:54 PM
  5. java : how to hide application icon from a task bar
    By yogeshagashe in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2008, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts