Results 1 to 12 of 12
  1. #1
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default 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 :
    Java 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);
    Last edited by Fubarable; 01-14-2011 at 05:05 PM. Reason: Moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Moderator edit: code tags added. Please see the first link in my signature links below to see how to do this.

  3. #3
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    I did not understand .Can u explain me in length

  4. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    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.

    Java 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]);
    --user0--

  5. #5
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    what's the purpose of this block?:

    Java Code:
                else if (arr[i] < tempSecSmall) {
                    tempSecSmall = arr[i];
                }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  8. #8
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    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.
    --user0--

  9. #9
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by radhi16 View Post
    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.
    Last edited by Fubarable; 01-14-2011 at 06:26 PM.

  11. #11
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Thank you.I solved the problem.

  12. #12
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by user0 View Post

    Java 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,
    --user0--

Similar Threads

  1. Sum positive numbers using 10 inputs from user
    By pvictory1 in forum New To Java
    Replies: 15
    Last Post: 10-10-2010, 01:30 AM
  2. Replies: 1
    Last Post: 09-24-2010, 07:08 PM
  3. Replies: 5
    Last Post: 02-07-2009, 07:48 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  5. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 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
  •