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 6 of 6

Thread: File Input Trouble

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with File input .

    Hello ,
    I have this fun assignment not for a grade i have to do , I am suppose to read the offering and prices of a restaurant menu from a file like this one

    Blueberry Muffins   $0.85
    Chocolate Brownie   $1.75
    French Toast        $2.25
    Strawberry Bagels   $0.80
    Lite Yogurt         $0.75
    Vanilla Ice Cream   $2.75
    Hash Browns         $2.50
    Toast               $2.00
    French Fries        $1.50
    Onion Soup          $3.00
    Coffee              $0.90
    Croissant           $0.75
    Iced Tea            $1.00
    Hot Chocolate       $1.75
    Food                $2.50
     
     Thank you for your business

    Then Sort the menu in alphabetic order, calculate the items prices and add a sale tax of 6% but also a local tax of 1% , then the program is suppose to read the greeting line such as << Thank you for your business >> from the file input and produce a day end report .

    Done so far
    I have the input and prices sorted on the left panel , I also have the day end report , the local and state tax set

    What I have left
    - For right now I have the prices saved in a array of static double instead of reading from a file
    -Input the prices and offerings when selected in the right pane in order to calculate the subtotal price and add to both taxes in order to have the total price
    - As of right now I have the program reading the greeting from a different file but would like to only use one file .

    Code

    public class remet extends JFrame implements ActionListener 
     
    {
     static double[] yourChoicesPrices = 
                  {0.85, 0.80, 0.75, 2.75,
                  2.50, 2.00, 1.50, 3.00,
                0.90, 1.00, 2.00,2.55,2.75,.75};
     
       private JList yourChoices;
       private  JTextArea  bill;
       double LocalTax = 0.010;
       double StateTax = 0.060;
       double TotalTax =0;
       double subTotal = 0;
       double total;
       private Container pane;
     
     
     
       public remet() throws IOException
     
       {
     
          super("Welcome to The Gourmet");
     
     
      	// File Input 
          // Display menu on the left pane
    		FileInputStream inFile = new FileInputStream("C://menu.txt");
    		DataInputStream menu = new DataInputStream(inFile);
    		BufferedReader br = new BufferedReader(new InputStreamReader(menu));
    		String Menu;
    		ArrayList<String> MenuItems = new ArrayList<String>();
    		while ((Menu = br.readLine()) != null)   {
    		    MenuItems.add(Menu);
    		}
     
     
    		String[] yourChoicesItems = new String[MenuItems.size()];
      	yourChoicesItems = MenuItems.toArray(yourChoicesItems);
    		menu.close();
    for (int i=0 ;i <yourChoicesItems.length;i++)
    		System.out.println();
    		for (int i=0; i<yourChoicesItems.length -2; i++){
    			for (int j=yourChoicesItems.length-2; j>=i;j--){
    				if (yourChoicesItems[j].compareTo(yourChoicesItems[j+1])>0){
    					String temp =yourChoicesItems [j];
    					yourChoicesItems[j]=yourChoicesItems[j+1];
    					yourChoicesItems[j+1]=temp;
    				}
    			}
    		}
     
             //Get the content pane and set its background color
             //and layout manager.
          pane = getContentPane();
          pane.setBackground(new Color(0, 200, 200));
          pane.setLayout(new BorderLayout(5, 5));
     
              //Create a label and place it at NORTH. Also
              //set the font of this label.
          JLabel yourChoicesJLabel = new JLabel("A LA CARTE MENU");
          pane.add(yourChoicesJLabel,BorderLayout.NORTH);
          yourChoicesJLabel.setFont(new Font("Dialog",Font.BOLD,20));
          yourChoices = new JList(yourChoicesItems);
          pane.add(new JScrollPane (yourChoices),BorderLayout.WEST);
          yourChoices.setFont(new Font("Courier",Font.BOLD,14));
     
              //Create a text area and place it at EAST. Also
              //set the font of this text area.
     
        bill = new JTextArea();
          JScrollPane Scrollbill = new JScrollPane(bill);
          Scrollbill.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          pane.add(Scrollbill,BorderLayout.EAST);
          bill.setFont(new Font("Courier",Font.PLAIN,12));
     
              //Create a button and place it in the SOUTH region and
              //add an action listener.
          JButton button = new JButton("Selection Completed");
          pane.add(button,BorderLayout.SOUTH);
          button.addActionListener(this);
          ///
          setSize(500, 360);
          setVisible(true);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
       }
     
               //Method to display the order and the total cost.
       private void displayBill() throws IOException
       {
     
          int[] listArray = yourChoices.getSelectedIndices();
     
          /// This is the second input to get the items  
          FileInputStream inFile1 = new FileInputStream("" +
          		"C://menu.txt");
    		DataInputStream menu = new DataInputStream(inFile1);
    		BufferedReader br1 = new BufferedReader(new InputStreamReader(menu));
    		String menuText;
    		ArrayList<String> menuItems = new ArrayList<String>();
    		while ((menuText = br1.readLine()) != null)   {
    		    menuItems.add(menuText);
    		}
    		String[] yourChoicesItems = new String[menuItems.size()];
      	yourChoicesItems = menuItems.toArray(yourChoicesItems);
    		menu.close();
     
             //Set the text area to non-edit mode and start
             //with an empty string.
          bill.setEditable(false);
          bill.setText("");
     
              //Calculate the cost of the items ordered.
          for (int index = 0; index < listArray.length; index++)
              subTotal = subTotal
                         + yourChoicesPrices[listArray[index]];
     
          LocalTax = LocalTax * subTotal;
          StateTax = StateTax* subTotal;
          TotalTax = LocalTax + StateTax;
          total = subTotal + LocalTax + StateTax;
     
              //Display the costs.
          bill.append("           THE GOURMET\n\n");
          bill.append("--------------- Welcome ----------------\n\n");
     
          for (int index = 0; index < listArray.length; index++)
          {
             bill.append(yourChoicesItems[listArray[index]] + "\n");
             bill.append(yourChoicesPrices[listArray[index]] + "\n");
          }
     
          bill.append("\n");
          bill.append("SUB TOTAL\t\t$"
                     + String.format("%.2f", subTotal) + "\n");
          bill.append("Local Tax      \t\t$"
                     + String.format("%.2f", LocalTax) + "\n");
          bill.append("State Tax      \t\t$"
                  + String.format("%.2f", StateTax) + "\n");
          bill.append("TOTAL    \t\t$"
                     + String.format("%.2f", total) + "\n\n");
    		FileInputStream inFile  = new FileInputStream ("c:\\thankyou.txt");
          DataInputStream Menu = new DataInputStream(inFile);
          BufferedReader br = new BufferedReader(new InputStreamReader (Menu));
          String MenuAll;
          while ((MenuAll= br.readLine()) != null) {
          bill.append(MenuAll+"\n\n" );
         }
         Menu.close();
     
              //Reset list array.
     
          yourChoices.clearSelection();
     
          repaint();
       }
       //Make Report for the day 
       private void Report()
       {
    	   PrintWriter outFile = null;
    	try {
    		outFile = new
    					PrintWriter("c:\\Day-End Report.txt");
    	} catch (FileNotFoundException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
     
    outFile.printf("Sales Tax Collected:     $%.2f %n" ,TotalTax );
    outFile.printf("Total Sales:     $%.2f %n" , total ); 
     
     
    outFile.close();	
     
       }
       public void actionPerformed(ActionEvent event)
       {
          if(event.getActionCommand().equals("Selection Completed"))
    		try {
    			displayBill();
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
              Report();
     
       }
     
       public static void main(String[] args) throws IOException 
       {
          remet alc = new remet();
       }
    }

    Any help would be greatly appreciated thanks
    Last edited by mael331; March 12th, 2012 at 05:06 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,376
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: File Input Trouble

    Do you have any specific questions about what you are trying to do?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: File Input Trouble

    Thanks for your reply , i am trying to read the items and prices from the input file into the program so I can calculate the subtotal then the total price .

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,376
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: File Input Trouble

    The Scanner class can be used for reading text files. Use the constructor that takes a File object and use one of the next methods to read in the lines from the file.
    If all the $ amounts are in the same column you could use the String class's substring method to get them.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: File Input Trouble

    I don't really want to use the scanner class because I have no idea how I can implement it to display the items and prices also I want the prices to be double how can I convert the prices from String to double in the program ?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,376
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: File Input Trouble

    I don't really want to use the scanner class
    Then look at using the BufferedReader class. It can read the lines from the file as Strings.
    how can I convert the prices from String to double
    Look at the Double class. It has a parse method for doing that.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  2. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 07:41 PM
  3. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 12:05 AM
  4. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM
  5. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM