Reputation: 488
Could you please recommend the best way how to:
input.readLine().isEmpty()
Integer.parseInt(input.readLine()) <10 && Integer.parseInt(input.readLine()) > 1
I do not know how to do this using fewer iterations:
System.out.println("Please enter N natural number of Strings :");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try {
if (input.readLine().isEmpty()) {
System.out.println("Dear User! \nYou did not specify number of Strings \nOr number of Strings greater then 10 \nOr number of Strings less then 1");
} else {
int numberOfString = Integer.parseInt(input.readLine());
stringArrayList = new ArrayList<String>(numberOfString);
Upvotes: 2
Views: 3383
Reputation: 9011
There are somethings that you need to consider to write safe code
1 - JavaDoc for BufferedReader.readLine
says
Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
So its important that you check for that too, but as you are using System.in as input stream, I think there wont be any issue.
Simplest way would be to read the string first:
System.out.println("Please enter N natural number of Strings :");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
do {
String line = input.readLine();
if(line != null) {
if(list.trim().equals("")) {
System.out.println("Dear User! \nYou did not specify number of Strings");
} else {
int numberOfString = 0;
try {
numberOfString Integer.parseInt(line);
if(numberOfString < 1 || numberOfString > 10) {
System.out.println("Dear User! \Number of string is either smaller then 1 or greater than 10");
} else {
stringArrayList = new ArrayList<String>(numberOfString);
//ReadString(input);
break;
}
} catch (NumberFormatException e) {
System.out.println("Dear User! \n" + line + " is not acceptable number");
}
}
}
} while(true);
Upvotes: 0
Reputation: 488
HUGE thank you!
my code looks :
public void initializeStringArr() {
System.out.println("Please enter N natural number of Strings :");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try {
String line = input.readLine();
if (line.isEmpty() || line == null) {
System.out.println("Dear User! \nYou did not specify number of Strings ");
} else {
int numberOfString = Integer.parseInt(line);
if(numberOfString<=1 || numberOfString>10 ){
System.out.println("Number of Strings greater then 10 \n" + "Or number of Strings less(equals) then 1");
} else {
stringArrayList = new ArrayList<String>(numberOfString);
for (int i = 0; i < numberOfString; i++) {
System.out.println("Please enter your string :");
BufferedReader newStr = new BufferedReader(new InputStreamReader(System.in));
stringArrayList.add(newStr.readLine());
}
}
Upvotes: 0
Reputation: 1785
You are using input.readLine() two times.So take it by storing in the string temp variable.
String temp=input.readLine();
if(Integer.parseInt(temp)>10) //or whatever you want
{
//put your code here
}
Upvotes: 0
Reputation: 48404
You are using input.readLine
twice:
else
statement, when you parse the int
Your first if
statement is always evaluated (except if you throw NullPointerException
that is :), but the value is never retrieved.
Therefore, if your first evaluation returns false
, your int
parsing will be based on the second line read by your input
.
Assign String line = input.readLine()
and reference line
in both cases.
Also remember to check for null
!
Upvotes: 0
Reputation: 1500055
You should call readLine()
once and use that input - otherwise you're checking a different line each time!
String line = input.readLine();
// readLine returns null for "end of input"
if (line == null || line.isEmpty()) {
// Report error
} else {
int number = Integer.parse(line);
if (number < 1 || number > 10) {
// Report error
} else {
// Handle success
}
}
Upvotes: 3