1

I was trying to test my skills and I try to do a test.

I receive the following input:

[7,11,10,6,9]

[21,24,25,23,26]

[116,115,117,120,121,119]

I need to sort all these values.

I try to do the following:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] parts = null;
List<String> linhas = new ArrayList<>();

for (int i = 0; i < 3; i++) {
    String line = br.readLine();
    line = line.replace("[","");
    line = line.replace("]","");
    line = line.replace(" ","");
    System.out.println(line);
    parts = line.split(",");
}

This way I got to show the output

7,11,10,6,9

21,24,25,23,26

116,115,117,120,121,119

The "String[parts]" got all values, but I don't know how to sort it, because the "parts" parameter is inside a "for loop".

How can I convert it to int/Integer and sort each line?

2
  • Have you tried anything yet? Consider using a loop in combination with Integer.parseInt("string")...Also you can look at Arrays.sort()... Commented Jul 25, 2019 at 15:44
  • If you want to keep the way you are doing it you can also use line = line.substring(1, line.length() - 1); to remove the brackets instead of replacing them one at a time. Commented Jul 25, 2019 at 15:58

3 Answers 3

1

Assuming [7,11,10,6,9] be the input, we can try converting to a list of integers, and then sort that list:

String input = "[7,11,10,6,9]";
input = input.replaceAll("\\[(.*)\\]", "$1");
String[] vals = input.split(",");
List<Integer> output = Arrays.stream(vals)
    .map(v -> Integer.parseInt(v))
    .collect(Collectors.toList());
Collections.sort(output);
System.out.println(Arrays.toString(output.toArray()));

This prints:

[6, 7, 9, 10, 11]
Sign up to request clarification or add additional context in comments.

Comments

1

Once you have parts you must convert this String[] into an int[]. To do this, first create an int[] for your results to go in. It must be the same size as your String[]:

int[] ints = new int[parts.length];

Then, iterate through the String[] and fill in values in the int[]:

for (int i = 0; i < parts.length; i++) {
    ints[i] = Integer.parseInt(parts[i]);  // converts a string containing an integer into its int value
}

Finally, to sort each line, a simple call to Arrays.sort(ints); will sort your array of integers.

Bonus:

This can be achieved more cleanly in a single line using Java 8 Streams, as follows:

List<Integer> sortedInts = Arrays.stream(parts).map(Integer::parseInt).sorted().collect(Collectors.toList());

2 Comments

You are sorting Strings not ints. Are you aware of this?
Check this int[] sortedInts = Arrays.stream(new String[] {"1", "2"}).mapToInt(Integer::parseInt).sorted().toArray(); Make IntStream, sort it, then invoke toArray directly.
1

You can do the replace() lines at once and split more cleanly with a regex:

parts = line.split("[\\[\\], ]+");

and use the Iterable technique that cameron1024 demonstrates in his answer. I think that using Iterable is a better choice for this use case than using Streams because the input size is trivially small and Streams has to spend more time to spin up.

The whole thing would look like:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] parts;
List<String> linhas = new ArrayList<>();

for (int i = 0; i < 3; i++) {
    String line = br.readLine();
    parts = line.split("[\\[\\] ,]+");
}

int ints[] = new int[parts.length];

for(int i = 0; i < parts.length; i++){
    ints[i] = Integer.parseInt(parts[i]);
}
Arrays.sort(ints);

3 Comments

When I try to compile your code. The regex line gives me an error. Invalid escape sequence
It works now, but I need to change "int ints[parts.length];" to "int ints[] = new int[parts.length];"
Usually, I don't like to use regex. it is very confusing for me.

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.