How to display an error message if user input is not in the correct form? - input

So far in my code I prompt the user to enter a positive integer representing the number of people they are inviting to an event.
I already have an if statement to return an error message if the user input is a negative value.
But how do I return an error message if the user enters a character, string, or double?
Whenever I test this by entering a letter, the terminal just displays the following message:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Cookies.main(Cookies.java:15)

Catch the exception then display the message
such as
try
{
int i = Integer.parseInt(input);
}
catch(NumberFormatException e)
{
System.out.println("You didn't enter an integer");
}

Related

How to show firebase auth error messages different in UI

I am using the firebase auth now I want to show a different message in UI for every error message
You have to check for specific error messages in your catch block and add custom handling.
You don't mention the language you're working in (and I'm not familiar with all of the different libraries), but C# will throw a FirebaseAuthException containing the property AuthErrorCode which is an enum representing the error. You could check that in, say, a switch statement to get the required message.
try {
userRecord = await _FirebaseAuth.GetUserByEmailAsync(email, token)
.ConfigureAwait(false);
}
catch (FirebaseAuthException ex) {
if (ex.AuthErrorCode == AuthErrorCode.UserNotFound) {
DisplayError($"Error retrieving user record for {email}");
}
}

Web3 Revert Message

I am using web3 over the Rinkeby test network for my solidity project. I am trying to get the exact revert message, using the error object.
try{
await contract.methods.acceptBattle(id).send({
from: address,
value:val
});
return '';
}
catch(e){
console.log(e.message);
}
After the code is running in the catch block, I am getting the output in the following format:
execution reverted: This battle isn't exist.
0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001b426174746c65206e7
56d6265722069736e27742065786973742e0a0000000000
Is there a way to get only the message itself (in the first row) and not the address coming after?
As I saw in Do all Ethereum networks return the revert reasons as a “message” field?
and since I am running over the Rinkeby test network, the address supposed to be part of the data field of the error and not part of the message.
This is the relevant code for the revert message:
function acceptBattle(uint256 battle_id) public payable{
Battle storage bate=battleInfo[battle_id];
require(bate.amountBet>0, "Battle number isn't exist.\n");
require(bate.creator!=msg.sender, "Impossible to fight against yourself.");
require(bate.creator==bate.opponent, "This battle is closed, opponent already exist.");
require(msg.value==bate.amountBet, "Betting value isn't as specified for this battle.");
bate.opponent=msg.sender;
}

JOptionPane.showInputDialog Changing the ´cancel´ Button

So I'm trying to get a number input from a player in an RPG/survival type game, using showInputDialog to present the options, prompting the user to input a number. My problem is, I get a fatal error if they press cancel.
This is my current code:
String typeReader;
do{
typeReader = JOptionPane.showInputDialog(options);
}while(typeReader.isEmpty());
if (typeReader.isEmpty())
typeReader = "0";
charType = Integer.parseInt(typeReader);
and this is the error I get:
Exception in thread "main" java.lang.NullPointerException
at Game.main(Game.java:66)
Java Result: 1
BUILD SUCCESSFUL (total time: 14 seconds)
Ideally, if a user presses cancel the program would just read it as an empty String:
typeReader = "";
Can anyone help?
OK, you seem to be pretty new to this ;-)
First, you won't need the loop. Just write
String typeReader = JOptionPane.showInputDialog(options);
If the user clicks "Cancel", typeReader will be null afterwards. null is not an object, so you cannot call isEmpty() on it and you get the NullPointerException. Instead, you should check for null:
if (typeReader != null) {
...
}
You should read the Oracle tutorial on dialogs and maybe also the Javadoc.

Syntax error on token "boolean", # expected

I want to check whether an alert message is present. For that i tried the code,
public boolean IsAlertPresent()
{
try
{
driver.switchTo().alert();
return true;
}
catch (NoAlertPresentException Ex)
{
return false;
}
}
But, error messages are shown in boolean and IsAlertPresent(). Boolean shows a message 'Syntax error on token "boolean", # expected' and IsAlertPresent() shows a message 'IsAlertPresent cannot be resolved to a type'.
I believe you have defined the method IsAlertPresent() inside another method. This is not allowed in Java. Define the method separately any your error will go away.
If you are writing the IsAlertPresent method in a jsp file, declare it in the <%! section rather than the <% section.
That way the method is part of the jsp class

Wait for something of variable time to complete before continuing method

I need to be able to halt a method while it waits for user input.
I tried using a while (true) loop to check to see if a boolean was set indicating that the operation had completed, but as I predicted, it made the application non-responsive.
How do you halt and resume a method after a variable has been set, without calling the method again, and without making the entire application non-responsive.
Here's basically what the program does
openFile method called
openFile method determines whether file has a password
if it does, display an alert to the user requesting the password
Here's the problem, halting the method until the password has been entered.
Any help appreciated.
Why do you want to halt the method? You could use a delegate. You present the alert view and register the delegate for the alert view. The delegate registers for the didDismissAlertViewWithButtonIndex method and acts according to the button. If the password was entered correctly and the OKAY button was tapped, you can continue with your process.
You can't. You need to split it up into two methods, one that does the initial check and then prompts for the password, then the second that uses the password to get the file.
You may need something like below.
class Program
{
static void Main(string[] args)
{
}
void YourFileReadMethod()
{
string password = string.Empty;
bool isPasswordProtected = CheckFileIsPasswordProtected();
if (isPasswordProtected)
{
Form passwordFrm = new Form();//This is your password dialogue
DialogResult hasEntered = DialogResult.No;
do
{
hasEntered = passwordFrm.ShowDialog();
password = (hasEntered == DialogResult.Yes) ?
passwordFrm.passwordTxt//password property/control value;
: string.Empty;
} while (hasEntered != DialogResult.Yes);
}
ReadFileMethod(password);
}
private void ReadFileMethod(string password)
{
//use the password value to open file if not String.Empty
}
bool CheckFileIsPasswordProtected()
{
//your logic which decides whether the file is password protected or not
//return true if password is required to open the file
return true;
}
}