java.lang.NumberFormatException: For input string: “” [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
The question is duplicate but I can't find a proper solution so please help me out with this,
I have to convert the credit card number(from edittext) to int.
Credit card number like : "3333 3333 3333 3333"
I removed white space using String removeWhiteSpace = cardNumEt.getText().toString().replace(" ", "");
Than converted to int like :
try
{
int nIntFromET = Integer.parseInt(removeWhiteSpace);
}
catch (NumberFormatException e)
{
Log.e("exptn",e.toString());
}
but unfortunately it's giving me exception :
java.lang.NumberFormatException: For input string: "3333333333333333"

The int type in Java can be used to represent any whole number from -2147483648 to 2147483647.
Your value is above the maximum positive integer.
But you can use instead long and BigInteger
class Scratch {
public static void main(String[] args) {
try
{
int nIntFromET = Integer.parseInt("2147483647");
}
catch (NumberFormatException e)
{
System.out.println(e);
}
}
}

Related

java.lang.Error am i missing something in the code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
This is just a simple query if anyone knows why I am facing this error I would appreciate the help.
The problem is simple java if else if else loop problem .
so I tried to make syntax as correct as possible.
The code here below is the one I am executing.
public class ifelseloop {
public static void main(String[] args){
// int a = 20;
// int b = 20;
String city = "Delhi";
if (city == "madras") {
System.out.println("the city is not delhi its madras");
}else if(city == "chennai") {
System.out.println("the city is not delhi its chennai");
}else if(city == "bartar") {
System.out.println("the city is bartar");
}else {
System.out.println(city);
}
}
}
and the error that I am getting is this.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at ifelseloop.main(ifelseloop.java:2)

Why I can't add elements to HashMap (Java) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
I don't know what I'm doing wrong... I have the next code:
public class Administrator {
private static Map<Integer, Professor> professors = new HashMap<Integer, Professor>();
private static void addProfessor(){
Scanner scanner = new Scanner(System.in);
System.out.println("\nADD PROFESSOR");
System.out.print("\tId: ");
Integer id = scanner.nextInt();
System.out.print("\tName: ");
String name = scanner.next();
System.out.print("\tLast name: ");
String lastname = scanner.next();
System.out.print("\tInit date (AAAA/MM/DD) (including slashes): ");
LocalDate date = LocalDate.parse(scanner.next());
if(professors.put(id, new Professor(id, name, lastname, date)) != null) {
System.out.println("Professor added successfully.");
} else {
System.err.println("We can't add the professor.");
}
}
}
And when I call the addProfessor method, the error "We can't add the professor." is printed. I don't know why the element is not added to the HashMap. There isn't any exception on my console, so I don't have any way to know whats wrong.
When calling Map.put(), the previous entry for the given key in the map is returned. If put() returns null, then the entry is added, but it didn't overwrite anything.

Cant print the value of scan.nextIn(); [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
I'm trying to delete the user integer input from an array. However, I can't get the value of the input.
while(keepGoing)
{
while (!scan.hasNextInt() )
{
scan.next();
System.out.print('\n'+"Choose a valid number: ");
}
int unitA = scan.nextInt();
if (unitA < 1)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else if (unitA > 14)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else
lengthValue.remove(unitA);
scan.close();
keepGoing = false;
}
//lengthValue.remove(int unitA);
System.out.println(unitA);
In my opinion, you forget to press "Enter" after entering input. A scanner can read the input if only you press the "Enter" key. Your solution seems correct to me. I was able to properly run it on my PC.
You can find a similar question here:
How to use Scanner to accept only valid int as input

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.

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);
}

Categories