2

I tried compiling this, and got an error saying: $javac Question2.java 2>&1 Question2.java:55: error: reached end of file while parsing } ^ 1 error TYPE CANNOT BE RESOLVED TO A VARIABLE

I am pretty sure the code is correct but there is a little thing I am missing. The program is meant to check the type of ticket (standard/vip/restricted) and discount(none/student/pensioner), if vip is selected, there is no discount, and there is 5% and 10% discounts for students and pensioners respectively, but I am unable to sort out the input methods, or type declarations.

Any sort of help is appreciated. PS, I am a student currently learning Java and cannot seem to find the exact solution to this problem, hence I've posted a question here.

import java.util.Scanner;



public class Question2 {

public static void main(String args[]){

    Scanner userinput = new Scanner(System.in);

    String ticket ;
    System.out.print(" Type of Ticket: ");
    ticket = userinput.next();

    String discount; 
    System.out.print("What sort of discount do you have?");
    discount = userinput.next();

    int standard = 40;
    int restricted = 20;
    int VIP = 60;



    if ((ticket == "standard") && (type == "student")) {
        double standard_student = standard * 0.95;
        }

    if ((ticket == "standard") && (type == "pensioners")) {
        double standard_pensioners = standard * 0.90;
        }


    if ((ticket == "restricted") && (type == "student")) {
        double restricted_students = restricted * 0.95;
    }


    if ((ticket == "restricted") && (type == "pensioner")) {
        double restricted_pensioner = restricted * 0.90;
    }

    if (ticket=="standard")
        System.out.print("Your ticket costs $.");

    if (ticket == "restricted")
        System.out.print("Your ticket costs $.");


    if (ticket== "VIP")
    System.out.print("Your discount type cannot be used with your requested ticket type.");


    System.out.println ("Thank you!");
    }
2
  • 1
    First, you forgot the closing } for your class. Second, compare string values with String's equals method, not with ==. Commented Oct 24, 2013 at 22:26
  • 1
    For the second part of rgettman's comment, see How do I compare strings in Java? Commented Oct 24, 2013 at 22:26

2 Answers 2

2

You're missing an ending curly brace for your Question2 class:

import java.util.Scanner;

public class Question2 {
  public static void main(String args[]) {
    Scanner userinput = new Scanner(System.in);

    String ticket ;
    System.out.print(" Type of Ticket: ");
    ticket = userinput.next();

    String discount; 
    System.out.print("What sort of discount do you have?");
    discount = userinput.next();

    int standard = 40;
    int restricted = 20;
    int VIP = 60;

    if ((ticket.equals("standard")) && (type.equals("student"))) {
      double standard_student = standard * 0.95;
    }

    if ((ticket.equals("standard")) && (type.equals("pensioners"))) {
      double standard_pensioners = standard * 0.90;
    }

    if ((ticket.equals("restricted")) && (type.equals("student"))) {
      double restricted_students = restricted * 0.95;
    }


    if ((ticket.equals("restricted")) && (type.equals("pensioner"))) {
      double restricted_pensioner = restricted * 0.90;
    }

    if (ticket.equals("standard"))
      System.out.print("Your ticket costs $.");

    if (ticket.equals("restricted"))
      System.out.print("Your ticket costs $.");

    if (ticket.equals("VIP"))
      System.out.print("Your discount type cannot be used with your requested ticket type.");

    System.out.println ("Thank you!");
  }
} // <--- Add this

This type of error can easily be avoided by using proper indention. In fact, most IDEs can automatically format code for you.

Also: Note, you'll need to use .equals() if you want to compare string values. Using the == operator will only compare references. I've updated my example.

Sign up to request clarification or add additional context in comments.

Comments

0

When comparing the VALUE of strings, always use the equals method, not == .

ex: if (type.equals("student"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.