1

I am just trying to sort the string in descending order. input provided by the user is 10,a,1,#,15,.,6 output must be 10,a,15,#,6,.,1 I have tried.

String input = JOptionPane.showInputDialog("Enter a string:");
String[] num = input.split(",");
ArrayList<String> arr = new ArrayList<>();

 System.out.println(num);
  for ( int i = 0; i < num.length - 1; i ++ )  
   {
    for (int j = i + 1; j < num.length; j ++ )
     {
       if(Integer.parseInt(num[i]) 
       && Integer.parseInt(num[j])  
        && num[i] < num[j])  {
          String temp = num[ i ];   //swapping
           num[ i ] = num[ j ];
           num[ j ] = temp;
            }
            }           
      } 
     }

In if statement i get error. error:- The operator && is undefined for the argument type(s) int, int - The operator < is undefined for the argument type(s) java.lang.String, java.lang.String

4
  • Indeed.. && is undefined for the argument type int. Commented Mar 8, 2015 at 12:20
  • I tried || even it is not working. Commented Mar 8, 2015 at 12:21
  • Because || is undefined for the argument type int as well. Your code is asking for something like: if(5 && 6) which make no sense in Java. Commented Mar 8, 2015 at 12:22
  • Integer.parseInt(num[i]) && Integer.parseInt(num[j]) seems meaning less Commented Mar 8, 2015 at 12:22

4 Answers 4

2

You can replace

   if(Integer.parseInt(num[i]) 
      && Integer.parseInt(num[j])  
      && num[i] < num[j])  {

with

   if(Integer.parseInt(num[i])  <
      Integer.parseInt(num[j])) {  

However, you'll get a NumberFormatException if one of the Strings can't be parsed as an integer.

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

Comments

1

You should change your if from:

Integer.parseInt(num[i]) 
   && Integer.parseInt(num[j])  
    && num[i] < num[j]

To:

Integer.parseInt(num[i]) 
   < Integer.parseInt(num[j])  
   ^^^
    && num[i].compareTo(num[j]) < 0
              ^^^^^^^^^

If you want to compare two numbers, you could compare for less than/greater than and/or equal to. You could apply && on two booleans like condition1 && condition2

Aside note, if any of the number is not parseable as integer then you might get NumberFormatException. So i would suggest you first recognize input and then use the api to convert it to a number.

2 Comments

when do i add it to array? and in case of string input what should i do?
You could check if the element you have is either number or String by using string.matches("^\\d+$") if it returns true then that means its a number else its a string.
0

The first thing is to make your code compile: recall that Integer.parse returns the integer, so you cannot use it in the sequence of logical expressions.

Then, you should make your code more efficient: move parsing of num[i] outside the loop, and skip the loop when parsing fails, like this:

for ( int i = 0; i < num.length - 1; i ++ ) {
    int numI;
    try { numI = Integer.parse(num[i]); } catch (NumberFormatException nfe) { continue; }
    for (int j = i + 1; j < num.length; j ++ ) 
        int numJ;
        try { numJ = Integer.parse(num[i]); } catch (NumberFormatException nfe) { continue; }
        if (numI < numJ) {
            String temp = num[ i ];   //swapping
            num[ i ] = num[ j ];
            num[ j ] = temp;
        }
    }
}

Now when parsing num[i] fails, the program skips over the nested loop, going to the next string instead.

6 Comments

i want to add num to arraylist arr.
@koshishkharel Make an ArrayList<Integer> arr instead of ArrayList<String>, use one loop to parse and add numbers to arr, and then use two loops to sort the results the way you did.
I am getting error in try catch block it says " The type of the expression must be an array type but it resolved to ArrayList<Integer>".
@koshishkharel You cannot use [] operator with lists. Replace num[x] with num.get(x) when you read the value, and num[x] =... with num.set(x, ...) when you set the value to a position that already exists. If an element does not exist yet, use num.add(...) and pass the appropriate value.
what should i do while swapping?
|
0

I'm not going to look at the rest of your code, but the bit with the error should give an error. Here is an illustration:

Integer.parseInt(num[i]) --> an int
Integer.parseInt(num[j]) --> another int

&& takes two booleans and gives a boolean, true if both evaluate to true.

However, two ints are given. What do we do with those? That's right, an error.

num[i] --> a String object (remember the declaration: String[] num = ...)
num[j] --> another String object

< is the 'less than' comparison operator, which works on ints, longs, shorts, bytes, floats and doubles.

As String is not one of those numeric types, however, an error is thrown.

I think that you want to say Integer.parseInt(num[i]) < Integer.parseInt(num[j]). This works, because you give the < operator two ints. Some ints are greater than others, not the case with Strings, so it compiles fine. But note the warning in Eran's answer; you will get a NumberFormatException if Integer.parseInt() is given an invalid input. To handle this, use a try/catch:

int numI, numJ;
try {
    numI = Integer.parseInt(num[i]);
    numJ = Integer.parseInt(num[j]);
    // rest of code
} catch (NumberFormatException e) {
    // not a number, handle here!
}

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.