Reputation: 1
I have seen other answers but still after using multiple try catch blocks, my program terminates at the very first exception. I want it to except all four variable values and then handle any exception usign the flag i have kept in each catch block.
Here is my code :
import java.text.DecimalFormat;
import java.util.Scanner;
public class Brokerage2 {
public static void main(String[] args) {
float bro = 0, buy = 0, sell = 0;
int quant = 0, flag = 0;
double subtot, pro, bb, bst, sb, ssst, seltot, buytot, surcharge;
Scanner sc = new Scanner(System.in);
try {
bro = sc.nextFloat();
if (bro > 0.03 || bro < 0.0) {
flag = 1;
}
} catch (Exception e) {
flag = 1;
}
try {
buy = sc.nextFloat();
} catch (Exception e) {
flag = 1;
}
try {
sell = sc.nextFloat();
} catch (Exception e) {
flag = 1;
}
try {
quant = sc.nextInt();
} catch (Exception e) {
flag = 1;
}
bb = (buy * quant) * bro * 0.01;
bst = 0.1036 * bb;
buytot = bst + bb;
sb = sell * quant * bro * 0.01;
ssst = sell * quant * 0.00025;
seltot = sb + ssst + bst;
surcharge = ((buy * quant) + (sell * quant)) * 0.00006;
subtot = surcharge + buytot + seltot;
pro = (sell - buy) * quant;
if (flag == 1) {
System.out.println("Invalid Input");
} else if (pro > subtot) {
System.out.println("profit");
System.out.println(Double.parseDouble(new DecimalFormat("##.##").format(pro - subtot)));
} else if (subtot > pro) {
System.out.println("loss");
System.out.println(Double.parseDouble(new DecimalFormat("##.##").format(subtot - pro)));
}
}
}
Upvotes: 0
Views: 166
Reputation: 105
If the scanner receives invalid input it doesn't wait for input anymore. It isn't a good idea to use the same scanner for multiple inputs. Instead create a new scanner each time and it will wait for input at each one.
import java.text.DecimalFormat;
import java.util.Scanner;
public class brokerage2 {
public static void main(String[] args) {
float bro = 0, buy = 0, sell = 0;
int quant = 0, flag = 0;
double subtot, pro, bb, bst, sb, ssst, seltot, buytot, surcharge;
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter float: ");
bro = sc.nextFloat();
if (bro > 0.03 || bro < 0.0) {
flag = 1;
}
} catch (Exception e) {
flag = 1;
}
try {
sc = new Scanner(System.in);
System.out.println("Enter float: ");
buy = sc.nextFloat();
}
catch (Exception e) {
flag = 1;
}
try {
sc = new Scanner(System.in);
System.out.println("Enter float:");
sell = sc.nextFloat();
} catch (Exception e) {
flag = 1;
}
try {
sc = new Scanner(System.in);
System.out.println("Enter int: ");
quant = sc.nextInt();
} catch (Exception e) {
flag = 1;
}
bb = (buy * quant) * bro * 0.01;
bst = 0.1036 * bb;
buytot = bst + bb;
sb = sell * quant * bro * 0.01;
ssst = sell * quant * 0.00025;
seltot = sb + ssst + bst;
surcharge = ((buy * quant) + (sell * quant)) * 0.00006;
subtot = surcharge + buytot + seltot;
pro = (sell - buy) * quant;
if (flag == 1) {
System.out.println("Invalid Input");
}
else if (pro > subtot) {
System.out.println("profit");
System.out.println(Double.parseDouble(new DecimalFormat("##.##")
.format(pro - subtot)));
}
else if (subtot > pro) {
System.out.println("loss");
System.out.println(Double.parseDouble(new DecimalFormat("##.##")
.format(subtot - pro)));
}
}
}
Upvotes: 1