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;
}
Related
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 2 years ago.
Improve this question
My requirement is to check divisibility of the input by 7, for the various test cases and I have written this code but it is throwing me NumberFormat Exception
class Solution{
int isdivisible7(String num){
// code her
long i= Long.parseLong(num);
// to convert string into long
if(i%7==0)
return 1;
else
return 0;
}
}
How can I handle the exception and return the result for any (both valid and invalid) input ?
If the input num is not number, then it will throw NumberFormatException, so you just have to catch it. Also, function names should be in camel case. And finally, it's better to make the function return boolean rather then int of values 0 and 1.
boolean isDivisibleBy7(String num){
try {
long i = Long.parseLong(num);
return i % 7 == 0;
} catch (NumberFormatException e) {
// print some error message if you want
System.out.println("You haven't passed number");
return false;
}
}
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.
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
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);
}
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();