'Why is the second string returned from readLine() an empty string?
This is the code I used:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
String s1 = reader.readLine();
String s2 = reader.readLine();
String s3 = reader.readLine();
System.out.format("s1%s s2%b s3%s",s1,s2.equals(""),s3);
When I input the following text in the console, the second result is an empty string.
gg
ss
s1gg s2true s3ss
Anyone knows why this happens?
I tested the program in the IDEA on Mac.
Solution 1:[1]
After further investigations, it seems like that the odd behavior you're describing happens only when multiple read lines are stacked one after the other and executed via Intellij IDEA. Apparently, even regardless of the operating system being used, since you're experiencing this issue on a Mac whereas in my case it occurs on a Windows machine.
I've tested your exact same code on other IDEs too, like Netbeans and Visual Studio Code, ultimately also manually compiled it with javac and executed it via cmd, and none of these cases have shown the same behavior happening on Intellij IDEA.
EDIT
Apparently, as it has been pointed out in the comments, this is indeed an issue of Intellij IDEA version 2022.1.1. The link below also says that it will be fixed with the version 2022.1.2.
https://youtrack.jetbrains.com/issue/IDEA-293951/Console-readLine-skips-input-in-2022-1-1
So, to answer your question this is not a problem with the code you've written but rather an issue with the IDE you're using.
Solution 2:[2]
It is indeed a bug in Intellij IDEA console as mentioned in Dan's answer. This was already reported and fixed as per their tracker. Fix should be available in 2022.1.2 version.
https://youtrack.jetbrains.com/issue/IDEA-293951/Console-readLine-skips-input-in-2022-1-1
Until then you can try to use any command line terminal to test the program which is working perfectly fine as expected.
enter image description here - Test result with Build #IC-221.5591.52
Solution 3:[3]
- you have a typo into CheckIfDepostiedAmountLooksOkay, so compiler was unable to find method;
- You have a wrong case into
MAX_ALLOWED_Limit. C# is case-sensitive; - You set curly braces wrong before method
CheckIfDepostiedAmountLooksOkay, so this method was outside of the classAccount.
The right code would be:
public class Program
{
public static void Main()
{
var checkingAccount = new Account();
}
public class Account
{
private const double MAX_ALLOWED_LIMIT = 100000;
public string AccountOwnerName { get; set; }
private double _balance;
public double Balance
{
get { return _balance; }
}
public void DepositMoney(double amount)
{
CheckIfDepositedAmountLooksOkay(amount);
_balance += amount;
}
public void WithdrawMoney(double amount)
{
CheckIfBalanceLooksRight(amount);
_balance -= amount;
}
private void CheckIfBalanceLooksRight(double amount)
{
{
if (_balance == 0)
{
throw new Exception("Insufficient balance");
}
if (_balance < amount)
{
throw new Exception(
"You are trying to withdraw money more than your balance. Please lower your balance.");
}
if (Balance < 0)
{
throw new Exception("There is something seriously wrong here. Contact the bank teller.");
}
}
if (amount > MAX_ALLOWED_LIMIT)
{
throw new Exception(
"You cannot depost more than what's allowed daily. Please contact your bank agent for more infomation.");
}
if (amount <= 0)
{
throw new Exception(
"This does not look right. You may not deposit 0 or less amount of money to your account. The eroor is being reported to the bank. Thanks for your patience");
}
}
private void CheckIfDepositedAmountLooksOkay(double amount)
{
if (amount > MAX_ALLOWED_LIMIT)
{
throw new Exception(
"You cannot deposit more than what's allowed daily. Please contact your bank agent for more infomation");
}
if (amount <= 0)
{
throw new Exception(
"this does not look right. You may deposit 0 or less amount of money to your account. The error is being reported to your bank. Thanks for your patience.");
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Georgy Tarasov |
