import java.util.Scanner;
import java.io.*;
public class coh5{
public static void main(String[]args)throws FileNotFoundException{
//Initiate a scanner object to read file
Scanner readFile= new Scanner(new File("products.txt"));
//Initiate a scanner object to re read file and extract product info
Scanner getProduct = new Scanner(new File("products.txt"));
//User input
Scanner userNum = new Scanner(System.in);
System.out.println("---Menu--- Enter 1,2,3,4,5 for action");
System.out.println("1.Add items");
System.out.println("2. View cart");
System.out.println("3.Clear cart");
System.out.println("4.check out");
System.out.println("5.EXIT");
int choice = userNum.nextInt();
while (choice !=5){
if (choice <1 || choice >5){
System.out.println("Please enter a number 1 - 5");
choice =userNum.nextInt();
}
else if(choice == 1){
String proName="";
double proPrice=0.0;
while(readFile.hasNextLine()){
proName=readFile.next();
proPrice=readFile.nextDouble();
System.out.println("product:"+proName);
System.out.println("price:"+proPrice);
System.out.println(" ");
}
else if(choice == 2){
System.out.println("Display cart");
}
//clear cart
else if(choice ==3){
//FIXME
System.out.println("clear");
}
//check out
else if(choice==4){
System.out.println("check out");
}
}
}
}
}
else with out if error. Line 37. the if choice 2 why doesn't it work? how to fix?
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
3 March 2021, 07:08
The closing brace before else if(choice == 2){ refers to while(readFile.hasNextLine()){ and not to else if(choice == 1){
+1