Hello,
Can someone give me an example where is a mistake with condition?
package pl.codegym.task.task15.task1527;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Parser żądań
*/
public class Solution {
public static void main(String[] args) throws IOException {
//tutaj wpisz swój kod
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String keyInput = reader.readLine();
// String keyInput = "http://codegym.cc/alpha/index.html?lvl=15&view&name=Amigo"; //Example 1.
// String keyInput = "http://codegym.cc/alpha/index.html?obj=3.14&name=Amigo"; //Example 2.
// Step_1 spliting url by "?"
String[] array = keyInput.split("\\?");
// Step_2 geting second item from array and dividing it by "&", all from that item puting to List
String s1 = array[array.length - 1];
List<String> list = Arrays.asList(s1.split("&")); // put it to list
// System.out.println("\n === step_2 === " + list);
// Step_3 if item in List don't have '=' add it.
for (int i = 0; i < list.size() - 1; i++) {
if (!hasSum(list.get(i))) { // I am using method hsSum() to check it.
list.set(i, list.get(i) + "= ");
}
}
// System.out.println(" === step_3 === " + list);
// Step_4 dividing positions form list by "=", and add it to new list2.
ArrayList<String> list2 = new ArrayList<>();
for (int j = 0; j <= list.size() - 1; j++) {
String s2 = list.get(j);
String[] table = s2.split("=");
for (int k = 0; k <= table.length - 1; k++) {
list2.add(table[k]);
}
}
// System.out.println(" === step_4 === " + list2);
// Step_5 Spliting list2 to two separate lists, obj and values.
ArrayList<String> obj = new ArrayList<>();
ArrayList<String> value = new ArrayList<>();
for (int j = 1; j < list2.size(); ) {
obj.add(list2.get(j - 1));
value.add(list2.get(j));
j += 2;
}
// System.out.println("\n === Step_5 === check ojb list: " + obj); // task req.
// System.out.println(" === Step 5 === check value list: " + value);
// Step_6 Output
// System.out.println("\n === step 6 === output:");
//task req output
System.out.println(obj);
for (int j = 0; j < obj.size(); j++) {
if (obj.get(j).equals("obj")) {
if (isDouble(value.get(j))) {
double x = Double.parseDouble(value.get(j));
alert(x);
} else alert(value.get(j));
}
}
}
static boolean hasSum(String s) {
boolean x = false;
for (char c : s.toCharArray()) {
if (c == '=') {
x = true;
}
}
return x;
}
static boolean isDouble(String s) {
try {
Double doub = Double.parseDouble(s);
} catch (NumberFormatException e) {
return false;
}
return true;
}
public static void alert(double value) {
System.out.println("double: " + value);
}
public static void alert(String value) {
System.out.println("String: " + value);
}
}