Hey guys,
I know, I did this task overly complicated :) But I wanted to try to get the variable name and value from every variable displayed in the url. And it´s working! :):):) Therefore I wrote a getVariableValue method, which returns an Object. This object is either a Double or a String. Now I was just modifying the code to satisfy the requirement that only the value of the obj variable should be displayed.
Object obj;
for (String s : allVariables) { //for each of those variables, print out the value
if (s.contains("obj")) {
obj = getVariableValue(s); //save the value within an object
if (obj instanceof Double) alert((double) obj); //check if object is a Double, then print out the value(object) as a Double
else if (obj instanceof String && !((String) obj).isEmpty()) alert((String)obj); //check if object is a String and if it is not empty, then print out the value(object) as a String
}
}
public static Object getVariableValue(String s) {
char currentChar;
boolean reading = false; //are we reading the value of the variable already?
String StringOfValue = "";
Double value;
for (int i = 0; i < s.length(); i++) {
currentChar = s.charAt(i);
if (currentChar == '=') { //value starts after =
reading = true; //we start reading....
continue; //but don´t want to include the = in the value
}
if (reading) {
StringOfValue += currentChar; //construct value from the current chars
}
}
try {
if (StringOfValue.contains(".")) //if the value contains a "."
return Double.parseDouble(StringOfValue); //try to read string as a double, if not possible, catch exeption and read as string
else return StringOfValue; //if the value is an int or String (no double with "."), return a string
} catch (Exception e) {
return StringOfValue;
}
}
The only requirement not fulfilled is: "The program should call the alert method with the double parameter if the obj parameter can be correctly converted to a double." And I have no idea why. Do you know, what I have to adjust? Thanks a lot in advance!package 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 {
ArrayList<String> allVariables = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String inputURL = reader.readLine();
getVariablesWithValue(inputURL, allVariables); //first put all variables with their values and = into an arraylist
for (String variable : allVariables) { //for each of those variables, print out the name without value
System.out.print(getVariableName(variable) + " ");
}
System.out.println();
Object obj;
for (String s : allVariables) { //for each of those variables, print out the value
if (s.contains("obj")) {
obj = getVariableValue(s); //save the value within an object
if (obj instanceof Double) alert((double) obj); //check if object is a Double, then print out the value(object) as a Double
else if (obj instanceof String && !((String) obj).isEmpty()) alert((String)obj); //check if object is a String and if it is not empty, then print out the value(object) as a String
}
}
//http://codegym.cc/alpha/index.html?obj=dadfasd4&name=Amigo
}
public static void getVariablesWithValue(String url, ArrayList<String> allVariable) {
char currentChar;
ArrayList<Character> oneVariable = new ArrayList<>(); //ArrayList, which saves each character of one variable (will be cleared, once reading a variable is finished)
boolean reading = false; //is the programm reading a variable right now?
String s = "";
for (int i = 0; i < url.length(); i++) {
currentChar = url.charAt(i); //get the current Character
if (currentChar == '?') { //? is signal to start reading first variable
reading = true; //now it is reading
continue; //don´t read the ?, continue to the next character
}
if (currentChar == '&' || i == url.length()-1) { //& or the end of the string are signals to stop reading this variable
if (i == url.length()-1) oneVariable.add(currentChar); //if last char of string reached, add to arraylist nevertheless
for (int j = 0; j < oneVariable.size(); j++) {
s = s + oneVariable.get(j); //create a string from all chars saved in oneVariable
}
allVariable.add(s); //add variable to ArrayList allVariables
s = "";
oneVariable.clear();
reading = true; //continue reading, but don´t read &
continue;
}
if (reading) { //if we are reading right now, add current Char do oneVariable
oneVariable.add(currentChar);
}
}
}
public static String getVariableName(String s) {
char currentChar;
String variable = "";
for (int i = 0; i < s.length(); i++) {
currentChar = s.charAt(i);
if (currentChar == '=') break; //name of variable stops before =
else {
variable = variable + currentChar; //construct variable name from
}
}
return variable;
}
public static Object getVariableValue(String s) {
char currentChar;
boolean reading = false; //are we reading the value of the variable already?
String StringOfValue = "";
Double value;
for (int i = 0; i < s.length(); i++) {
currentChar = s.charAt(i);
if (currentChar == '=') { //value starts after =
reading = true; //we start reading....
continue; //but don´t want to include the = in the value
}
if (reading) {
StringOfValue += currentChar; //construct value from the current chars
}
}
try {
if (StringOfValue.contains(".")) //if the value contains a "."
return Double.parseDouble(StringOfValue); //try to read string as a double, if not possible, catch exeption and read as string
else return StringOfValue; //if the value is an int or String (no double with "."), return a string
} catch (Exception e) {
return StringOfValue;
}
}
public static void alert(double value) { //prints out the value of the double
System.out.println("double: " + value);
}
public static void alert(String value) { //prints out the value of the String
System.out.println("String: " + value);
}
}