import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Task about algorithms
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<String>();
while (true) {
String s = reader.readLine();
if (s.isEmpty()) break;
list.add(s);
}
String[] array = list.toArray(new String[list.size()]);
sort(array);
for (String x : array) {
System.out.println(x);
}
}
public static void sort(String[] array) {
// write your code here
for(int i = 0; i < array.length; i++)
{
for(int j = i+1; j < array.length;j++) {
if (isGreaterThan(array[j], array[i]))
{
if(isNumber(array[i]) && isNumber(array[j])) {
String temp;
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
else if (isGreaterThan(array[i], array[j])) {
if (!isNumber(array[i]) && !isNumber(array[j]))
{
String temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
}
// String comparison method: 'a' is greater than 'b'
public static boolean isGreaterThan(String a, String b) {
return a.compareTo(b) > 0;
}
// Is the passed string a number?
public static boolean isNumber(String s) {
if (s.length() == 0) return false;
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if ((i != 0 && c == '-') // The string contains a hyphen
|| (!Character.isDigit(c) && c != '-') // or is not a number and doesn't start with a hyphen
|| (i == 0 && c == '-' && chars.length == 1)) // or is a single hyphen
{
return false;
}
}
return true;
}
}
help please...!!! numbers in String format....
Resolved
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Heikki
25 October 2018, 12:29solution
I think that the main problem is that your function isGreaterThan(array[j], array[i]) compares String and not int types. If you call is GreaterThan("11","7") it will give you false because String 7 is greater that 11.
First you have to define if compared variables are int type. After that you can decide which one is bigger.
I would use Integer.parseInt(<string>) to get int from String type. I don't know if you have encountered this yet but you will in the future. It requires try - catch exception handling but this way you can define if the string is int type.
I suggest that next time you will give the task number so people can see what you are trying to do :).
+2
Tayyab Mubeen
25 October 2018, 13:07
sorry bro...!! i tried but i think the codey gym would not allow to last three question to ask i mean there is no option to put task no...!! but i think i would do it manually...!!!
i first put values in is numbers function and then parse Int and then compare...??
am i right.?
0
Tayyab Mubeen
25 October 2018, 13:24
thank you soo much bro...!!! i sloved it
0
Roman
25 October 2018, 11:42solution
Try to sort numbers and strings separately.
+2
Tayyab Mubeen
25 October 2018, 04:13
i aslo try comparator
Apple
22
0
Bob
3
Cherry
1
Watermelon
my output this time :( i dont know what happens with zero :/
0