Java: using a string to stop a while loop and it is consumed by following scanner input
One way to not pass the data from the sc
scanner is to create a new Scanner Object. Lets call this one scan
. This way when you refer to scan
it won't know the sc
scanners input from earlier.
In your if
statement, you would refer to the scan
scanner instead of the sc
scanner.
The reason the data was being passed from the while
loop to your if
statement is because they both referred to the same object and when you called sc.hasNext()
it read the last thing that you inputted into the console.
Java program that reads user input until 0 is entered, also makes a few calculations (evens/odds, average, etc.)
When reading the first numbers to populate large and small we only need to do that once. And without re-reading the numbers with in.nextInt()
because that will eat up the next inputs that are entered, which was probably causing the not terminating at zero error.
while ((input = in.nextInt()) != 0) {
if (counter == 0)
small = large = input;
if (input != 0)
sum = input + sum;
counter++;
if (input > large)
large = input;
if (input < small)
small = input;
if (input % 2 == 0)
even = even + 1;
else
odd = odd + 1;
}
Java - Try/Catch NumberFormatException uses a former value?
I don't follow everything that you're saying, but these 2 lines (from within your catch
block) look problematic...
spaceTestAndConvert(numInput.trim());
return Double.parseDouble(numInput.trim());
You are calling the spaceTestAndConvert
function recursively, but throwing away the value. I don't understand why you would call it and not be interested in the value.
The second line is also a mess. You so carefully surround the first call to Double.parseDouble()
with try/catch, but then you call it again within your catch
block. If the second Double.parseDouble()
generates a NumberFormatException
, it will not be caught.
Q : Doing multiple loops and multiple if-statements and if-else-statements || RENTAL CAR CALCULATOR PROJECT
Here you go, i modified it a bit and put the whole thing in a while loop (while (cus != 0)) now it is working perfectly, try this code and do let me know if you have questions
package inter;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Inter {
public static void main(String []args){
int count=0;
int days;
int cus = 10; // added
double DailyFee=0, NontaxTotal, CarType, Total,FullTotal=0;
boolean F1 = false, F2 = false;
Scanner in=new Scanner(System.in);
while (cus != 0) {
while (true) {
System.out.println("If there are any customer press 1 else press 0");
try {
cus=in.nextInt();
if (cus == 0 || cus == 1) {
break;
} else {
System.out.println("Number must be either 1 or 0");
}
} catch (InputMismatchException ex) {
System.out.println("Invalid entry");
in.next();
}
}
if(cus == 1) {
while(F1 == false) {
F1 = true;
count++;
System.out.print("What vehical would you like to rent?\n");
System.out.println("Enter 1 for an economy car");
System.out.println("Enter 2 for a sedan car");
System.out.println("Enter 3 for an SUV");
try {
CarType = in.nextInt();
if (CarType <= 0 || CarType >= 4) {
System.out.print("Number must be 1-3\n");
System.out.println("Please enter 1 for an economy car");
System.out.println("Enter 2 for a sedan car");
System.out.println("Enter 3 for an SUV");
F1 = false;
} else {
if (CarType == 1) {
F1 = true;
DailyFee=31.76;
} else if(CarType == 2) {
F1 = true;
DailyFee=40.32;
} else if(CarType == 3) {
F1 = true;
DailyFee=47.56;
}
while (F2 == false) {
F2 = true;
try {
System.out.print("Please enter the number of days rented. (Example; 3) : ");
days = in.nextInt();
if (days <= 0) {
System.out.println("Number of days must be more than zero");
F2 = false;
} else {
//days = in.nextInt();
double x=days;
NontaxTotal = (DailyFee * x);
Total = (NontaxTotal * 1.06);
FullTotal+=Total;
}
} catch(InputMismatchException ex) {
System.out.println("Answer must be a number");
F2 = false;
in.next();
}
}
F2 = false;
}
} catch (InputMismatchException ex) {
F1 = false;
System.out.println("Answer must be a number");
in.next();
}
}
F1 = false;
}
}
System.out.println("Count of customers : " + count);
System.out.printf("Total of the Day : $ %.2f", FullTotal);
}
}
Related Topics
Getting Versioncode and Versionname from Google Play
How to Solve Maven 2.6 Resource Plugin Dependency
How to Center Crop a Background Image of Linear Layout
How to Do Post Request With Raw Data Via Spring Rest Template
How to Junit Test That Two List<E> Contain the Same Elements in the Same Order
In Java How to Extract Exact Domain Name from Url
Disable Spring Security Config Class for @Webmvctest in Spring Boot
Source Folder Is Not on the Java Build Class Path, Creating Java Package
Most Efficient Solution for Reading Clob to String, and String to Clob in Java
How to Get My Loops to Display Horizontally Instead of Vertically
Org.Hibernate.Tool.Schema.Spi.Commandacceptanceexception: Error Executing Ddl Via Jdbc Statement
How to Save a File Downloaded With Httpclient into a Specific Folder
Ora-00942 Sqlexception With Hibernate (Unable to Find a Table)
Converting Excel File (Xlsx) into Json
Determine the Number of Pages in a Pdf File
Spring Boot - Json Object Array to Java Array
Spring Data Repository @Query - Update and Return Modified Entity