I have the following code in a project called PostOffice:
JavaScript
x
package project;
import java.util.Scanner;
public class PostOffice {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("User input: ");
String input=sc.nextLine();
String[] determineinput=input.split(",");
String regular="Regular Postcard";
String large="Large Postcard";
String envelope="Envelope";
String largeenvenlope="Large Envelope";
String packagee="Package";
String largepackage="Large Package";
String unmailable="Unmailable";
if(3.5<Integer.parseInt(determineinput[0]) && Integer.parseInt(determineinput[0])<4.25){
if(3.5<Integer.parseInt(determineinput[1]) && Integer.parseInt(determineinput[1])<6){
if(0.007<Double.parseDouble(determineinput[2]) && Double.parseDouble(determineinput[2])<0.016){
System.out.println(regular);
}
}
}
else if(4.25<Integer.parseInt(determineinput[0]) && Integer.parseInt(determineinput[0])<6){
if(6<Integer.parseInt(determineinput[1]) && Integer.parseInt(determineinput[1])<11.5){
if(0.007<Double.parseDouble(determineinput[2]) && Double.parseDouble(determineinput[2])<0.016){
System.out.println(large);
}
}
}
else if(3.5<Integer.parseInt(determineinput[0]) && Integer.parseInt(determineinput[0])<6.125){
if(5<Integer.parseInt(determineinput[1]) && Integer.parseInt(determineinput[1])<11.5){
if(0.25<Double.parseDouble(determineinput[2]) && Double.parseDouble(determineinput[2])<0.5){
System.out.println(envelope);
}
}
}
else if(6.125<Integer.parseInt(determineinput[0]) && Integer.parseInt(determineinput[0])<24){
if(11<Integer.parseInt(determineinput[1]) && Integer.parseInt(determineinput[1])<18){
if(0.25<Double.parseDouble(determineinput[2]) && Double.parseDouble(determineinput[2])<0.5){
System.out.println(largeenvenlope);
}
}
}
else if(Integer.parseInt(determineinput[0])<6.125 || Integer.parseInt(determineinput[0])>24){
if(Integer.parseInt(determineinput[1])<11 || Integer.parseInt(determineinput[1])>18){
if(Double.parseDouble(determineinput[2])<0.25 || Double.parseDouble(determineinput[2])>0.5){
if((Integer.parseInt(determineinput[0])*2+Integer.parseInt(determineinput[1])*2)<=84){
System.out.println(packagee);
}
}
}
}
else if(Integer.parseInt(determineinput[0])<6.125 || Integer.parseInt(determineinput[0])>24){
if(Integer.parseInt(determineinput[1])<11 || Integer.parseInt(determineinput[1])>18){
if(Double.parseDouble(determineinput[2])<0.25 || Double.parseDouble(determineinput[2])>0.5){
if((Integer.parseInt(determineinput[0])+Integer.parseInt(determineinput[1]))>84 && (Integer.parseInt(determineinput[0])+Integer.parseInt(determineinput[1]))<130){
System.out.println(largepackage);
}
}
}
}
if((Integer.parseInt(determineinput[0])<3.5 || Integer.parseInt(determineinput[0])>24) && (Double.parseDouble(determineinput[2])<0.07 && Double.parseDouble(determineinput[2])>0.25) && (Integer.parseInt(determineinput[1])<3.5 && Integer.parseInt(determineinput[1])>18)){
System.out.println(unmailable);
}
}
}
But when I run it, it prints nothing. It just says<terminated>
and the output is blank. I tried fixing it and ran it several times over and over again, but the result is the same. Is there something wrong with my code? If so, what can I do to resolve it? And how would I prevent this from happening again?
Advertisement
Answer
It says <terminated>
because your input doesn’t match any if
condition, so the program terminates with no output. Add a simple print at the end of your main function to report when there is no match like:
JavaScript
System.out.println("Input doesn't match any result");
Also as mentioned in the comment, parse the input at the beginning and then process it. I refactored your code:
JavaScript
// ...
double height = Double.parseDouble(determineinput[0]);
double length = Double.parseDouble(determineinput[1]);
double thickness = Double.parseDouble(determineinput[2]);
if (3.5 < height && height < 4.25) {
if (3.5 < length && length < 6) {
if (0.007 < thickness && thickness < 0.016) {
System.out.println(regular);
}
}
} else if (4.25 < height && height < 6) {
if (6 < length && length < 11.5) {
if (0.007 < thickness && thickness < 0.016) {
System.out.println(large);
}
}
} else if (3.5 < height && height < 6.125) {
if (5 < length && length < 11.5) {
if (0.25 < thickness && thickness < 0.5) {
System.out.println(envelope);
}
}
} else if (6.125 < height && height < 24) {
if (11 < length && length < 18) {
if (0.25 < thickness && thickness < 0.5) {
System.out.println(largeenvenlope);
}
}
} else if (height < 6.125 || height > 24) {
if (length < 11 || length > 18) {
if (thickness < 0.25 || thickness > 0.5) {
if ((height * 2 + length * 2) <= 84) {
System.out.println(packagee);
}
}
}
} else if (height < 6.125 || height > 24) {
if (length < 11 || length > 18) {
if (thickness < 0.25 || thickness > 0.5) {
if ((height + length) > 84 && (height + length) < 130) {
System.out.println(largepackage);
}
}
}
}
if ((height < 3.5 || height > 24)
&& (thickness < 0.07 || thickness > 0.25)
&& (length < 3.5 || length > 18)) {
System.out.println(unmailable);
}
System.out.println("Input doesn't match any result");