user13881639
user13881639

Reputation:

Code is printing error though I am sure I did this right. What is wrong?

Here's the code I have a question about:

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";
        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");
    }
} 

When I run this particular code, I get this error:

Exception in thread "main" java.util.NoSuchElementException: No line found 
  at java.util.Scanner.nextLine(Scanner.java:1540)
  at project.PostOffice.main(PostOffice.java:8)

What is wrong? I included the conditions to match the initialized variables and print that variable when the conditions return true. The system does print "User input:" but beneath it, the error pops up. I got rid of what could have become the problem but it doesn't change. I'm pretty sure I made the code pretty explicit and simple to read for the system but it keeps complaining. I see no other way of fixing this problem. Is it something with my for loops? Or is it something else?

Upvotes: 1

Views: 49

Answers (1)

sunrise
sunrise

Reputation: 404

(Apologies, this should really go in the comments but my reputation isn't yet high enough for me to do that).

I have tried running your code on IntelliJ and it seems to work for me (I used inputs of 4, 4, 4. Is your error coming up immediately after the User input line prints, or are you able to put in some figures first? If the latter, what is your input?

Not related to your problem, but something which may make your code easier to read: Where a variable name is made of multiple words, try using camel case (capitalise the first letter in each word apart from the first, e.g. largeevelope becomes largeEnvelope). I do like that you haven't used cryptically abbreviated names :)

Upvotes: 0

Related Questions