Do while with try catch JAVA validation [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a trouble figuring out how to solve this issue when it just stops when I call this method when I input letters and other types. What is the problem with my code? Can anyone help? Thanks in advance!
Here is my code:
public int selectOption(int maxRange, String sourceType) throws IOException
{
do
{
try
{
userInput = input.nextInt();
}//end try
catch(Exception e)
{
validInput=false;
input.next();
}//end catch
if (userInput<1 || userInput>maxRange)
{
MovieHouse.clearScreen();
System.out.println("Select from the options only!");
loadHeader();
if(sourceType.equals("main"))
MovieHouse.homeMenu();
else if(sourceType.equals("movie menu"))
loadMovieMenu();
}//end if
}while(userInput<1 || userInput>maxRange || validInput==false);
return userInput;
}//end selectOption

"input letters" - Does it mean normally the program take digits and you are providing char as input.
Not clear what is your issue.

Related

String cannot be converted to char, how to fix it? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
It says "incompatible types: String cannot be converted to char"
How to fix it?
Here is the example.
This is your solution :
public class NewMain {
public static void main(String args[]) throws ParseException {
Scanner s = new Scanner(System.in);
char yes;
do {
System.out.println("Hi");
yes = s.next().charAt(0);
} while (yes == 'Y'); // if u enter 'Y' the it will continue
}
}
To exit enter any thing other then 'Y'

Why isn't this recursion working in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to calculate the factorial of the integer user inputs, but it does not return anything. Why?
Thanks.
import java.util.Scanner;
`class App {
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
int recurse = factorial(n - 1);
int result = n * recurse;
return result;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer to calculate its factorial: ");
int users = input.nextInt();
factorial(users);
}
}
The problem in your code is that you are have not giving a print statement for displaying factorial of the number entered. Just returning a value will not print it. If you are working in BlueJ environment, you can only use the code by directly executing the method factorial. Thank You.
The problem in your code is that , you are have not given a print statement for >displaying factorial of the number entered.

Try-Catch Exception handling [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
try {
System.out.println("how many times");
rollnumber = scanner.nextInt();
nigh=2;
}catch (Exception e){
System.out.print("invalid. re-enter");
}
}while (nigh==1);
It keeps printing out infinity "invalid re-enterhow many times"
You get into an infinite loop because scanner.nextInt(); does not consume characters from the input on error. Change the catch clause as follows to make it work:
try {
... // Your code
} catch (Exception e){
System.out.print("invalid. re-enter");
scanner.nextLine();
}

How to check if the input is an integer? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am taking input as integer using Scanner class. like this:in.nextInt();
I need to prompt "wrong input" if user has entered any floating point number or character or string.
How can i accomplish this ?
nextInt() can only return an int if the InputStream contains an int as the next readable token.
If you want to validate input, you should use something like nextLine() to read a full String, and use Integer.parseInt(thatString) to check if it is an integer.
The method will throw a
NumberFormatException - if the string does not contain a parsable integer.
As I mentioned in the comments, try using the try-catch statement
int someInteger;
try {
someInteger = Scanner.nextInt();
} catch (Exception e) {
System.out.println("The value you have input is not a valid integer");
}
Put it in a try-catch body.
String input = scanner.next();
int inputInt 0;
try
{
inputInt = Integer.parseInt(input);
} catch (Exception e)
{
System.out.println("Wrong input");
System.exit(-1);
}

Array Cannot Find Symbol-Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am still new to coding but I get a cannot find symbol
I don't know much about arrays but I have been looking on the internet but I cannot find why
import java.util.Scanner;
public class Alphabet {
public static void main(String[] args) {
String[] alpha = new String[26];
alpha[0] = a;
System.out.println("PLease enter a number from 0-25:");
Scanner input = new Scanner(System.in);
int userInput = input.nextInt();
}
}
alpha[0] = a;
Variable a is not declared, that's why you are getting the error.
Generally Cannot find symbol error occurs when compiler not able to resolve the symbol.
Here a can not be resolved by the compiler in your code at line
alpha[0]= a;
So you are getting error.
Declare it like this
String a = "hello";
alpha[0] = a;
or
alpha[0] = "a";

Categories