package com.codegym.task.task06.task0610;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
ConsoleReader class
*/
public class ConsoleReader {
public static String readString() throws Exception {
//write your code here
BufferedReader bf =new BufferedReader(new InputStreamReader(System.in));
String ab=bf.readLine();
return ab;
}
public static int readInt() throws Exception {
//write your code here
BufferedReader bf =new BufferedReader(new InputStreamReader(System.in));
String ab=bf.readLine();
int ak=Integer.parseInt(ab) ;
return ak;
}
public static double readDouble() throws Exception {
//write your code here
BufferedReader bf =new BufferedReader(new InputStreamReader(System.in));
String au=bf.readLine();
double ak=Double.parseDouble(au) ;
return ak;
}
public static boolean readBoolean() throws Exception {
//write your code here
BufferedReader bf =new BufferedReader(new InputStreamReader(System.in));
String ab=bf.readLine();
//int ak=Integer.parseInt(ab) ;
//boolean t=false;
boolean t =ab.equals(true)?true:false;
return t;
}
public static void main(String[] args) {
}
}
help...what wrong with my code,stucked with the last requirement
Under discussion
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
yehuda b
11 January 2020, 21:02
I think in addition to the other answer, the boolean method has to read a boolean which is true or false, that's it. so you would have to write: boolean t = Boolean.parseBoolean(bf.readLine());
return t;
just like you did with the int with a little less code.
0
Antali András
11 January 2020, 12:56
Hi,
I think the fail is here, in the readBoolean() method :
boolean t =ab.equals(true)?true:false;
It seems to me, you just accidentally forgot the " signs around the "true" string in the brackets.
BR,
András
0
yehuda b
11 January 2020, 21:02
I don't know if a String can equal a boolean even if it has brackets, don't you have to parse the string to a boolean ?
0
Antali András
11 January 2020, 21:37
The variable "ab" is a String in this code. So one can only compare it to an other String, that is the reason the "" signs are needed in the brackets. If the "true" String and the one that the user typed in are equals, then true value will be assigned to the boolean t variable (the first value after the ? operator). Else the false (the second value after the : op). So there is no conversion.
boolean t = ab.equals("true") ? true : false;
Your version also works, moreover it is a better one!
0
yehuda b
11 January 2020, 21:44
"TRUE" :-) and thanks for clarifying, I realize now that your 1st reply addressed my question, my mistake.....
0