Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

input mismatch problem.

807599Feb 24 2007 — edited Feb 25 2007
Below is my code which compiles with no error but when i try to run it gives me an error :
Exception in thread "main" java.lang.NumberFormatException: empty String
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
        at java.lang.Double.parseDouble(Double.java:482)
        at ProfitCalculator.main(ProfitCalculator.java:37)
and here is my code.
import java.io.*;
import java.util.Scanner;
import java.io.BufferedReader;

class ProfitTester 
{
	public double computeProfit(double quantity, double price, int code)
	{
		if(code==1)
		return 0.10 * quantity * price;
		else if(code==2)
		return 0.12 * quantity * price;
		else if(code==3)
		return 0.15 * quantity * price;
		else
		return -1;
	}
}
class ProfitCalculator
{
	public static void main(String[] args) throws IOException
	{
		Scanner s = null;
		int profitcode;
		double quantity;
		double price, itemprofit, totalprofit=0;
		ProfitTester test = new ProfitTester();
		try 
		{
			s = new Scanner(
				new BufferedReader (new FileReader("saloo.txt")));
				String f = s.nextLine();
			//	while(f!=null)
			for(int i=0; f!=null; i++)
				{
			String[] str = f.split(" ");
			 itemprofit = test.computeProfit(Double.parseDouble(str[1]), Double.parseDouble(str[2])
			 ,Integer.parseInt(str[3]));
			 totalprofit += itemprofit;
			System.out.println("product: "+str[0]);
            System.out.println ("Quantity: "+str[1]);
            System.out.println("price: "+str[2]);
            System.out.println("code: "+str[3]);
            System.out.println("Expected Profit: "); 
            System.out.println("------------------ ");
			f=s.nextLine();
				}

				}	
	finally
		{
			s.close();
		}
		System.out.println("THe total profit is " + totalprofit);
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 25 2007
Added on Feb 24 2007
19 comments
333 views