Reputation: 347
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
Upvotes: 1
Views: 95
Reputation: 2907
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 boolean
s and gives a boolean
, true
if both evaluate to true
.
However, two int
s 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 int
s, long
s, short
s, byte
s, float
s and double
s.
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 int
s. Some int
s are greater than others, not the case with String
s, 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!
}
Upvotes: 0
Reputation: 726579
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.
Upvotes: 0
Reputation: 37023
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.
Upvotes: 1
Reputation: 393821
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.
Upvotes: 2