this compareto method
I know there are alot of examples about this compareto method for strings.
but I can not seem to get this working
if(r.equals(temp[j]))
and here is the rest of the code.
public static double paid()
{
temp = recall.recalldata();
String total ="";
double totald=0.0;
double totalamount =0.0;
String r = "P";
for(int j=4;j<c;j+=5)
{
if(r.equals(temp[j]))
{
totald = Double.valueOf(temp[j-2]).doubleValue();
totalamount = (totalamount + totald);
}
}
return totalamount;
}
thanks>
The code you posted did not include a compareTo method. Note the capital "T" in compareTo. If you call the method compareto, it won't get called.
I have tried that if(r.compareTo(temp[j])==0)
What type is r and what exactly are you trying to do? The Comparable interface (whose only method is compareTo) is usually used when you need to sort a class. It looks like you are attempting to test for equality and should probably be using the equals method.
So what exactly isn't working? In other words: what do you expect to happen, and what happens instead?
here is my code for the class
if I take out the array it works,
I want it return true when array gets to P
import java.util.*;
import java.text.*;
class mainstuff
{
private static int c = LineCounter.c();
private static String [] temp = new String [c];
public static String menu()
{
String menu1 = ("1. Display Total Amount Owed for Bills this Month."+
"\n2. Display Total Amount Paid So Far This Month." +
"\n3. Display Total Amount Unpaid this Month." +
"\n4. Display Entire Amount Owed in Credit Card Debt(all Cards)." +
"\n5. Display Amount of disposable Income Left After Paying "+
"All the Monthly Bills."+
"\n6. Display the Entire Budget."+
"\n7. Add Bills."+
"\n8. Add money to the Bank." +
"\n Press Q to quit");
return menu1;
}
public static String mainswitch(int key)
{
String temp="";
switch(key)
{
case 1: double t = Total();
String ts = formater(t);
return ts;
case 2: double r = paid();
String ps = formater(r);
return ps;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
default: break;
}
return temp;
}
public static double Total()
{
temp = recall.recalldata();
String total ="";
double totald=0.0;
double totalamount =0.0;
for(int i=2;i<c;i+=5)
{
totald = Double.valueOf(temp).doubleValue();
totalamount = (totalamount + totald);
}
return totalamount;
}
public static String formater(double amount)
{
DecimalFormat myFormatter = new DecimalFormat("$###,###.###");
String output = myFormatter.format(amount);
return output;
}
public static double paid()
{
temp = recall.recalldata();
String total ="";
double totald=0.0;
double totalamount =0.0;
String r = "P";
for(int j=4;j<c;j+=5)
{
if(r.equals(temp[j])
{
totald = Double.valueOf(temp[j-2]).doubleValue();
totalamount = (totalamount + totald);
}
}
return totalamount;
}
}>
this statement if(r.equals(temp[j]) does not come true even r is p and temp[j] is p
This code doesn't even compile. The line
f(r.equals(temp[j])
is missing a closing parenthesis. It should be
f(r.equals(temp[j]))
In the line
totald = Double.valueOf(temp).doubleValue();
you are attempting to pass a String array (temp) to the valueOf method. This method takes a String not a String array.
You didn't post the complete code so it is hard to tell what is in temp and r.
here is all the code
import ann.util.*;
import java.io.*;
class recall
{
public static String [] recalldata()
{
DataInputStream dataIn=null;
String s="";
int c=0;
c = LineCounter.c();
String [] test = new String[c];
try
{
try
{
dataIn = new DataInputStream(new BufferedInputStream(new FileInputStream("budget.dat")));
for(int a=0; a<c;a++)
{
test[a]=(dataIn.readLine()+"\n");
}
}
catch(EOFException e)
{
dataIn.close();
}
}
catch(Exception e)
{
Controller.fatal("main()", e.toString());
}
for(int b=0;b<c;b++)
{
if(test == null)
{
test="";
}
}
return test;
}
}
import java.io.*;
import ann.easyio.*;
class test
{
public static void main(String [] args)
{
Keyboard thekeyboard = new Keyboard();
String value ="";
int c=0;
int valueint=0;
c = LineCounter.c();
String [] array = new String[c];
array=recall.recalldata();
if((value !="q")||(value !="Q"))
{
System.out.print(mainstuff.menu());
System.out.println();
value = thekeyboard.readLine();
valueint = Integer.parseInt(value);
System.out.println(mainstuff.mainswitch(valueint));
}
System.exit(0);
}
}
and then the data file
First Federal Mortgage Company
109 Buena Vista Avenue Austin Texas 30669
1196.46
115532.12
P
Georgia Power Company
1109 Peachtree Street Atlanta Georgia 30607
46.29
46.29
N
Georgia Natural Gas Company
1400 Waycross Way Macon Georgia 30209
88.80
88.80
N
Southern Bell
119 14th Street Atlanta Georgia 30207
21.47
0.00
P
Sprint
2909 North Street Charlotte North Carolina 30806
42.87
0.00
P
Pegasus Satellite TV Service
2397 East Point Blvd Atlanta Georgia 30967
42.09
42.09
N
General Motors Acceptance Corporation
3432 Lawson Street Louisville Kentucky 30604
356.34
0.00
P
Capital One Bank MasterCard
PO Box 16 Steward Way Kansas City Kansas
25.00
142.00
P
Providian Bank Visa
1111 Olden Street New York NY 39990
10.00
37.16
N
Citibank Visa
6709 Smart Street St. Paul Minnisota 29029
150.00
3264.00
P
I am trying to make a budget program.
I am new at java and just trying figure things out here.>
"This code doesn't even compile. The linef(r.equals(temp[j])is missing a closing parenthesis. "that was a copy and paste error
hello is there anyone out there please help
> hello is there anyone out there please help
Yes, there are people out there.
If you'd like to get help, I suggest you:
- repost your current code using [code] and [/code] tags;
- explain in detail what isn't working;
- how it's supposed to work;
- provide compiler errors.
Please use code tags (see button above posting box). First your [ i ] made italics, then your [ b ] made bold.Compare Strings using .equals, not == (you have some != in there for Strings--that won't work).
MLRona at 2007-7-13 10:18:28 >

Now that I see more of your code and your data, the reason that the line:
f(r.equals(temp[j]))
doesn't return true when you expect it to isn't because the line is wrong. Your temp[j] doesn't contain a "P", it contains a "P\n" because this is what the recalldata method is putting into the array via the line
test[a]=(dataIn.readLine()+"\n");
Either don't append the "\n" or change r to "P\n" instead of "P".
By the way, the lines in recalldata:
if(test == null)
test="";
should be
if(test[b] == null)
test[b]="";
> By the way, the lines in recalldata:
> > if(test == null)
> test="";
>
> should be
> > if(test[b] == null)
> test[b]="";
>
I think that's what he had. Hence the sudden switch to bold in untagged code.
jverda at 2007-7-13 10:18:28 >

test[a]=(dataIn.readLine()+"\n");its works now,thanks so much, I forgot about that \n
Good point, jverd.Bigbencook, when posting code always enclose your code within [code] and [/code] tags. If you don't do that, your array indexes might be interpretted as display control. This is especially true if you use variable names "b" or "i" for your array index.