A little problem

Okay, I modified another program I wrote, I probabaly should have started from scratch, but whatever.

(most of what you see commented out are parts of the code I didn't want to get rid of, incase it was useful)

The problem is somewhat explained in the code, but basically, I run the program, put in my latest paycheck, it takes half of my overall total, then takes any withdrawls and sticks it in another variable, then any tips I happen to get. When I run it, I know it saves what I put in, because I have it printing that out in the beginning.

I'm not sure if the parseInt is doing what it needs to do, I have outputs randomly in the code to try and find where the problem is, so they don't really matter. Now, When it outputs the final numbers, it is just taking what I put in that run and using them, the math I am running with the old numbers isn't effecting the results any.

publicclass Balance{

/**

* @param args

*/

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

{

String path ="C:/paycheck.txt";

JTextArea outputarea =new JTextArea();

String[] spaces=new String[5];

spaces[0]="Total: ";

spaces[1]="Half: ";

spaces[2]="Left to spend: ";

spaces[3]="Tips: ";

String[] data=new String[5];

int counter=0;

String test="";

String[] subtotals=new String[5];

double totals[]=newdouble[5];

double[] outPut=newdouble[5];

double soFar;

double half;

double left;

subtotals[0]="";

subtotals[1]="";

subtotals[2]="";

subtotals[3]="";

try

{

FileReader tester=new FileReader(path);

BufferedReader thingy=new BufferedReader(tester);

for(int count=0;count<4;count++)

{

try

{

test=thingy.readLine();

data[count]=test;

System.out.println(data[count]);

}

catch (IOException e){}

}

try{

tester.close();

}

catch(IOException e){}

}

catch(FileNotFoundException e)

{

System.out.println("File Not Found");

}

for(int scoreloop=0;scoreloop<=4;scoreloop++)

{

try{

subtotals[scoreloop]=subtotals[scoreloop]+(data[scoreloop].substring(0, data[scoreloop].length()));

try

{

totals[scoreloop]=Integer.parseInt(data[scoreloop]);

System.out.println(totals[scoreloop]);

}

catch(NumberFormatException e){}

}

catch(NullPointerException e){}

}

double newCheck=Console.readDouble("Enter Paycheck");

double takenOut=Console.readDouble("Any withdrawl?");

double tips=Console.readDouble("Tips?");

// I think this is where the problem is, it seems to just use the numbers

// that I enter, and do none of the math.

soFar=totals[0]+newCheck;

half=soFar/2;

left=totals[2]+(newCheck/2)-takenOut;

//System.out.println("totals[0]:"+totals[0]);

//System.out.println("takenOut:"+takenOut);

//System.out.println("tips:"+tips);

outPut[0]=soFar;

outPut[1]=half;

outPut[2]=left;

outPut[3]=totals[3]+tips;

try{

FileOutputStream newtester=new FileOutputStream(path);

PrintStream thingy2=new PrintStream(newtester);

for(int count=0;count<4;count++)

{

thingy2.println(outPut[count]);

}

newtester.close();

}

catch(IOException e)

{

System.out.println("File Not Found");

}

//String out="";

for(int count=0;count<4;count++)

{

//System.out.println((count+1)+"."+order2[count]+"\t"+order[count]);

System.out.println(spaces[count]+outPut[count]);

}

//outputarea.setText( out );

//JOptionPane.showMessageDialog( null, outputarea, "High Scores", JOptionPane.INFORMATION_MESSAGE );

}

}

}

This was supposed to be a quick little program, so i don't really have seperate classes or anything for it.

[7265 byte] By [bizkuta] at [2007-11-27 9:49:59]
# 1

The code you posted is really hard to read. I don't know what you did to screw up the code formatter (weird tabs and spaces in the copied source?), but the indenting is a mess. It's difficult to tell where loops end and what catch statements go with which try statements. People will be more inclined to reply to your post if they can follow your code.

Next, when you catch exceptions you always always always (always always always) need to print something or log something or give some indication of that. Your program is failing because of a NumberFormatException, but you don't know that because you catch the exception and do nothing with it. Add printlns to all those catch blocks and you'll find your error.

Also, you initialize your arrays like data[] to length 5, then you only populate 4 elements in the array with loops from for( int i=0; i < 4; i++ ), leaving the last element unset. Is there a reason for this?

Probably the line that is causing your problems is this. What are you trying to do with this line, because I guarantee it is not doing whatever you think it is.

subtotals[scoreloop]=subtotals[scoreloop]+(data[scoreloop].substring(0, data[scoreloop].length()));

Also, you know that you can catch multiple exceptions from the same try block, right? So your code:

try{

subtotals[scoreloop]=subtotals[scoreloop]+(data[scoreloop].substring(0, data[scoreloop].length()));

try

{

totals[scoreloop]=Integer.parseInt(data[scoreloop]);

System.out.println(totals[scoreloop]);

}

catch(NumberFormatException e){}

}

catch(NullPointerException e){}

would be a lot cleaner as

try

{

subtotals[scoreloop]=subtotals[scoreloop]+(data[scoreloop].substring(0, data[scoreloop].length()));

totals[scoreloop]=Integer.parseInt(data[scoreloop]);

System.out.println(totals[scoreloop]);

} catch(NumberFormatException e) {

System.out.println(e);

e.printStackTrace();

} catch(NullPointerException e){

System.out.println(e);

e.printStackTrace();

}

or don't even catch those at all, since they are RuntimeExceptions.If you're seeing them that should be a hint that something else is wrong.

Jemiah

fishninja007a at 2007-7-13 0:18:33 > top of Java-index,Java Essentials,Java Programming...