Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By quinnvanorder

Thread: Read from file returns zero, which is WRONG!

  1. #1
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Read from file returns zero, which is WRONG!

    I am trying to use a file to store the price of items selected. The problem is that when it reads the data back (in the form of a printline at line 60) it always returns zero, when it should be returning 5.0, which is the value stored in said file. I would really appreciate any help!


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class pizza extends JPanel implements ActionListener 
    {	static int large = 5;
    	static int medium = 4;
    	static int small = 3;
    	static float herp;
    	public JTextArea textArea = new JTextArea();
    	public float total = 0;
    	public JCheckBox bacon = new JCheckBox("bacon");
        public pizza()
       		{	//String output = "cost";
        		setLayout (new BorderLayout());
                setBackground (Color.white);
                
                JPanel b1 = new JPanel(new GridLayout(0,1));
                JButton b2 = new JButton ("Top");
                JPanel b3 = new JPanel(new GridLayout(0,1));
                
                JRadioButton large = new JRadioButton("Large");
                JRadioButton medium = new JRadioButton("Medium");
                JRadioButton small = new JRadioButton("Small");
               
                ButtonGroup size = new ButtonGroup();
                size.add(large);
                size.add(medium);
                size.add(small);
                
                b3.add(textArea);
                
                
                large.addActionListener(this);
                medium.addActionListener(this);
                small.addActionListener(this);
                bacon.addActionListener(this);
               
                b1.add(large);
                b1.add(medium);
                b1.add(small);
                b1.add(bacon);
               
    
                add (b1, BorderLayout.CENTER);
                add (b2, BorderLayout.NORTH);
                add (b3, BorderLayout.EAST);
                 
       		}
       public void actionPerformed(ActionEvent e) 
       		{	String input=e.getActionCommand();
       			
       			if (input == "Small")
       				{	total=0;
       					total+=5;
       					bacon.setSelected(false);
       					write(total);
       					//test
       					System.out.println(herp);
       					
       				}
       			
       			else if (input == "Medium")
    				{	total=0;
    					total+=7;
    					bacon.setSelected(false);
    				}
    			
       			else if (input == "Large")
    				{	total=0;
    					total+=10;
    					bacon.setSelected(false);
    				}
    			
       			else if (input == "bacon" && bacon.isSelected())
    				{	total+=0.50;
    				}
       			
       			else if (input == "bacon" && !bacon.isSelected())
    			{	total-=0.50;
    			}
       			
       			String totalString = total +  "";
       			textArea.setText(totalString);
       		}
       
       public void write (float input)
       		{	try
       				{	String writeString = input +  "\n";
       					FileWriter fstream = new FileWriter("data.txt");
       					PrintWriter out = new PrintWriter(fstream);
       					out.write(writeString);
       					//Close the output stream
       					out.close();
       				}
       			catch (Exception d)
       				{	System.err.println("Error: " + d.getMessage());
       				}
       		}
       
       private static void read() throws java.io.IOException{
    	    byte[] buffer = new byte[(int) new File("data.txt").length()];
    	    FileInputStream f = new FileInputStream("data.txt");
    	    herp = f.read(buffer);
    	  //  return new String(buffer);
    	}
    }

  2. #2
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Read from file returns zero, which is WRONG!

    Do you actually ever call read()?

  3. #3
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Read from file returns zero, which is WRONG!

    well no, and now I feel like a silly, so thanks for catching that... however now it is displaying "4.0" when the variable in the file is "5.0" any thoughts?
    here is the new code (the try catch was to make eclipse stop yelling at me)
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class pizza extends JPanel implements ActionListener 
    {	static int large = 5;
    	static int medium = 4;
    	static int small = 3;
    	static float herp;
    	public JTextArea textArea = new JTextArea();
    	public float total = 0;
    	public JCheckBox bacon = new JCheckBox("bacon");
        public pizza()
       		{	//String output = "cost";
        		setLayout (new BorderLayout());
                setBackground (Color.white);
                
                JPanel b1 = new JPanel(new GridLayout(0,1));
                JButton b2 = new JButton ("Top");
                JPanel b3 = new JPanel(new GridLayout(0,1));
                
                JRadioButton large = new JRadioButton("Large");
                JRadioButton medium = new JRadioButton("Medium");
                JRadioButton small = new JRadioButton("Small");
               
                ButtonGroup size = new ButtonGroup();
                size.add(large);
                size.add(medium);
                size.add(small);
                
                b3.add(textArea);
                
                
                large.addActionListener(this);
                medium.addActionListener(this);
                small.addActionListener(this);
                bacon.addActionListener(this);
               
                b1.add(large);
                b1.add(medium);
                b1.add(small);
                b1.add(bacon);
               
    
                add (b1, BorderLayout.CENTER);
                add (b2, BorderLayout.NORTH);
                add (b3, BorderLayout.EAST);
                 
       		}
       public void actionPerformed(ActionEvent e) 
       		{	String input=e.getActionCommand();
       			
       			if (input == "Small")
       				{	total=0;
       					total+=5;
       					bacon.setSelected(false);
       					write(total);
       					//test
       					try {
    						read();
    					} catch (IOException e1) {
    						// TODO Auto-generated catch block
    						e1.printStackTrace();
    					}
       					System.out.println(herp);
       					
       				}
       			
       			else if (input == "Medium")
    				{	total=0;
    					total+=7;
    					bacon.setSelected(false);
    				}
    			
       			else if (input == "Large")
    				{	total=0;
    					total+=10;
    					bacon.setSelected(false);
    				}
    			
       			else if (input == "bacon" && bacon.isSelected())
    				{	total+=0.50;
    				}
       			
       			else if (input == "bacon" && !bacon.isSelected())
    			{	total-=0.50;
    			}
       			
       			String totalString = total +  "";
       			textArea.setText(totalString);
       		}
       
       public void write (float input)
       		{	try
       				{	String writeString = input +  "\n";
       					FileWriter fstream = new FileWriter("data.txt");
       					PrintWriter out = new PrintWriter(fstream);
       					out.write(writeString);
       					//Close the output stream
       					out.close();
       				}
       			catch (Exception d)
       				{	System.err.println("Error: " + d.getMessage());
       				}
       		}
       
       private static void read() throws java.io.IOException
       		{	try
       			{	byte[] buffer = new byte[(int) new File("data.txt").length()];
       			FileInputStream f = new FileInputStream("data.txt");
       			herp = f.read(buffer);
       			}
       			catch (Exception d)
    			{	System.err.println("Error: " + d.getMessage());
    			}
       		}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Read from file returns zero, which is WRONG!

    One problem with your code is that you're using == to check for String equivalence rather than use either the equals(...) or the equalsIgnoreCase(...) method. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

    Java Code:
    if (fu == "bar") {
      // do something
    }
    do,

    Java Code:
    if (fu.equals("bar")) {
      // do something
    }
    or,

    Java Code:
    if (fu.equalsIgnoreCase("bar")) {
      // do something
    }
    brynpttrsn likes this.

  5. #5
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Read from file returns zero, which is WRONG!

    Also, recheck in the javadocs exactly what FileInputStream.read() returns. It doesn't do exactly what you think it does.

  6. #6
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Read from file returns zero, which is WRONG!

    Excelent, thanks, I changed my == to equalsIgnoreCase and changed my read method to
    Java Code:
       private static void read() throws java.io.IOException
       		{	try
       			{	FileReader fr = new FileReader("data.txt");
       				BufferedReader br = new BufferedReader(fr); 
       				herp =  br.readLine();
       			}
       			catch (Exception d)
    			{	System.err.println("Error: " + d.getMessage());
    			}
       		}
    It works now! Thanks!
    Last edited by quinnvanorder; 05-06-2012 at 11:36 PM. Reason: spelling... not =! now
    Fubarable likes this.

Similar Threads

  1. Every Input File I Use Returns "Unsorted"
    By Cod in forum New To Java
    Replies: 36
    Last Post: 02-27-2011, 06:43 AM
  2. Replies: 4
    Last Post: 02-16-2011, 04:31 AM
  3. Replies: 0
    Last Post: 10-13-2010, 06:26 PM
  4. String file and carriage returns
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 01:28 AM
  5. Replies: 0
    Last Post: 02-11-2009, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •