package com.codegym.task.task06.task0610;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class ConsoleReader {
//Scanner sc = new Scanner(System.in);
public static String readString() throws Exception {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
return s;
}
public static int readInt() throws Exception {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
return i;
}
public static double readDouble() throws Exception {
Scanner sc = new Scanner(System.in);
double d = sc.nextDouble();
return d;
}
public static boolean readBoolean() throws Exception {
Scanner sc = new Scanner(System.in);
boolean b = sc.nextBoolean();
if (b == true) {
System.out.println("true");
} else if (b == false) {
System.out.println("false");
}
sc.close();
return b;
}
public static void main(String[] args) {
//Scanner sc = new Scanner(System.in);
}
}
I could do this task but my question is: I thought normally we need to invoke the Scanner once in the main method but didn't work and then I tried in the first line of ConsolReader class but didnt work. I thought once we imported the Scanner class it is reachable anywhere within the package. So not
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Wojciech
1 October 2020, 10:15useful
Declare in class ConsoleReader with default package modifier and define before use in any method or add in constructor.
+1