# Problem getting numbers from user and finding smallest two numbers

Printable View

• 01-14-2011, 06:05 PM
radhi16
Problem getting numbers from user and finding smallest two numbers
Here is the code written for taking input from command line and displaying the first smallest and 2nd smallest number of the given array.I am not able to understand y i am not getting the first smallest and 2nd smallest number of the given array.Can any one tell me.

The code :
Code:

```        int i;         int arr[] = new int[10];         Scanner scan = new Scanner(System.in);         System.out.println("Enter numbers for the array: ");         String s = scan.nextLine();         System.out.print("The numbers entered are :");         System.out.println(s);         int tempFirstSmall = arr[0];       int tempSecSmall = arr[0];         for (i = 1; i < arr.length; i++) {             if (arr[i] < tempFirstSmall) {                 tempSecSmall = tempFirstSmall;                 tempFirstSmall = arr[i];             }             else if (arr[i] < tempSecSmall) {                 tempSecSmall = arr[i];             }         }         System.out.println("The 1st smallest number of the given array is:" + tempFirstSmall);         System.out.println("The 2nd smallest number of the given array is:" + tempSecSmall);```
• 01-14-2011, 06:06 PM
Fubarable
Moderator edit: code tags added. Please see the first link in my signature links below to see how to do this.
• 01-14-2011, 06:17 PM
radhi16
I did not understand .Can u explain me in length
• 01-14-2011, 06:38 PM
user0
Hi radhi - what Fubarable means is, whenever you ask here for help and you copy and paste your code, always use the code tags around your code. There is a button "#" you should be able to see when you post your question. Simply, highlight your code, and press that button. It makes it easier for everyoen to read your code that way.

Secondly, have a look at the below code. All i did was do a sort on your array using Arrays.sort() method, it makes what you're trying to accomplish very simple.

Code:

```        int arr[] = new int[10];         Scanner scan = new Scanner(System.in);         for(int i = 0; i < arr.length; i++)        {             System.out.print("Enter number " + (i+1) + ": ");             arr[i] = scan.nextInt();         }         System.out.print("The numbers entered are : ");         for(int x: arr)        {                 System.out.print(x + " ");         }         System.out.println();         Arrays.sort(arr);                 System.out.println("The 1st smallest number of the given array is:" + arr[0]);         System.out.println("The 2nd smallest number of the given array is:" + arr[1]);```
• 01-14-2011, 06:43 PM
radhi16
Thank you very much.I will make sure that i follow it when i post some thing next time.
I am not suppose to use array.sort .so can u let me know where is it that i am doing wrong.
• 01-14-2011, 06:52 PM
Fubarable
what's the purpose of this block?:

Code:

```            else if (arr[i] < tempSecSmall) {                 tempSecSmall = arr[i];             }```
• 01-14-2011, 07:00 PM
Fubarable
Also and more importantly, where do you fill the array with numbers?
• 01-14-2011, 07:03 PM
user0
if you are not allowed to use sorting, then I ask you just as Fubarable did, why have the else if statement? I think if you get rid of it, you should be good.
• 01-14-2011, 07:15 PM
radhi16
Scanner scan = new Scanner(System.in);
System.out.println("Enter numbers for the array: ");
String s = scan.nextLine();
System.out.print("The numbers entered are :");
System.out.println(s);
So here s will have the numbers which i entered through command line
and after removing the else if code,my code doesn't work.
i am not able to understand the logic .Please help.
• 01-14-2011, 07:18 PM
Fubarable
Quote:

Originally Posted by radhi16
Scanner scan = new Scanner(System.in);
System.out.println("Enter numbers for the array: ");
String s = scan.nextLine();
System.out.print("The numbers entered are :");
System.out.println(s);
So here s will have the numbers which i entered through command line
and after removing the else if code,my code doesn't work.
i am not able to understand the logic .Please help.

Again, when posting code, please use code tags.

s will not have any numbers, but rather s will hold a String, nothing more, nothing less and your int array will be empty unless you fill it. You have to iterate through this String (you could create another Scanner object with s -- new Scanner(s) and iterate through it using nextInt() to get your ints) and fill your array.

edit: I also changed your thread title from "help needed" to one that makes more sense, that summarizes your main problem. Please use more informative titles in the future.
• 01-14-2011, 07:35 PM
radhi16
Thank you.I solved the problem.
• 01-14-2011, 07:36 PM
user0
Quote:

Originally Posted by user0

Code:

```        int arr[] = new int[10];         Scanner scan = new Scanner(System.in);         for(int i = 0; i < arr.length; i++)        {             System.out.print("Enter number " + (i+1) + ": ");             arr[i] = scan.nextInt();         }```

Look at the code I posted earlier. This is how I would populate the array, using nextInt().

Best,