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

Thread: whats wrong with my program??

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default whats wrong with my program??

    I am writing a program that reads data from a file, converts it to integers and than displays 30% of the value...but i cannot get it to work...can someone give me a tip on this please.....

    public class K {	
    	public static void main (String[] args) throws IOException
    	{
    		int value;
    		String fileName;
    		String income;
    		double finalList=0;
    		fileName=JOptionPane.showInputDialog("Enter the file name: ");
    		try
    		{
    			double tax = 0;
    			FileReader freader=new FileReader(fileName);
    			BufferedReader  inputFile= new BufferedReader(freader);
    			income= inputFile.readLine();
    			value=Integer.parseInt(income);
    			while(income!=null)
    			{
    				tax=(value)*.30;
    				finalList+=tax+'\n';
    				income=inputFile.readLine();
    				tax=Double.parseDouble(income);
     
    			}
    			JOptionPane.showMessageDialog(null, tax+'\n');
    			inputFile.close();
    		}
    		catch (FileNotFoundException e)
    		{
    			JOptionPane.showMessageDialog(null, "file not found");
     
    		}
    	}
    }
    Last edited by helloworld922; May 9th, 2010 at 10:15 AM. Reason: please use [code] tags


  2. #2
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: whats wrong with my program??

    I am writing a program that reads data from a file, converts it to integers and than displays 30% of the value...but i cannot get it to work...can someone give me a tip on this please.....
    What happens when you run it?

  3. The Following 2 Users Say Thank You to smarty187 For This Useful Post:

    JavaPF (May 10th, 2010), Json (May 10th, 2010)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: whats wrong with my program??

    I think the logic in your program is somewhat flaw.

    You only read the value from the first line of the file you open and after that you loop through each line in the file reading in each line and storing it in the tax variable which then gets overwritten in the next while cycle by value x 0.30, no matter what the tax you add to the finalList will always be the same. On another note finalList is a primitive double and you try to increase it and add \n to the end?

    finalList+=tax+'\n';

    Should that not just read.

    finalList += tax;

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    jrodriguo (May 11th, 2010)

  6. #4
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: whats wrong with my program??

    Quote Originally Posted by jrodriguo View Post
    can someone give me a tip on this please.....

    your while loop was wrong when the file contained multiple lines. to read multiple line use my while loop.

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
     
    import javax.swing.JOptionPane;
     
     
    public class TestClass {
     
    		public static void main (String[] args) throws IOException
    		{
    			int value;
    			String fileName;
    			String income;
    			double finalList=0;
    			String allIncome = "";
    			fileName=JOptionPane.showInputDialog("Enter the file name: ");
    			try
    			{
    				double tax = 0;
    				FileReader freader=new FileReader(fileName);
    				BufferedReader  inputFile= new BufferedReader(freader);
    				while((income= inputFile.readLine())!=null)
    				{
    					value = Integer.parseInt(income);
    					tax=value / 3.0;
    					finalList+=tax;
    					allIncome+=tax + "\n";
    				}
    				JOptionPane.showMessageDialog(null, allIncome);
    				inputFile.close();
    			}
    			catch (FileNotFoundException e)
    			{
    				JOptionPane.showMessageDialog(null, "file not found");
     
    			}
    		}
    	}

    have fun.

  7. The Following User Says Thank You to j2me64 For This Useful Post:

    jrodriguo (May 11th, 2010)

  8. #5
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: whats wrong with my program??

    thanks everyone...this worked out for me great...really appreciate everyone's help

Similar Threads

  1. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM
  2. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 08:38 PM
  3. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 10:08 AM
  4. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  5. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM