0

So i'm making a program which basically works out tickets sold for an event/events. I have at the moment got an external text file which is linked to my code, the code takes the numbers (which are the amount of tickets someone should of sold for an event) and then i want the user to input using a dialog box how many tickets they have sold. Using if statements i then want the output to be either.. ''Well done you have sold enough tickets'' or ''You should really of sold more tickets'' something like that. This is what i have so far...

import java.util.*;
import java.io.*; 
import javax.swing.JOptionPane;

public class ticketjava
{ 
    public static void main(String[] args) throws FileNotFoundException 
    {

        Scanner inFile = new Scanner(new FileReader("C:\\TicketGoals.txt"));

        double minimumAmount; 
        double goodAmount;

            minimumAmount = inFile.nextDouble();
            goodAmount = inFile.nextDouble();

        String yourTickets;

        yourTickets = JOptionPane.showInputDialog("Enter your tickets sold:");
        if (yourTickets > minimumAmount)  

        JOptionPane.showInputDialog(null, "Well done you have sold enough tickets",  JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);

        inFile.close();

        }

    }   

As you can see my if statement is nowhere near where it should be as i am really struggling how to order it all out, any help will be much appreciated thanks! I am really struggling with my if statements

11
  • 3
    What type is yourTickets and can > be used with that type? Commented Oct 29, 2013 at 17:03
  • One bug I can see is that your if (yourTickets > minimumAmount) needs a { after it Commented Oct 29, 2013 at 17:05
  • 1
    Also what really is your question? Commented Oct 29, 2013 at 17:05
  • 1
    it's my if statement i cant seem to work out So you give up? Read the exception, what does it tell you? What do you think it means? Commented Oct 29, 2013 at 17:08
  • 2
    The problem (as @SotiriosDelimanolis was trying to hint without being explicit) is that yourTickets is a String and so can't be used with the > operator. You need to convert the string to some numeric type. Commented Oct 29, 2013 at 17:09

1 Answer 1

1

I believe you want to convert the variable yourTickets to a double so you can compare it to the variable minimumAmount. You can use the Double.parseDouble() method for that. I recommend reading about comparing Java objects and data types:

http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

You shouldn't be comparing a String type with a double type. Furthermore, you have to use .compareTo() or .equals() for Strings where as you can use >, <, >=, <=, and == for double.

Using if statements i then want the output to be either.. ''Well done you have sold enough tickets'' or ''You should really of sold more tickets'' something like that. This is what i have so far...

You need an if/else statement for this.

import java.util.*;
import java.io.*; 
import javax.swing.JOptionPane;

public class ticketjava
{ 
    public static void main(String[] args) throws FileNotFoundException 
    {

        Scanner inFile = new Scanner(new FileReader("C:\\TicketGoals.txt"));

        double minimumAmount; 
        double goodAmount;

        minimumAmount = inFile.nextDouble();
        goodAmount = inFile.nextDouble();

        String yourTickets;

        yourTickets = JOptionPane.showInputDialog("Enter your tickets sold:");

        //you need to convert the String to a double
        //this will make it comparable with ">" in the below if statement
        double converted_yourTickets = Double.parseDouble(yourTickets);

        //added if/else
        //if condition A is true then do the follow...else do something different
        if (converted_yourTickets > minimumAmount){
            JOptionPane.showInputDialog(null, "Well done you have sold enough tickets",  JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            JOptionPane.showInputDialog(null, "You should really of sold more tickets",  JOptionPane.INFORMATION_MESSAGE);
        }

        //close the file before doing system.exit(0)
        inFile.close();
        //but im not sure why you have it in the first place...
            //System.exit(0);

    }

}   

You seem new to Java, I recommend the following readings of if/else:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

And data types:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

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

1 Comment

I will go through them tutorials now thank you very much you've been a great help!

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.