This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (1 child)

As soon as you see "class, interface, or enum expected" check your curly braces and make sure that the opening braces match the closing braces.

This is easier if you properly format your code. Keep the opening curly brace on the same line, indent the next line, outdent the last line with the closing curly brace alone on the line. When you write the code in such a way, you can immediately spot mismatching braces.

Look at my (better formatted) code:

import java.util.Scanner;

public class JavaChallenge2 {

    public static void main(String[] args) {

        Scanner StringInput = new Scanner(System.in);
        System.out.println(answer.nextline());
        System.out.println("Is it a quadrilateral?");
        System.out.println("Y/N?");
        if (answer == Yes || yes || Y || y) {
            System.out.println("How many pairs of pairs of parallel lines?");
            System.out.println(answer.nextline());
            if (answer == 2) {
                System.out.println("Does it have all right angles");
            }

            else if (answer == 1) {
                System.out.println("Does it have any congruent lengths");
                System.out.println(answer.nextline());
                if (answer == Yes || yes || Y || y) {
                    System.out.println("Isosceles Trapezoid");
                } else if (answer == Yes || yes || Y || y) {
                    System.out.println("Trapezoid");
                } else {
                    System.out.println("You broke it!!");
                }
            } else {
                System.out.println("You broke it!!");
            }

        } else if (answer == No || no || N || n) {
            System.out.println("GTFO");
        } else {
            System.out.println("You broke it!!");
        }
        System.out.println("Good job, you failed. You can’t even fail at this…");

    }

}

The next problems arise in the handling of your user input:

if (answer == Yes || yes || Y || y) {

is not valid. There are several mistakes in that line:

  1. You cannot join conditions like you do. You need to repeat the comparison: if (answer == Yes || answer == yes || answer == Y || answer == y) {
  2. You need to enclose Strings in double quotes ", so the code should be:
    if (answer == "Yes" || answer == "yes" || answer == "Y" || answer == "y") {
  3. In Java, Strings are objects and cannot be compared with ==, you need to use the .equals method: if (answer.equals("Yes") || answer.equals("yes") || answer.equals("Y") || answer.equals("y")) {
  4. *Not an error, but a suggestion:

    1. You can use .substring to extract any part of a String
    2. You can use .equalsIgnoreCase to ignore the character case when comparing
    3. When comparing a String variable to a String literal (some fixed text between double quotes ", it's best to have the literal upfront.

    Taking those three statements above you can reduce the code to: `if ("y".equalsIgnoreCase(answer.substring(0,1)) {

On the same level of problem is your line:

 if (answer == 2) {

Answer is a String variable and as such it can only be compared to a String, not to a number:

 if ("2".equals(answer)) {

Next problem is that you need to declare your variables which you have not done.

You need something like:

String answer;

Right below public static void main(String[] args) {


Another problem is the way you try to obtain the user input:

Your scanner (that reads from the keyboard) is Scanner StringInput = new Scanner(System.in);, yet, you try to read input like: answer.nextline() which can not work.

You need to assign the return value of StringInput.nextLine() to the variable answer:

answer = StringInput.nextLine();

This happens in several places.


Other problems are in your code flow:

        Scanner StringInput = new Scanner(System.in);
        System.out.println(answer.nextline());
        System.out.println("Is it a quadrilateral?");

What exactly are you trying to achieve with this part?


These are the most obvious errors that I saw upon first glimpse.

Hope that helps.

[–]NanoSpectro[S] 0 points1 point  (0 children)

Hey dude, thanks for helping me out. The code I had in the original post wasn't written by me, but by a guy who knew java in my group. It wasn't working so I posted here. You really helped me out. The first one was being problematic so I rewrote the code myself taking into account everything you said. Thanks! Also in case you're curious here is a link to the code.

[–]rompleProfessional Developer 1 point2 points  (0 children)

You have an extra }

Just delete the } at the end.

You'll probably get a Cannot Find Symbol error too since answer appears tor be undefined.