Having trouble with if statement [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
System.out.println("Aside from that how old are, because I'm 15.");
String age = input.nextLine();
next line has a problem but don't know why on netbeans. I think it has to do something with the tweenty.
if(age > 20){
System.out.println("Were not really the same age.");
}

You are comparing a String object to an integer.
First convert the String to an integer, then compare the integers.
To do this, use Integer.parseInt(age). This will return an integer that is equal
to the number written in the string.
String age = input.nextLine();
int ageInteger = Integer.parseInt(age);
if (ageInteger > 20) {
System.out.println("...");
}

You can do it 2 ways
you can convert string to integer :
System.out.println("Aside from that how old are, because I'm 15.");
String age = input.nextLine();
int ageInInt = Integer.parseInt(age);
if(ageInInt > 20){
System.out.println("Were not really the same age.");
}
Or 2. you can read an in from input :
System.out.println("Aside from that how old are, because I'm 15.");
int ageInInt = input.nextInt();
if(ageInInt > 20){
System.out.println("Were not really the same age.");
}

Change:
String age = input.nextLine();
to:
int age = input.nextInt();

Related

I have to obtain the user's birth month in int form and store as variable for print out later [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Here is the code I have.
//input
System.out.println("Using numbers, in what month were you born? ");
String **userMonth** = input.nextLine();
// also tried int **userMonth** = input.nextInt();
It does not work either way.
userMonth will not "activate?" (sorry noob and don't know all terms)
When tried to call later in program error:
if (userMonth < 3) {
System.out.println("Your vacation home is a van down by a river!");
}
//userMonth cannot be resolved to variable
Very odd that your variables are showing up as **varName** instead of varName.
First use:
int userMonth = 0; // initialize variable.
userMonth = input.nextInt();
But it does not consume the newline ('ENTER').
Therefore, immediately after, consume the newline character via:
input.nextLine();
Also make sure Scanner was declared as
Scanner input = new Scanner(System.in);
Don't forget to close the scanner at the end of program
input.close();

Java scanners - skip all "non-int" and "less than 0" inputs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to loop through and skip all inputs that are less or equal 0 or non integers.
I have written this code, but it doesn't work and I don't understand why.
while(!userInput.hasNextInt() || userInput.nextInt() <= 0) {
userInput.next();
}
return userInput.nextInt();
I think you need to change a logic a bit, for example:
while (userInput.hasNext()) {
if (userInput.hasNextInt()) {
int intValue = userInput.nextInt();
if (intValue > 0) {
return intValue;
}
}
userInput.next();
}
Because when you're trying to verify that int value is less or equal zero userInput.nextInt() <= 0, you're actually getting the value.
So, if it's not true, you will go to this line return userInput.nextInt();, but cursor will already be on the next value.
You can check whether there is any user input or not. If there is take the input and then process it else continue with the loop.
while(userInput.hasNextInt()){
int a=userInput.nextInt();
if(a>=0){
return a;
}else
continue;
}

How to clarify whether the user's input was a binary number? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was required to make a code that accepted a binary number (1's and 0's) and then counted how many ones were in that binary number. My code fulfills this purpose.
The second part of the exercise is this: if the user enters a number that is NOT binary, I must output that there is an error and keep prompting the user until they give a binary number.
Can someone show me how to incorporate this? I have tried several times but cannot make it click. Thanks! Here is my code.
import java.util.Scanner;
public class NewClass
{
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in);
int i = 0, count = 0;
String number;
System.out.println("Please enter a binary number.");
number = scan.next();
String number1 = "1";
while ((i = number.indexOf(number1, i++)) != -1) {
count++;
i += number1.length();
}
System.out.println("There are "+ count + " ones in the binary number.");
}
}
You already know how to find all of the 1's; you should be able to do something similar to find all of the 0's.
If the sum of those two counts is not the length of the string, there must be at least one illegal character in it.
If you use the parseInt method you can do
Integer.parseInt("1100110", 2) returns 102
or in your case
int intVal = Integer.parseInt(number, 2);
as per the javadocs
Throws:
NumberFormatException - if the String does not contain a parsable int.

How to get user input as string that has only zeros? in JAVA [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In my application user can enter number between 0 to 999
However, whether he enter "0" or "00" or "000" should understood and treated differently.
All the functions that in know convert the input into single zero "0" before putting it into the variable.
private static void AddAmountChart1D (){
String str_pana;
int int_pana, Amount;
Scanner UserInput = new Scanner(System.in);
System.out.print("Enter the game pana : ");
str_pana = UserInput.nextLine(); /* This line should treat "0", "00" & "000" as different types of strings.
Presently it make all three as "0"
System.out.print("Enter the Amount : ");
Amount = UserInput.nextInt();
int_pana = get_int_pana(str_pana);
ChartAmount1D[int_pana] = ChartAmount1D[int_pana]+Amount;
}
Your purpose is not that much clear to me.
But if you use
UserInput.nextLine();
this will be treated as a String so it'll return the zeros like "0", "00" or "000".
Further if you want the value to be processed as a number simply use
int number = Integer.parseInt(str_pana);
and use the number.
Edit
Please try this and check the results for "0", "00" or "000".
if (str_pana.equals("0")) {
System.out.println("Zero");
}else if (str_pana.equals("00")) {
System.out.println("Zero-Zero");
}else if (str_pana.equals("000")) {
System.out.println("Zero-Zero-Zero");
}
Actually my point is to check the zeroes so that you can set the integer values in the if clause and convert them to respective integer values to be used later.
Read whole line.
Use function
public boolean startsWith("0")
From String class
If it doesn't return true - just parse.
Else count zeros

How can I input only integer and this integer will be only ending with zero? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to validate input values to pass only integers that are divisible by 10. The code below is failing.
public static void main(String args[]) {
Scanner scan =new Scanner(System.in);
ArrayList<Integer> liste = new ArrayList<Integer>(); // I have filled my array with integers
int x=scan.nextInt();
int y=x%10;
do{
if(y==0){
liste.add(x);}
else if(y!=0){
System.out.println("It is not valid"); continue;
}
else
{System.out.println("Enter only integer"); continue;
}
}while(scan.hasNextInt()); }
System.out.println(liste);
System.out.println("Your largest value of your arraylist is: "+max(liste));
You're calling scan.nextInt() twice. Each time you call it, it will read another int from the input. Thus, if your input was something like
10
5
13
then the 10 would pass the scan.nextInt()%10==0 check, and then 5 would be added to the list. Store the result of scan.nextInt() in a variable first, so the value won't change.
Instead of
if(scan.nextInt()%10==0){
liste.add(scan.nextInt());}
do
int num = scan.nextInt();
if(num%10 == 0){
liste.add(num);
}

Categories