1

I am working on a program where user can input random numbers and when user give -1 the loop will break and the entered numbers will be displayed

  Example:
  Enter a Number: 32

  Enter next Number: 1243

  Enter next Number: 123

  Enter next Number: 76

  Enter next Number: -1

  Thank You. You have entered 32, 1243, 123, 76

Now whatever number is entered it will be displayed in ascending order

 ----Ascending order -----
   [32,76,123,1243]

Now i have completed the following but the to get exact result the user need to enter

 ->0032

 ->0076

 ->0123

 ->1243

Then i am getting the exact result

    [0032, 0076, 0123, 1234]

Then only my sorting is working fine otherwise it is like

      [ 123, 1243, 32, 72]

Now how to solve this ?

package Testx;

import java.util.Arrays;
import java.util.Scanner;

public class Test7Ctr{

public static void main(String[] args) 
{
    // TODO Auto-generated method stub
    Scanner user = new Scanner(System.in);
    String user_input = "";
    String holdv="";
    String holdvx="";   
    String ascend="";
    //AsveNd(user_input);

try
{
    int b =0;

    do
    {
        System.out.print("Enter next number:");
        user_input = user.nextLine();
        int x= Integer.valueOf(user_input);

        if (x != -1)
        {
        holdv=user_input;
        holdvx+=holdv+",";
        ascend+=holdv+" ";
        b++;
        }

        else
        {

            System.out.println("THANK YOU FOR ENTERING= "+holdvx);
            break;
        }
    }
   while(b <= 100);
    {

    }

    String[] numbers=ascend.split("\\s");
    for(String numb:numbers)
    {  
        int intarray[] = new int[numbers.length]; 
        Arrays.sort(numbers);
        //break;
        }  
    System.out.println("---Ascending order---");
    System.out.println(Arrays.toString(numbers));

}
catch(Exception e)
{

}

}
}
2
  • well you are storing your numbers as Strings, so when you sort you will do sorting by String not int - what is the use of intarray it is not even used Commented Jun 1, 2016 at 6:47
  • I would also recommend, for learning purposes, that you submit your working code on codereview.stackexchange.com. Empty catch clauses, wrong casing, bad variable names... ;) Commented Jun 1, 2016 at 7:09

3 Answers 3

3

You are sorting strings rather than numbers. This makes your sort work in lexicographic order instead of plain ascending order. So to fix your problem, simply add Integers and not Strings. You can even parse a String to an Integer using Integer.parseInt().

Also, there is no need to call sort every time you insert a new number, but just once in the end. That adds a lot of overhead to your process.

Sign up to request clarification or add additional context in comments.

Comments

0

You can get the proper integer sorting with the following:

String[] numbers = ascend.split("\\s");
int intarray[] = new int[numbers.length];
int i = 0;
for (String numb : numbers)
{
    // convert the String to an int
    intarray[i++] = Integer.parseInt(numb);
}
Arrays.sort(intarray); // sort the int array, not the String array
System.out.println("---Ascending order---");
System.out.println(Arrays.toString(intarray));

Side Note:

Your do/while loop would be better written as:

int x = 0;
do
{
    System.out.print("Enter next number:");
    user_input = user.nextLine();
    x = Integer.valueOf(user_input);

    if (x != -1)
    {
        holdv = user_input;
        holdvx += holdv + ",";
        ascend += holdv + " ";
    }

} while (x != -1);

System.out.println("THANK YOU FOR ENTERING= " + holdvx);
  • You don't need the int b
  • You don't need the else statement in the loop. Just have the loop terminate when x is -1.
  • Also, you had an empty block after the while (...); That is not doing anything. The while portion of a do/while loop has no body.

Comments

0

You are sorting the "Strings" and not the "numbers". Scan those inputs as number, put them to a dynamic list (and not a static sized array) and then sort the list of numbers (and not of string)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.