Trouble setting a Parameter - java

Im confused as to how I allow only numbers 1-4? Im not sure if there is a term for this, i think its parameter
THE CODE IM QUESTIONING IS THE 3RD TO LAST LINE
private void validatePositiveNumber() {
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Please enter a positive number: ");
while (!scanner.hasNextInt()) {
String input = scanner.next();
System.out.printf("\"%s\" is not a valid number.\n", input);
}
number = scanner.nextInt();
} while (number < 4);
System.out.printf("You have entered a positive number %d.\n", number);
}

Use while (number > 4 || number < 1); This disallows anything outside of the range.

Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Please enter a positive number: ");
while (!scanner.hasNextInt()) {
String input = scanner.next();
System.out.printf("\"%s\" is not a valid number.\n", input);
}
number = scanner.nextInt();
} while (number > 4 || number < 1);
System.out.printf("You have entered a positive number %d.\n", number);

Related

Why is my very simple while loop repeating one extra time?

Here is my while loop. The program sums up integers until a negative number is input. At that point the loop should break and it should print "Goodbye". However it is adding the negative number each time before it says goodbye. Im not sure what is going wrong here. Please help?!
import java.util.Scanner;
public class While {
public static void main(String[] args)
{
int input = 5;
int sum = 0;
while(input >= 0)
{
System.out.println("Please enter a positive integer: ");
Scanner in = new Scanner (System.in);
input = in.nextInt();
sum = sum + input;
System.out.println("Running total: " + sum );
}
System.out.println("Goodbye!" );
}
Test:
Please enter a positive integer:
5
Running total: 5
Please enter a positive integer:
10
Running total: 15
Please enter a positive integer:
-1
Running total: 14
Goodbye!
I do not want to get the return value of 14, it should simply say Goodbye!
You need to use the break keyword. Your loop will always finish so the check on the while only happens after you've added the negative number. You could change to this:
while(true)
{
System.out.println("Please enter a positive integer: ");
Scanner in = new Scanner (System.in);
input = in.nextInt();
if(input <0){
break;
}
sum = sum + input;
System.out.println("Running total: " + sum );
}
Or this:
while(input >= 0)
{
System.out.println("Please enter a positive integer: ");
Scanner in = new Scanner (System.in);
input = in.nextInt();
if(input <0){
break;
sum = sum + input;
System.out.println("Running total: " + sum );
}
}
Or to avoid if statements entirely if needed (though that isn't the point of loops):
while(input >= 0)
{
sum = sum + input;
System.out.println("Please enter a positive integer: ");
Scanner in = new Scanner (System.in);
input = in.nextInt();
System.out.println("Running total: " + sum );
}
Here was my solution:
while(input >= 0)
{
sum = sum + input;
System.out.println("Running total: " + sum );
System.out.println("Please enter a positive integer: ");
Scanner in = new Scanner (System.in);
input = in.nextInt();
}
System.out.println("Goodbye!" );
}
by calculating the sum at initialization and before the first integer is entered. It appears to work the way I want now. Thanks for your help.

I am trying to get user input until user enter 0 and then show maximum value among them in JAVA

I am a beginner to java and don't know to write a program using loops that prompt the user to enter a number till user does does not enter 0.
When user enter 0 then system should display MAX number among user input
QUE 2
Write a program to ask the user to enter a sequence of numbers (double type). The numbers are separated by the return key (and give a prompt for each enter). The user ends the sequence by entering a 0. Then output the maximum number of all the entered numbers. Here is an example (the part in italic is the user’s input): Please enter a sequence of numbers, separated by return, and then end this sequence with a 0 at last: 25
Next number: 35.6
Next number: 112.112
Next number: 0
The maximum among your enters is 112.112
import java.util.Scanner;
public class Q3
{
public static void main(String[] args[])
{
double n;
// double i;
double MAX=0;
System.out.println("Please Enter the number: ");
Scanner Kb = new Scanner(System.in);
n = Kb.nextDouble();
if(n>0){
System.out.println("Please Enter the number: ");
n = Kb.nextDouble();
return;
}
else if(n==0) {
if (MAX>0){
MAX=n;
return ;
}
}
return;
}
}
Keep track of the max and each time a user inputs a number check if it is greater than that max
import java.util.Scanner;
public class Q3 {
public static void main(String... args) {
double max = 0;
System.out.println("Please enter the number: ");
Scanner kb = new Scanner(System.in);
double number = kb.nextDouble();
while (number != 0) {
if (max < number) {
max = number;
}
number = kb.nextDouble();
}
System.out.print("The max is " + max);
}
}
Since zero is the terminal character then negative input can be essentially ignored and the initial value of max as zero is acceptable.
Note that nextDouble can throw an InputMismatchException if the user decides to give you input that can not be parsed to a double.
using Collections.max ,
List<Double> doubleList = new ArrayList<>();
System.out.println("enter a number :");
Scanner kb = new Scanner(System.in);
while (kb.hasNext() ) {
double input = kb.nextDouble();
if(input == 0){
break;
}
doubleList.add(input);
}
System.out.println("Max Value Entered : " + Collections.max(doubleList));
Scanner sc = new Scanner(System.in);
double max = 0;
while(true){
double number = sc.nextDouble();
if(max<number){
max = number;
}
else if(number==0){
break;
}
}
System.out.print(max);

In my code in while loop when the input number is negative I want to prompt the user again to enter the number, but I got error doing so

import java.util.Scanner;
public class SumOfNumber
{
public static void main(String[] args)
{
// Declare variable
int inpNum;
int total;
// Prompt the user to input a number;
Scanner keyboard = new Scanner(System.in);
// Prompt user for positive nonzero integer.
System.out.println("Enter a positive nonzero integer");
// Convert the input into int.
inpNum = keyboard.nextInt();
// Assign value to total.
total = 0;
Want to prompt the user if the input number is less than zero
// Method to find sum of integers.
while( inpNum < 0 )
{
System.out.println("Negative integer entered.\n"
+ "Please enter a positive integer");
When I excetue the program, I got an error: cannot find symbol for
inpNum = keyboard.nextInt();
inpNum = Keyboard.nextInt();
}
while( inpNum >= 1 )
{
total += inpNum;
inpNum--;
}
System.out.println("Sum of the positive integers is " + total);
}
}
Change
inpNum = Keyboard.nextInt(); //to
inpNum = keyboard.nextInt();
Your problem is in the line inpNum = Keyboard.nextInt(); you declared the scanner askeyboard and using it as Keyboard capitalized.

Java: How to use a sentinel value to quit a program when the user wants

The only thing i am missing is to use a sentinel value, like zero, to quit the loop when the user wants, even without enter any gussing.
import java.util.Scanner;
import java.util.Random;
public class RandomGuessing {
//-----------------------------------------------------------------------------------------------
//This application prompt to the user to guess a number. The user can still playing until
//guess the number or want to quit
//-----------------------------------------------------------------------------------------------
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
Random rand = new Random();
int randNum = rand.nextInt(100) + 1;
System.out.println(randNum);
System.out.println("\t\tHi-Lo Game with Numbers\t\t\n\t Guess a number between 1 and 100!!!\n");
String ans;
int attemptsCount = 0;
do {
System.out.print("Guess the number: ");
int input = scan.nextInt();
while(input != randNum){
attemptsCount++;
if(input < randNum){
System.out.println();
System.out.print("low guessing\nEnter new number: ");
input = scan.nextInt();
}
else{
System.out.println();
System.out.print("high guessing\nEnter new number: ");
input = scan.nextInt();
}
}
System.out.println();
System.out.println("Congrats!!! You guess right\nAttempts: "+attemptsCount);
System.out.println();
System.out.print("You want to play again (yes/no): ");
ans = scan.next();
randNum = rand.nextInt(100) + 1; //generate new random number between same above range,
//if the user wants to keep playing.
}while (ans.equalsIgnoreCase("yes"));
}
}
Here's a simple approach:
// inside do loop
System.out.print("Guess the number (-1 to quit): ");
int input = scan.nextInt();
if (input == -1) {
break;
}
Try using this code for exiting totaly:
System.exit(0);

How to sum up the resultes in a while loop while centain input will end the while loop?

I have a situation in java;
I would like to ask the user to put in some numbers & have a total of those numbers. However if the user enter a negative number it will end the loop;
currently I have a while loop as below;
double sum = 0;
double Input = 0;
System.out.println("Please enter the numbers (negative to end)")
System.out.println("Enter a number");
Scanner kdb = new Scanner(System.in);
Input = kdb.nextDouble();
while (Input > 0)
{
System.out.println("Enter an income");
Input = kdb.nextDouble();
sum = Input;
}
However it does not do the job. If the user put in 40,60,50, and -1 the correct result should be 150; my loop result in 109.
Please help!
Many thanks!
Jackie
double sum = 0;
double Input = 0;
System.out.println("Please enter the numbers (negative to end)")
System.out.println("Enter a number");
Scanner kdb = new Scanner(System.in);
Input = kdb.nextDouble();
while (Input > 0)
{
sum += Input;
System.out.println("Enter an income");
Input = kdb.nextDouble();
}
I recommend variable names not to start with upper case letters.
You should check for Input > 0 before doing sum += Input.
This should work!
double sum = 0;
double Input = 0;
boolean Adding= true;
System.out.println("Please enter the numbers (negative to end)");
Scanner kdb = new Scanner(System.in);
while(Adding == true)
{
System.out.print("Enter a number: ");
Input = kdb.nextDouble();
if(Input > 0)
{
sum+= Input;
}
else
Adding = false;
}
System.out.println("Your sum is: " + sum);
The first input value was overwritten by the second one since the the sum was done only at the end of the loop.
**double sum = 0;
double Input = 0;
System.out.println("Please enter the numbers (negative to end)");
System.out.println("Enter a number");
Scanner kdb = new Scanner(System.in);
Input = kdb.nextDouble();
while (Input>0)
{
sum+= Input;
System.out.println("Enter an income");
Input = kdb.nextDouble();
}
System.out.println(sum);
}**
The output is:
Please enter the numbers (negative to end)
Enter a number
40
Enter an income
50
Enter an income
60
Enter an income
-1
150.0

Categories

Resources