Hello everyone! For the last couple of days, I have been trying to solve this task... I know I'm really close, but I can't figure this out:
Why is this code not capturing name as a next token?! Any hint?
EDITED: Test passed, but when try to pass the task...oh wonder, no pass...
NEW CODE:
Scanner scan = new Scanner(System.in);
String url = scan.nextLine();
// String url = "http://codegym.cc/alpha/index.html?lvl=15&view&name=Amigo";
String subStr = url.substring(url.indexOf("?") + 1);
Pattern p = Pattern.compile("(\\w+)");
Pattern doublPatt = Pattern.compile("([0-9]+\\.[0-9]+)");
Matcher doublMatch = doublPatt.matcher(subStr);
Matcher m = p.matcher(subStr);
double d = 0;
String validToken = "";
String doubleToken = "";
List<Object> obj = new ArrayList<Object>();
while(m.find()){
validToken = m.group();
if(doublMatch.find()){
try{
doubleToken = doublMatch.group(1);
d = Double.parseDouble(doubleToken);
if(!((d % 1) == 0)){
obj.add(d);
}
}catch(Exception e){
}
}
char ch = validToken.toCharArray()[0];
if(Character.isLowerCase(ch)) obj.add(validToken);
}
for(Object o : obj){
if(!(o instanceof Double)) System.out.print(o + " ");
}
for(Object o : obj){
if(o instanceof Double) {
System.out.println();
alert(d);
}
}
package com.codegym.task.task15.task1527;
/*
Request parser
*/
import java.util.*;
public class Solution {
public static void main(String[] args) {
//write your code here
Scanner scan = new Scanner(System.in);
// String url = scan.nextLine();
String url = "http://codegym.cc/alpha/index.html?lvl=15&view&name=Amigo";
String subStr = url.substring(url.indexOf("?") + 1);
System.out.println(subStr);
StringTokenizer strToken = new StringTokenizer(subStr, "&=");
double d = 0;
String validToken = "";
List<Object> obj = new ArrayList<Object>();
while(strToken.hasMoreTokens()){
validToken = strToken.nextToken();
System.out.println(validToken);
char ch = validToken.toCharArray()[0];
if(Character.isLowerCase(ch)) obj.add(validToken);;
try{
d = Double.parseDouble(strToken.nextToken());
if(!((d % 1) == 0)){
obj.add(d);
}
}catch(Exception e){
}
}
for(Object o : obj){
if(!(o instanceof Double)) System.out.print(o + " ");
}
for(Object o : obj){
if(o instanceof Double) {
System.out.println();
alert(d);
}
}
}
public static void alert(double value) {
System.out.println("double: " + value);
}
public static void alert(String value) {
System.out.println("String: " + value);
}
}