David Clegg
David Clegg

Reputation: 3

If statement and input output

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

Upvotes: 0

Views: 1666

Answers (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

Upvotes: 1

Related Questions