I am filling a Double Array with a for-loop, the loop runs for how many laps the user enters on line 4. However, as I demonstrate in my picture included:
I enter 3 laps and the question prompts 3 times however my data validation eats one of the inputs I am looking for.
How do I fix this? I feel like its something very simple.
import java.util.Scanner;
public class JK_FINALPRAC1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Time-Trial Statistics Program\n");
System.out.println("This program will ask you to input split-times for each lap or length of a race. The ");
System.out.println("program will then calculate some basic statistics from the split-times.\n");
System.out.print("Enter the number of laps or lengths in the race: ");
int arraySize = sc.nextInt();
Double[] lapArray = new Double[arraySize];
System.out.println("Now input the elapsed time (split time) in seconds for each lap/length of the race.");
int index = 1;
double currentNum = 0.0;
for(int i=0;i<arraySize;i++){
System.out.print("Time for lap or length #" + index + ": ");
currentNum = sc.nextDouble();
if(currentNum > 0 && currentNum < 61){
lapArray[i] = currentNum;
index++;
}else if(currentNum<0 || currentNum >60){
System.out.println("Invalid input! Time must be between 0 and 60.");
}
}
}
double currentNum = 0.0;
while (index < arraySize) {
System.out.print("Time for lap or length #" + (index + 1) + ": ");
currentNum = sc.nextDouble();
if (currentNum > 0 && currentNum < 61) {
lapArray[index] = currentNum;
index++;
} else if (currentNum < 0 || currentNum > 60) {
System.out.println("Invalid input! Time must be between 0 and 60.");
}
}
Since you're running for-loop, i is getting incremented every time (even if your validation fails).
It's very simple: in order to be sure you have inserted a double without eat a lap, put your scanner input line inside a while-loop
do{
double x = scanner.nextDouble();
}while(x < 0 || x > 60);
This will not afford for-loop increasing counter, unless the condition of do-while is false.
In order to print validation, too:
double x = scanner.nextLine();
while( x < 0 || x > 60){
//print message
//scanner again
}
Related
So it's time to come back to the users of stackoverflow for help again on an assignment.
I'm supposed to fill an array with a size of 10 with doubles given from the user. The entered numbers are supposed to be grades so they have to be in the range of 0 - 100. Anything out of the range (more, less, or incorrect characters) are not saved to the array and the user is prompted to try again at the given index of the array. After either the array is filled to the max value or the user presses enter to skip, the program is supposed to return the grades entered in order and display the average.
public static final SIZE = 10;
Scanner userIn = new Scanner(System.in);
double[] grades = new double [SIZE];
int counter = 0;
int counterList = 1;
boolean exit = false;
String sTemp = "";
double gradeNum = 0;
double avrGrade = 0.0;
double sum = 0;
while((counter < SIZE) && (!exit)) {
System.out.println("Enter grade " + counterList +
", or press enter to quit.");
sTemp = userIn.nextLine();
counterList++;
if(sTemp.length() < 1) {
exit = true; //ending for loop
} else {
gradeNum = Double.parseDouble(sTemp);
grades[counter] = gradeNum;
counter++;
} //else statement
} //end of while loop
counterList = 1;
System.out.println("You entered " + counter + " grades total.");
for(int i = 0; i <= counter; i++) {
System.out.println("Your grade " + counterList + " is " + grades[i]);
counterList++;
}
for(int i = 0; i < grades.length; i++)
sum += grades[i];
System.out.println("The average grade is: " + sum / grades.length);
So I finished taking my user's input and calculating the average but I'm having trouble setting the range and not saving the invalid inputs. I also feel like once I started to struggle I got really sloppy and there might be stuff going on in there that's not needed. Let me know anything helps!
An example of what the program should output given different scenarios
You can trim down your code. There are variables that you don't require. Explanations after the code.
import java.util.Scanner;
public class GradeAvg {
private static final int SIZE = 10;
public static void main(String[] args) {
Scanner userIn = new Scanner(System.in);
double[] grades = new double [SIZE];
int counter = 0;
String sTemp = "";
double sum = 0;
while(counter < SIZE) {
System.out.println("Please enter grade " + (counter + 1) + ": ");
sTemp = userIn.nextLine();
if (sTemp.isEmpty()) {
break;
}
try {
grades[counter] = Double.parseDouble(sTemp);
}
catch (NumberFormatException xNumberFormat) {
System.out.println("That wasn't a valid percentage, I need a number between 0 - 100. Try again.");
continue;
}
if (grades[counter] < 0 || grades[counter] > 100) {
System.out.println("That wasn't a valid percentage, I need a number between 0 - 100. Try again.");
continue;
}
sum += grades[counter];
counter++;
} //end of while loop
for (int i = 0; i < counter; i++) {
System.out.println("grade " + (i + 1) + ": " + grades[i]);
}
System.out.println();
System.out.println("number of valid grades entered: " + counter);
System.out.println();
System.out.println("average: " + sum / counter);
}
}
After accepting the user input, first check if the user simply pressed <ENTER> without entering a value. I used method isEmpty(), of class java.lang.String, to do this check. If the user simply pressed <ENTER>, you need to exit the while loop. This is what break does. So no need for boolean variable exit.
Variable counter is all you need to both keep track of how many grades have been entered and which index of array grades needs to be assigned a value, so no need for variable counterList.
If an invalid value is entered, continue skips the rest of the while loop and starts a new loop iteration. Think of it as a sort of goto that jumps back to the statement:
while (counter < SIZE)
You can assign a value directly to an element in array grades so no need for variable gradeNum.
You can update variable sum inside the while loop so no need for the extra for loop in order to calculate the average. By the way, your calculation of the average is incorrect since you are dividing by the size of array grades and not by the actual number of grades that were entered. Adding up all the elements in array grades still gives you the correct sum since all elements of the array are implicitly initialized to 0 (zero).
I changed what the program displays on the screen so as to match your example of what the program should output given different scenarios.
Here is the output according to the sample you provided.
Please enter grade 1:
A
That wasn't a valid percentage, I need a number between 0 - 100. Try again.
Please enter grade 1:
100
Please enter grade 2:
Bob
That wasn't a valid percentage, I need a number between 0 - 100. Try again.
Please enter grade 2:
41.5
Please enter grade 3:
-7
That wasn't a valid percentage, I need a number between 0 - 100. Try again.
Please enter grade 3:
grade 1: 100.0
grade 2: 41.5
number of valid grades entered: 2
average: 70.75
I made some adjustments:
final int SIZE = 10;
Scanner userIn = new Scanner(System.in);
double[] grades = new double [SIZE];
int counter = 0;
String sTemp = "";
double gradeNum = 0;
double sum = 0;
while(counter < SIZE) {
boolean err = false;
System.out.println("Enter grade " + (counter + 1) +
", or press enter to quit.");
sTemp = userIn.nextLine();
try {
gradeNum = Double.parseDouble(sTemp);
} catch(NumberFormatException ex)
{
err = true;
}
if(sTemp.length() < 1) {
break; //ending the loop
} else if(gradeNum <= 100 && gradeNum >= 0 && !err)
{
grades[counter] = gradeNum;
counter++;
} else
{
System.out.println("That wasn't a valid percentage, I need a number between 0 - 100. Try again.");
}
} //end of while loop
userIn.close(); //closing the Scanner
System.out.println("You entered " + counter + " grades total.");
for(int i = 0; i < counter; i++) {
System.out.println("Your grade " + (i + 1)+ " is " + grades[i]);
}
for(int i = 0; i < counter; i++) {
sum += grades[i];
}
System.out.println("\nNumber of valid grades entered: " + counter + "\n");
System.out.println("The average grade is: " + sum / counter);
}
This seems to work, I made some corrections (like SIZE declaration, took out the println from the last loop, replaced exit with a break statement, ...). The most relevant changes are the input checks and the average grade calculation (I used counter instead of grades.length because it is always 10). The try/catch checks if the input string contains only numbers.
(sorry for bad english, just ask if something isn't clear)
Very new to Java (please don't laugh)and trying to do a project that asks user for a number and will keep track of the highest and lowest number when the user enter a sentinel value. My error is the I cannot get max and min values to update when going through the while loop.
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a double number, or 'q' to quit");
double currentNum = 0.0;
double maxNum=0.0;
double minNum=0.0;
int count = 0;
while(sc.hasNextDouble())
{
currentNum = sc.nextDouble();
count++;
System.out.println(count);
if(count ==1)
{
minNum = currentNum;
maxNum = currentNum;
System.out.println("Please enter a double number, or 'q' to quit");
}
else if(count!=1){
if (currentNum > maxNum)
{
currentNum = maxNum;
System.out.println("You are in currentNum > maxNum");
System.out.println(maxNum);
System.out.println("Please enter a double number, or 'q' to quit");
System.out.println(maxNum);
}
else if (currentNum < minNum)
{
currentNum = minNum;
System.out.println("You are in currentNum < minum");
System.out.println("Please enter a double number, or 'q' to quit");
}
}
}
System.out.println("Min num " + minNum);
System.out.println("Max num " + maxNum);
}
}
I think you got your logic mixed up.
When you get a number larger or smaller than maxNum you want to update maxNum to currentNum not the other way around.
Also the count == 1 if condition is redundant.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a double number, or 'q' to quit");
double currentNum = 0.0;
double maxNum=(double)Integer.MIN_VALUE;
double minNum=(double)Integer.MAX_VALUE;
int count = 0;
while(sc.hasNextDouble()) {
currentNum = sc.nextDouble();
count++;
System.out.println(count);
if (currentNum > maxNum){
maxNum = currentNum;
System.out.println("You are in currentNum > maxNum");
System.out.println("Maximum : "+maxNum);
}
else if (currentNum < minNum){
minNum = currentNum;
System.out.println("You are in currentNum < minum");
System.out.println("Minimum : "+minNum);
}
System.out.println("Please enter a double number, or 'q' to quit");
}
System.out.println("Min num " + minNum);
System.out.println("Max num " + maxNum);
}
}
One thing that I noticed is that you're comparing double variables. Generally it's not a good idea comparing floating point values because they have precision problems. You should use long or int instead. If you really don't have any other choice and have to use double, you should checkout this question: How to compare two double values in Java?
As pointed out in comment, you're updating currentNum instead of maxNum/minNum. So your else-if block for updating min-max value should look like this:
if (currentNum > maxNum) {
System.out.printf("You are in currentNum(%d) > maxNum(%d)", currentNum, maxNum);
//currentNum = maxNum; // <- Wrong, doesn't update maxNum; currentNum would be updated in next loop iteration anyway
maxNum = currentNum; // <- What you should be doing
}
else if (currentNum < minNum) {
System.out.printf("You are in currentNum(%d) < minNum(%d)", currentNum, minNum);
//currentNum = minNum; // <- Wrong, doesn't update minNum
minNum = currentNum; // <- What you should be doing
}
I need to create a loop where the user can input any amount of numbers between 1-4 and then I calculate the average. Typing 0 will end the program and calculate the average. Any value greater than 4 or less than 0 should not count and ask the user to input the value again. I'm stuck on the last part. I'm not sure if the while loop is the correct loop to use either. Thanks for any help
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double sum = 0;
double count = 0;
while(input.hasNextInt()) {
int num = input.nextInt();
if (num == 0)
break;
if (num > 4)
System.out.println("Invalid number");
sum += num;
count += 1;
}
System.out.println("Average: " + sum/count);
}
You will always hit the lines:
sum += num;
count += 1;
Because the code just drops through from the second if statement.
The following would work - note the else if and else blocks will only be executed when the first if drops through:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double sum = 0;
double count = 0;
while(input.hasNextInt()) {
int num = input.nextInt();
if (num == 0) {
break;
}
else if (num > 4) {
System.out.println("Invalid number");
}
else {
sum += num;
count += 1;
}
}
System.out.println("Average: " + sum/count);
}
For class I am writing a program that accepts a stream of positive integers. The program will stop accounting for numbers and compute the average once a negative number is entered into the console.
My issue is, I keep writing while loops that account for the negative entered. How do I stop this? I have tried the following
do{
if (number < 0){
break;
} else if ( number >= 0) {
number = input.nextDouble();
DivideBy++;
sum+=number;
}
}while (number >= 0);
When entering 1.1, 1.9, 3, and -1, the program prints 1.25, when the correct answer is 2.
Here is another example of what I have tried:
do {
number = input.nextDouble();
DivideBy++;
sum+=number;
}while (number >= 0);
Any help is appreciated.
Try this:
Scanner input = new Scanner(System.in);
double number = 0;
double sum = 0;
int divideBy = 0;
double avg = 0;
while(number >= 0) {
System.out.println("Enter number: ");
number = input.nextDouble();
if(number < 0){
break;
}
sum+=number;
divideBy++;
avg = sum/divideBy;
}
System.out.println(avg);
Your if statement should come after the number has been read from the keyboard :]
Try this out, it worked for me:
while(number >= 0){
total = number + total;
} //Your average logic here//
What you are doing now is checking whether the number is negative after you incremented DivideBy and added the number to the sum.
You can easily fix this by undoing what you did after a negative number.
// outside the loop:
sum -= number;
DivideBy--;
Then you will get the correct result.
Or, you can do it like this - check negative first, then add it to sum.
Scanner s = new Scanner(System.in);
int num = s.nextInt();
int count = 0;
int sum = 0;
while (num > 0) {
sum += num;
count++;
num = s.nextInt();
}
System.out.println(sum / count);
Read the number before the if condition. Try this.
do {
number = input.nextDouble();
if (number < 0) {
break;
} else if (number >= 0) {
DivideBy++;
sum += number;
}
} while (number >= 0);
I have this code:
import java.util.Scanner;
public class PositiveNegative { public static void main(String[] args) {
int numbers, plus = 0, minus = 0;
int count = 0;
double total = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
while(numbers != 0)
{
total += numbers;
if(numbers > 0)
plus++;
if(numbers < 0)
minus++;
}
System.out.println("The number of positives is: " +plus);
System.out.println("The number of negatives is: " +minus);
System.out.println("The number of total is: " +total);
}
}
The problem with is that I try to run it and type the numbers but it does nothing. I want it so that when you type 0 it stops taking numbers and starts processing the code. What should I do?
Try this:
import java.util.Scanner;
public class PositiveNegative {
public static void main(String[] args) {
int numbers = 0, plus = 0, minus = 0;
double total = 0;
do{
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
numbers = Integer.valueOf(scan.nextLine());
total += numbers;
if (numbers > 0)
plus++;
if (numbers < 0)
minus++;
}
while (numbers != 0);
System.out.println("The number of positives is: " + plus);
System.out.println("The number of negatives is: " + minus);
System.out.println("The number of total is: " + total);
}
}
Put your Scanner in the while loop so that everytime loop start it will ask for User input.
You need to update numbers or your loop will run for ever. And I recommend using braces (and an else). Something like,
System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
while (numbers != 0) {
total += numbers;
if (numbers > 0) {
plus++;
} else if (numbers < 0) {
minus++;
}
System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
}
Alternatively, you could use a do-while loop. Then you only need one copy of the prompt. Like,
do {
System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
total += numbers;
if (numbers > 0) {
plus++;
} else if (numbers < 0) {
minus++;
}
} while (numbers != 0);
You have to modify numbers each time to make it work in your while.
So, in your existing code, just comment out numbers = scan.nextInt(); and use below--
// numbers = scan.nextInt(); //comment out this call
while ((numbers = scan.nextInt()) != 0) {
....
this will give you desired output--
Enter an integer (0 to quit): 9
4
-9
1
0
The number of positives is: 3
The number of negatives is: 1
The number of total is: 5.0