Easy getline class for getting the input from user
AndreiL :: Info space :: Info space :: Java Space
Page 1 of 1
Easy getline class for getting the input from user
Hi. Recently I "ported" myself from C++ to Java, and tried to do some simple console applications. What surprised me was that the cout and cin methods from C++ in Java were much more complicate, in the sense of writing and implementing (cin in this case). So then I created a class which would help me get the input from the user. This method takes the whole stream from the input (meaning also the empty space character ' ') and returns it in a Java String. Also you can modify this so that it would return a Java StringBuffer class. Anyway, hope it helps those "porting" themselves from C++ to Java.
It's a public static method of the Cstdin Class, so you must call it by String line=Cstdin.getline();
It's a public static method of the Cstdin Class, so you must call it by String line=Cstdin.getline();
1 | import java.io.BufferedReader; |
- Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Cstdin {
public static String getline(){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
str = in.readLine();
return (str);
}
catch (IOException e) {
return "";
}
}
}
AndreiL :: Info space :: Info space :: Java Space
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum