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!");
    }
TomMatt
  • 37
  • 5
  • 1
    First, you forgot the closing `}` for your class. Second, compare string values with `String`'s `equals` method, not with `==`. – rgettman Oct 24 '13 at 22:26
  • 1
    For the second part of rgettman's comment, see [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?rq=1) – Dennis Meng Oct 24 '13 at 22:26

2 Answers2

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.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
0

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

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

josh
  • 365
  • 3
  • 12