Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Hey all I have a few lab assignments due this Friday and I am new to JAVA and programming in general. This specific assignment requires me to write a program that calculates the total sales after each football game. There are 4 types of tickets: box, sideline, premium, and general admission. After each game, data is stored in a file... then output the number of tickets sold and total sale amount. Format your output with 2 decimal places.

    Okay I know there is a I/O section and wasnt sure where to post this. I need some insight on how to use file input and output as well if anyone can give me some ideas how to "clean" my program up a little. I feel like its a lot longer than it has to be but again I am a total noob and only have little knowledge of programming. Here is my program:

    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
     
    public class ticketsSold
    {
    	public static void main(String[] args)
    	{
    		String BoxSeat;
    		String SideLine;
    		String PremiumSeat;
    		String GeneralSeat;
    		double boxCost = 250.00;
    		double sideCost = 100.00;
    		double premiumCost = 50.00;
    		double generalCost = 25.00;
    		double box;
    		double side;
    		double premium;
    		double general;
    		String newDecimal;
    		String newDecimal1;
    		String newDecimal2;
    		String newDecimal3;
    		String newDecimal4;
     
    		BoxSeat =
    			JOptionPane.showInputDialog("Enter how many box tickets were sold: ");
    			box = Double.parseDouble(BoxSeat);
     
    		SideLine =
    			JOptionPane.showInputDialog("Enter how many sideline tickets were sold: ");
    			side = Double.parseDouble(SideLine);
     
    		PremiumSeat =
    			JOptionPane.showInputDialog("Enter how many premium tickets were sold: ");
    			premium = Double.parseDouble(PremiumSeat);
     
    		GeneralSeat =
    			JOptionPane.showInputDialog("Enter how many general tickets were sold: ");
    			general = Double.parseDouble(GeneralSeat);
     
    		double totalBox = (box * boxCost);
    		double totalSide = (side * sideCost);
    		double totalPremium = (premium * premiumCost);
    		double totalGeneral = (general * generalCost);
    		double grandTotal = (totalBox + totalSide + totalPremium + totalGeneral);
     
    		DecimalFormat formatter = new DecimalFormat("#0.00");
     
    		newDecimal = (formatter.format(grandTotal));
    		newDecimal1 = (formatter.format(totalBox));
    		newDecimal2 = (formatter.format(totalSide));
    		newDecimal3 = (formatter.format(totalPremium));
    		newDecimal4 = (formatter.format(totalGeneral));
     
     
    		JOptionPane.showMessageDialog(null, "The total number of $250.00 box seats sold this"
    									 + " week: " + box + " seats netting: $"
    									 + newDecimal1 + "\nThe total number of $100.00 sideline"
    									 + " seats sold this week: " + side + " tickets"
    									 + " netting: $" + newDecimal2 + "\nThe total number"
    									 + " of $50.00 premium seats sold this week: "
    									 + premium + " seats netting: $" + newDecimal3
    									 + "\nThe total number of $25.00 general seats sold this"
    									 + " week: " + general + " seats netting: $"
    									 + newDecimal4 + "\nThe grand total of sales for the"
    									 + " MCC football stadium this week is: $" + newDecimal
    									 + "", "MCC Stadium Report", JOptionPane.INFORMATION_MESSAGE);
     
    									 System.exit(0);
    	}
    }


  2. #2
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    This is my output Dialog Box.

    output.jpg

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    can give me some ideas how to "clean" my program up a little
    are there any compilation errors? runtime errors? bugs?
    if it does not have any errors, i think experience and practice will make you create a better coding, algorithm
    just continue programming as long as you enjoy it

    Format your output with 2 decimal places.
    you can use printf method to format the output i suggest you to read this tutorial for printf
    Java For Complete Beginners - formatted strings
    you can also use String.format to format you output
    String (Java Platform SE 7 )


    I need some insight on how to use file input and output
    have you tried to use/read some IO classes in java that can support what you want to do?
    if not, here are the links

    for java.io.File File (Java Platform SE 7 )

    you can read files using these classes:
    java.io.FileReader FileReader (Java Platform SE 7 )
    java.io.BUfferedReader BufferedReader (Java Platform SE 7 )

    you can write on files using these classes:
    java.io.FileWriter FileWriter (Java Platform SE 7 )
    java.io.BufferedWriter BufferedWriter (Java Platform SE 7 )

    there are another way of reading/writing on files aside from those i gave,
    but you can stick to that as for starters.

    try to do that, and if there is a problem, just ask here and we are going to help you

  4. #4
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    There are no compiler errors and the program runs just fine. I was just curious if there are any shortcuts to make it well, shorter! haha... As far as input and output I know I can import the java.io.* class and use FileWriter, PrintWriter etc... I am just unsure of how to even code it into the program. I am the type of learner that has to see it done and then explained why it works like that. I will take a look at some of the links you posted. Thanks!

    --- Update ---

    Quote Originally Posted by dicdic View Post
    are there any compilation errors? runtime errors? bugs?
    if it does not have any errors, i think experience and practice will make you create a better coding, algorithm
    just continue programming as long as you enjoy it


    you can use printf method to format the output i suggest you to read this tutorial for printf
    Java For Complete Beginners - formatted strings
    you can also use String.format to format you output
    String (Java Platform SE 7 )



    have you tried to use/read some IO classes in java that can support what you want to do?
    if not, here are the links

    for java.io.File File (Java Platform SE 7 )

    you can read files using these classes:
    java.io.FileReader FileReader (Java Platform SE 7 )
    java.io.BUfferedReader BufferedReader (Java Platform SE 7 )

    you can write on files using these classes:
    java.io.FileWriter FileWriter (Java Platform SE 7 )
    java.io.BufferedWriter BufferedWriter (Java Platform SE 7 )

    there are another way of reading/writing on files aside from those i gave,
    but you can stick to that as for starters.

    try to do that, and if there is a problem, just ask here and we are going to help you
    Also, can I use printf when I have to show my output in a dialog box?

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Okay. Let me give you a simple way to learn how to read and write to files using those classes.

    We will learn how to read it first.
    First, create a file (hello.txt) on you computer, put some text on the file.
    then create a new java file.
    that java file will read the file hello.txt

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Quote Originally Posted by brobertson300 View Post
    Also, can I use printf when I have to show my output in a dialog box?
    Yes. Simply format a string and then pass it to JOptionPane!

    String message = String.format("grand total is %.2f", grandTotal);
    JOptionPane.showMessageDialog(null, message);

    Note: String has only format, not printf. I/O classes like PrintStream/PrintWriter have both format and printf (they do the same thing).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. #7
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Quote Originally Posted by dicdic View Post
    Okay. Let me give you a simple way to learn how to read and write to files using those classes.

    We will learn how to read it first.
    First, create a file (hello.txt) on you computer, put some text on the file.
    then create a new java file.
    that java file will read the file hello.txt
    Okay so I create a file ____.txt and then a new file in java. Next steps?

  8. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    sorry for late response, its only 8:00 am here.. so i think i can assist you this whole day since i am not busy

    okay, next were going to open that file and read using FileReader.

    FileReader:
    Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.
    FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.

    -that is from Java Doc
    FileReader has 3 constructor:
    FileReader(File file) - Creates a new FileReader, given the File to read from.
    FileReader(FileDescriptor fd) - Creates a new FileReader, given the FileDescriptor to read from.
    FileReader(String fileName) - Creates a new FileReader, given the name of the file to read from.

    For now, we are going to use third one. FileReader(String fileName)
    How to declare a FileReader? here's how:
    first import FileReader: java.io.FileReader
    next we will create an object reference variable for FileReader:
    FileReader (variable name) = new FileReader(<path of your file that will going to read>);
    new keyword will make us create new Object.
    next to it is the constructor.
    I think it throws FileNotFoundException, so make sure that you handle it..
    try it now

  9. #9
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Quote Originally Posted by dicdic View Post
    sorry for late response, its only 8:00 am here.. so i think i can assist you this whole day since i am not busy

    okay, next were going to open that file and read using FileReader.

    FileReader:
    Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.
    FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.

    -that is from Java Doc
    FileReader has 3 constructor:
    FileReader(File file) - Creates a new FileReader, given the File to read from.
    FileReader(FileDescriptor fd) - Creates a new FileReader, given the FileDescriptor to read from.
    FileReader(String fileName) - Creates a new FileReader, given the name of the file to read from.

    For now, we are going to use third one. FileReader(String fileName)
    How to declare a FileReader? here's how:
    first import FileReader: java.io.FileReader
    next we will create an object reference variable for FileReader:
    FileReader (variable name) = new FileReader(<path of your file that will going to read>);
    new keyword will make us create new Object.
    next to it is the constructor.
    I think it throws FileNotFoundException, so make sure that you handle it..
    try it now
    Okay just logged in. I am going to try now!

    --- Update ---

    Alright. So i am having trouble becasue im not sure what the coding is supposed to look like. Can you Show me an example?

  10. #10
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    just paste here the code you have created. and i'm gonna check what is wrong with your code

  11. #11
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Quote Originally Posted by dicdic View Post
    just paste here the code you have created. and i'm gonna check what is wrong with your code
    I couldnt figure out how to even write the code. I need a guideline of a basic example to get started. Sorry :/ for some reason this is really hard for me to figure out lol

  12. #12
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Quote Originally Posted by brobertson300 View Post
    I couldnt figure out how to even write the code. I need a guideline of a basic example to get started. Sorry :/ for some reason this is really hard for me to figure out lol
    You don't know how to write a code?
    hmm, so I think you should study the basics first.
    Since you have internet, try to visit this link:
    Java Tutorial
    that is a java tutorial for beginners.

  13. #13
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Quote Originally Posted by dicdic View Post
    You don't know how to write a code?
    hmm, so I think you should study the basics first.
    Since you have internet, try to visit this link:
    Java Tutorial
    that is a java tutorial for beginners.
    No No haha... I know how to write code. I am just unsure of how to get the program to read data from a .txt file. This is what I have so far and need guidance on the next steps. I used Scanner infile = new Scanner(new FileReader("__.txt")
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    import java.io.*;
     
    public class ticketsSold
    {
    	public static void main(String[] args)
    	{
    		String BoxSeat;
    		String SideLine;
    		String PremiumSeat;
    		String GeneralSeat;
    		double box;
    		double side;
    		double premium;
    		double general;
    		String newDecimal;
    		String newDecimal1;
    		String newDecimal2;
    		String newDecimal3;
    		String newDecimal4;
     
    		Scanner infile = new Scanner(new fileReader("TicketInvoice");
     
    		BoxSeat =
    			JOptionPane.showInputDialog("Enter how many box tickets"
    									   + "were sold: ");
    			box = inFile.nextInt();
     
    		SideLine =
    			JOptionPane.showInputDialog("Enter how many sideline tickets"
    									   + "were sold: ");
    			side = inFile.nextInt();
     
    		PremiumSeat =
    			JOptionPane.showInputDialog("Enter how many premium tickets"
    									   + "were sold: ");
    			premium = inFile.nextInt();
     
    		GeneralSeat =
    			JOptionPane.showInputDialog("Enter how many general tickets"
    									   + "were sold: ");
    			general = inFile.nextInt();
     
    		double totalBox = (box * boxCost);
    		double totalSide = (side * sideCost);
    		double totalPremium = (premium * premiumCost);
    		double totalGeneral = (general * generalCost);
    		double grandTotal = (totalBox + totalSide + totalPremium
    							+ totalGeneral);
     
    		DecimalFormat formatter = new DecimalFormat("#0.00");
     
    		newDecimal = (formatter.format(grandTotal));
    		newDecimal1 = (formatter.format(totalBox));
    		newDecimal2 = (formatter.format(totalSide));
    		newDecimal3 = (formatter.format(totalPremium));
    		newDecimal4 = (formatter.format(totalGeneral));
     
     
    		JOptionPane.showMessageDialog(null, "The total number of $250.00"
    									 + "box seats sold this"
    									 + " week: " + box + " seats netting: $"
    									 + newDecimal1 + "\nThe total number of"
    									 + "$100.00 sideline"
    									 + " seats sold this week: " + side
    									 + " tickets"
    									 + " netting: $" + newDecimal2 + "\nThe"
    									 + "total number"
    									 + " of $50.00 premium seats sold"
    									 + "this week: "
    									 + premium + " seats netting: $"
    									 + newDecimal3
    									 + "\nThe total number of $25.00 general"
    									 + "seats sold this"
    									 + " week: " + general + " seats"
    									 + "netting: $"
    									 + newDecimal4 + "\nThe grand total"
    									 + "of sales"
    									 + "for the"
    									 + " MCC football stadium this week is: $"
    									 + newDecimal
    									 + "", "MCC Stadium Report"
    									 +"", JOptionPane.INFORMATION_MESSAGE);
     
    									 System.exit(0);
    	}
    }

  14. #14
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    I guess you have compilation error right?
    FileReader throws FileNotFoundException, it is either you catch it using try catch or throws it.

    Scanner infile = new Scanner(new fileReader("TicketInvoice");
    you parameter is String, that must be the file path of your file. something like C:\File.txt

  15. #15
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Quote Originally Posted by dicdic View Post
    I guess you have compilation error right?
    FileReader throws FileNotFoundException, it is either you catch it using try catch or throws it.


    you parameter is String, that must be the file path of your file. something like C:\File.txt
    Okay I am converting over to the Scanner class instead of using dialog boxes (JOptionPane) because thats what my teacher wants. I will post what I have in a few minutes.

    --- Update ---

    Quote Originally Posted by dicdic View Post
    I guess you have compilation error right?
    FileReader throws FileNotFoundException, it is either you catch it using try catch or throws it.


    you parameter is String, that must be the file path of your file. something like C:\File.txt
    import java.util.Scanner;
    import java.text.DecimalFormat;
    import java.io.*;
     
    public class ticketsSold
    {
    	public static void main(String[] args) throws IOException
    	{
    		double box;
    		double side;
    		double premium;
    		double general;
    		String newDecimal;
    		String newDecimal1;
    		String newDecimal2;
    		String newDecimal3;
    		String newDecimal4;
     
    		Scanner infile = new Scanner(new fileReader("TicketInvoice.txt"));
     
    		System.out.println("Enter how many box tickets"
    						  + "were sold: ");
    		box = inFile.nextInt(BoxSeat);
     
    		System.out.println("Enter how many sideline tickets"
    						  + "were sold: ");
    		side = inFile.nextInt(SideLine);
     
    		System.out.println("Enter how many premium tickets"
    						  + "were sold: ");
    		premium = inFile.nextInt(PremiumSeat);
     
    		System.out.println("Enter how many general tickets"
    						  + "were sold: ");
    		general = inFile.nextInt(GeneralSeat);
     
    		double totalBox = (box * boxCost);
    		double totalSide = (side * sideCost);
    		double totalPremium = (premium * premiumCost);
    		double totalGeneral = (general * generalCost);
    		double grandTotal = (totalBox + totalSide + totalPremium
    							+ totalGeneral);
     
    		DecimalFormat formatter = new DecimalFormat("#0.00");
     
    		newDecimal = (formatter.format(grandTotal));
    		newDecimal1 = (formatter.format(totalBox));
    		newDecimal2 = (formatter.format(totalSide));
    		newDecimal3 = (formatter.format(totalPremium));
    		newDecimal4 = (formatter.format(totalGeneral));
     
     
    		JOptionPane.showMessageDialog(null, "The total number of $250.00"
    									 + "box seats sold this"
    									 + " week: " + box + " seats netting: $"
    									 + newDecimal1 + "\nThe total number of"
    									 + "$100.00 sideline"
    									 + " seats sold this week: " + side
    									 + " tickets"
    									 + " netting: $" + newDecimal2 + "\nThe"
    									 + "total number"
    									 + " of $50.00 premium seats sold"
    									 + "this week: "
    									 + premium + " seats netting: $"
    									 + newDecimal3
    									 + "\nThe total number of $25.00 general"
    									 + "seats sold this"
    									 + " week: " + general + " seats"
    									 + "netting: $"
    									 + newDecimal4 + "\nThe grand total"
    									 + "of sales"
    									 + "for the"
    									 + " MCC football stadium this week is: $"
    									 + newDecimal
    									 + "", "MCC Stadium Report"
    									 +"", JOptionPane.INFORMATION_MESSAGE);
     
    									 System.exit(0);
    	}
    }


    --- Update ---

    Now my question is how does the program pick up the correct information from the .txt file and put it in the right place? I am still trying to figure out how this actually works. I created a .txt file in notepad the following values are stored in there like this:

    250 5750
    100 28000
    50 3750
    25 18750

    These numbers represent ticket prices and tickets sold.

  16. #16
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    used nextLine() method of scanner. that returns a string.
    try to print it

    but make sure to handle it with try-catch. you might end up with NoSuchElementException I guess.

  17. #17
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    Quote Originally Posted by dicdic View Post
    used nextLine() method of scanner. that returns a string.
    try to print it

    but make sure to handle it with try-catch. you might end up with NoSuchElementException I guess.
    Okay so use nextLine instead of nextInt... What is try-catch?

    --- Update ---

    This is all messed up. I really cant figure out how to do this. Cant you show me a proper example? Doesnt have to be with my problem just something general

  18. #18
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!

    First fix the path of your FileReader.
    that must be a path.
    Scanner infile = new Scanner(new fileReader("TicketInvoice.txt"));
    change "TicketInvoice.txt" to its full path.
    it is okay to use not the full path, but for now, we are going to use its absolute path

Similar Threads

  1. [SOLVED] [Homework] Requesting User Input
    By nKulo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 24th, 2013, 01:42 PM
  2. File input and output
    By RKA in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: January 11th, 2013, 08:27 AM
  3. Input/Output help if possible
    By rossonomous in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 13th, 2011, 01:05 PM
  4. Input/Output Sequence
    By JoseGuad in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: August 30th, 2011, 06:50 AM
  5. [SOLVED] Need help with input/output error
    By stefan2892 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2011, 10:44 AM