7 Replies - 920 Views - Last Post: 22 January 2013 - 06:08 PM Rate Topic: -----

#1 surfmatic  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 17
  • Joined: 21-January 13

Displaying smallest two numbers out of any numbers entered.

Posted 21 January 2013 - 05:08 PM

Hello! I'm new to these boards and I have long stalked them for help, and I figured it was time to finally join in on the fun. I started with an intro to C class and did well in it. This is my second programming class and introduction to OOP via Java.

My assignment is to find the two smallest integers entered. No arrays may be used. Any valid integers will be entered, numbers may be repeated, and no need to handle non-numeric input. They will only test for positive and negative integers. I have to also use EOF as the first character sequence on a line to indicate no more input, and the code has to handle any number of entries including less than 2.

Here are some sample executions...

Enter a series of integers; EOF to quit.
-16 3 -16
^D

Smallest distinct number... -16
Second smallest number ... 5

Enter a series of integers; EOF to quit.
5
^D

Smallest distinct number... 5

Enter a series of integers; EOF to quit.
4 0
5 -20
34
22
-1
^D

Smallest distinct number... -20
Second smallest number ... -1

Enter a series of integers; EOF to quit.
3 3
^D

Smallest distinct number... 3

Enter a series of integers; EOF to quit.
^D
No number entered.


Anyhow this is my code so far, and it makes sense to me however I simply cannot get it to run without a bunch of errors. What I'm trying to do is have input.nextInt() read off how-ever many numbers were inputted, and comparing them via my if else statements. By doing so I will have the two smallest integers in the end and will print them individually. I really do apologize if it is messy, but this is my second day on this and my brain has officially run out of possibilities for this code to run. I feel like I'm so close!! Any help would be great!

import java.util.*;

public class SmallestTwo {
  public static void main(String[] args) {

    int min1 = 0;
    int min2 = 0;
    int input = 0;

    Scanner input = new Scanner (System.in);
    System.out.print ("Enter a series of integers; EOF to Quit.");

    while ( input.hasNext() )
    {
      number = input.nextInt();

      min1 = num1;
      min2 = num1;


        if (num2 <= min1)
        {
          min2 = min1;
          min1 = num2;
        }
        else if (num2 < min2)
        {
          min2 = num2;
        }
        else if (min1 == min2)
        {
          min2 = num2;
        }

      System.out.println("Smallest distinct number entered was " + min1);
      System.out.println("Second smallest distinct number entered was " + min2);
    }
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying smallest two numbers out of any numbers entered.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Displaying smallest two numbers out of any numbers entered.

Posted 21 January 2013 - 05:19 PM

It's always helpful (and preferred) if you post your errors with your code, copied and pasted just as they appear at your end.

You have some basic variable errors:

2 variables named input (one int, one Scanner)
variables number, num1, and num2 are undeclared (what types of variables are they?)

Reread the errors you're getting and compare them to my comments above so that you can correlate the error messages to a more common-man explanation.
Was This Post Helpful? 1
  • +
  • -

#3 surfmatic  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 17
  • Joined: 21-January 13

Re: Displaying smallest two numbers out of any numbers entered.

Posted 21 January 2013 - 05:50 PM

Hi Greg, thanks for responding. I took a little extra time to revamp the code to make it a little easier to follow, also with consideration to what you said. Here is the code:

import java.util.*;

public class TwoSmallest {
  public static void main(String[] args) {

    int min = 0;
    int max = 0;
    int input = 0;

    Scanner input = new Scanner (System.in);
    System.out.print ("Enter a series of integers; EOF to Quit.");

    while ( input.hasNext() )
    {
      number = input.nextInt();

      max = number;
      min = number;


        if (input > max)
        {
          max = input;
        }
        else if (input <= min)
        {
          min = input;
        }

      System.out.println("Smallest distinct number entered was " + min);
      System.out.println("Second smallest distinct number entered was " + max);
    }
  }
}



Here is the error:

TwoSmallest.java:10: input is already defined in main(java.lang.String[])
    Scanner input = new Scanner (System.in);
            ^
TwoSmallest.java:13: int cannot be dereferenced
    while ( input.hasNext() )
                 ^
TwoSmallest.java:15: cannot find symbol
symbol  : variable number
location: class TwoSmallest
      number = input.nextInt();
      ^
TwoSmallest.java:15: int cannot be dereferenced
      number = input.nextInt();
                    ^
TwoSmallest.java:17: cannot find symbol
symbol  : variable number
location: class TwoSmallest
      max = number;
            ^
TwoSmallest.java:18: cannot find symbol
symbol  : variable number
location: class TwoSmallest
      min = number;
            ^
6 errors


Could you please help me tackle the errors?
Was This Post Helpful? 0
  • +
  • -

#4 surfmatic  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 17
  • Joined: 21-January 13

Re: Displaying smallest two numbers out of any numbers entered.

Posted 21 January 2013 - 06:07 PM

I'm currently working on the sorting section of the code as it seems to not work.
Was This Post Helpful? 0
  • +
  • -

#5 SpiderPig42  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 15-March 12

Re: Displaying smallest two numbers out of any numbers entered.

Posted 21 January 2013 - 07:56 PM

Try this

Solution Removed by GregBrannon



This post has been edited by GregBrannon: 22 January 2013 - 03:32 AM
Reason for edit:: Removed the solution

Was This Post Helpful? 1
  • +
  • -

#6 surfmatic  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 17
  • Joined: 21-January 13

Re: Displaying smallest two numbers out of any numbers entered.

Posted 21 January 2013 - 11:11 PM

Spider, that code works perfectly! I hesitate to ask but seeing as you're a first time poster as well, is this your own code from a relative assignment, or did you use my code and then manipulate it?

Either way, flawless and thank you.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Displaying smallest two numbers out of any numbers entered.

Posted 22 January 2013 - 03:29 AM

@SpiderPig42,

Please review the DIC Guidelines for answering questions at the DIC FAQ Announcement, above. In short, rather than posting the code/solution to the OP's homework problem, assist the OP to discover their own solution. The goal is to help those who ask for help to become independently competent programmers, because you won't be sitting beside them for all tests, quizzes, and homework assignments.

@surfmatic,

Please come back when you need help understanding why your code doesn't work and/or for guidance on how to make your code better. Do not expect to be handed the solution in the future.
Was This Post Helpful? 3
  • +
  • -

#8 surfmatic  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 17
  • Joined: 21-January 13

Re: Displaying smallest two numbers out of any numbers entered.

Posted 22 January 2013 - 06:08 PM

His answer was unexpected and not expected in the future. It was far from complete since it only adhered to having a solution for inputs greater than 2, but not for 1 or null. The code had to be reworked and rewritten but served as a skeleton backbone for the re-written code I now have, which is twice in length.


Thank you for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1