When I run the code it returns the following message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at com.codegym.task.task15.task1527.Solution.main(Solution.java:34)
Edit: I changed lines 31-35 to:
for (int i = 0; i<parts2.length; i++){
String[] parts3 = parts2[i].split("=");
if (parts3.length ==1)
parameters.add(parts3[0]);
else{
parameters.add(parts3[0]);
values.add(parts3[1]);
}
}
The code seems to run fine for the following input:
http://codegym.cc/alpha/index.html?obj=3.14&name=Amigo
But the same conditions are still not passingpackage com.codegym.task.task15.task1527;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Request parser
*/
public class Solution {
public static void main(String[] args) throws IOException {
//write your code here
//read url
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String url = reader.readLine();
//find parameters
String[] parts = url.split("\\?");
//split parameters
String[] parts2 = parts[1].split("&");
ArrayList<String> parameters = new ArrayList<>();
ArrayList<String> values = new ArrayList<>();
//split parameters and values
for (int i = 0; i<parts2.length; i++){
String[] parts3 = parts2[i].split("=");
parameters.add(parts3[0]);
values.add(parts3[1]);
}
//display parameters
for (int i = 0; i<parameters.size(); i++){
System.out.print(parameters.get(i)+ " ");
}
System.out.print("\n");
//check if value is double or String
for (int i = 0; i < values.size(); i++){
double d = 0;
if (isDouble(values.get(i))==true){
d = Double.parseDouble(values.get(i));
alert(d);
}
else alert(values.get(i));
}
}
public static boolean isDouble(String str ){
try {
Double.parseDouble(str);
return true;
}catch( Exception e ){
return false;
}
}
public static void alert(double value) {
System.out.println("double: " + value);
}
public static void alert(String value) {
System.out.println("String: " + value);
}
}