Help with code...

D

DBJohnston0104

I'm trying to complete a homework assignment where a window asks for a
billType (ex: Rent, Food, Gas, etc.) and then the next window asks for
an amount. It should keep a running total of all bills entered and
continue to loop until you enter the word "done" as a billType. I
just don't understand why the code below doesn't work. Now I don't
want anyone to give me the answer or the correct code, but I would
like some hints. I'm really interested in getting this to work so
here I am. And here is what I have so far:

// MonthlyBills.java - This program calculates the total of your
monthly bills.
// Input: Bill type and bill amount.
// Output: Prints the total of your monthly bills.

import javax.swing.JOptionPane;

public class MonthlyBills
{
public static void main(String args[])
{

String billType; // Description of bill.
String stringAmount; // String version of bill amount.
double billAmount; // Amount of the bill.
double sum = 0; // Accumulates sum of bills.

/* You should set up your loop to execute as long as the user
has not entered the word done. You can use these input and
output statements anywhere in your program. They are not in
any particular order.
*/

do
{
// This input statement asks the user to enter a bill type or the
word none.
billType = JOptionPane.showInputDialog("Enter bill type or the word
done to quit.");

if(billType != "done")
{
// This input statement asks your user to enter a bill amount.
stringAmount = JOptionPane.showInputDialog("Enter amount of
bill");

// This statement converts the string version of the amount to a
double.
billAmount = Double.parseDouble(stringAmount);

//This totals up the amount of the bills
sum += billAmount;
}

}while(billType != "done");

// This statement displays the sum of monthly bills.
System.out.println("Sum of monthly bills is $: " + sum);

// This statement causes the program to exit.
System.exit(0);

} // End of main() method.

} // End of MonthlyBills class.
 
A

Andrew Thompson

I'm trying to complete a homework assignment ...

A good group for Java beginners is comp.lang.java.help
...And here is what I have so far:

That code was a pretty good description of your problem,
but the lines wrapped and had to be fixed before it would
compile and the error became clear to me.

To help avoid line-wrap in code, use the TWC
<http://www.physci.org/twc.jnlp>

Here is the fixed code, with comments. Note that this is an
SSCCE that spans only 68 characters width. Hopefully
it should not line-wrap.

<sscce>
import javax.swing.JOptionPane;

/* MonthlyBills.java - This program calculates the total
of your monthly bills.
Input: Bill type and bill amount.
Output: Prints the total of your monthly bills. */
public class MonthlyBills
{
public static void main(String args[])
{
/** Description of bill. */
String billType;
/** String version of bill amount. */
String stringAmount;
/** Amount of the bill. */
double billAmount;
/** Accumulates sum of bills. */
double sum = 0;

do
{
// This input statement asks the user to
// enter a bill type or the word none.
billType = JOptionPane.showInputDialog(
"Enter bill type or the word done to quit.");

// String comparison needs to be done using .equals()
// to check equality of the contents, as opposed to
// the String objects
if(!billType.equals("done"))
{
// This input statement asks your user
// to enter a bill amount.
stringAmount = JOptionPane.showInputDialog(
"Enter amount of bill");

// This statement converts the string version
// of the amount to a double.
billAmount = Double.parseDouble(stringAmount);

//This totals up the amount of the bills
sum += billAmount;
}

}while(!billType.equals("done"));

// This statement displays the sum of monthly bills.
System.out.println("Sum of monthly bills is $: " + sum);

// This statement causes the program to exit.
System.exit(0);

} // End of main() method.

} // End of MonthlyBills class.
</sscce>

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
 
R

Ravi

== and != does reference comparison. If you want to do object
comparison, use equals() method.
This is corrected by Andrew in his code snippet.

As an example, consider the following code
String str1 = new String("String");
String str2 = new String("String");
String str3 = str2;
System.out.println(str1==str2);
System.out.println(str2==str3);
System.out.println(str3==str1);

What does it print? Reason it out.

Regards,
Ravi.

I'm trying to complete a homework assignment ...

A good group for Java beginners is comp.lang.java.help
...And here is what I have so far:

That code was a pretty good description of your problem,
but the lines wrapped and had to be fixed before it would
compile and the error became clear to me.

To help avoid line-wrap in code, use the TWC
<http://www.physci.org/twc.jnlp>

Here is the fixed code, with comments. Note that this is an
SSCCE that spans only 68 characters width. Hopefully
it should not line-wrap.

<sscce>
import javax.swing.JOptionPane;

/* MonthlyBills.java - This program calculates the total
of your monthly bills.
Input: Bill type and bill amount.
Output: Prints the total of your monthly bills. */
public class MonthlyBills
{
public static void main(String args[])
{
/** Description of bill. */
String billType;
/** String version of bill amount. */
String stringAmount;
/** Amount of the bill. */
double billAmount;
/** Accumulates sum of bills. */
double sum = 0;

do
{
// This input statement asks the user to
// enter a bill type or the word none.
billType = JOptionPane.showInputDialog(
"Enter bill type or the word done to quit.");

// String comparison needs to be done using .equals()
// to check equality of the contents, as opposed to
// the String objects
if(!billType.equals("done"))
{
// This input statement asks your user
// to enter a bill amount.
stringAmount = JOptionPane.showInputDialog(
"Enter amount of bill");

// This statement converts the string version
// of the amount to a double.
billAmount = Double.parseDouble(stringAmount);

//This totals up the amount of the bills
sum += billAmount;
}

}while(!billType.equals("done"));

// This statement displays the sum of monthly bills.
System.out.println("Sum of monthly bills is $: " + sum);

// This statement causes the program to exit.
System.exit(0);

} // End of main() method.

} // End of MonthlyBills class.

</sscce>
 
D

DBJohnston0104

I'm trying to complete a homework assignment ...

A good group for Java beginners is comp.lang.java.help
...And here is what I have so far:

That code was a pretty good description of your problem,
but the lines wrapped and had to be fixed before it would
compile and the error became clear to me.

To help avoid line-wrap in code, use the TWC
<http://www.physci.org/twc.jnlp>

Here is the fixed code, with comments. Note that this is an
SSCCE that spans only 68 characters width. Hopefully
it should not line-wrap.

<sscce>
import javax.swing.JOptionPane;

/* MonthlyBills.java - This program calculates the total
of your monthly bills.
Input: Bill type and bill amount.
Output: Prints the total of your monthly bills. */
public class MonthlyBills
{
public static void main(String args[])
{
/** Description of bill. */
String billType;
/** String version of bill amount. */
String stringAmount;
/** Amount of the bill. */
double billAmount;
/** Accumulates sum of bills. */
double sum = 0;

do
{
// This input statement asks the user to
// enter a bill type or the word none.
billType = JOptionPane.showInputDialog(
"Enter bill type or the word done to quit.");

// String comparison needs to be done using .equals()
// to check equality of the contents, as opposed to
// the String objects
if(!billType.equals("done"))
{
// This input statement asks your user
// to enter a bill amount.
stringAmount = JOptionPane.showInputDialog(
"Enter amount of bill");

// This statement converts the string version
// of the amount to a double.
billAmount = Double.parseDouble(stringAmount);

//This totals up the amount of the bills
sum += billAmount;
}

}while(!billType.equals("done"));

// This statement displays the sum of monthly bills.
System.out.println("Sum of monthly bills is $: " + sum);

// This statement causes the program to exit.
System.exit(0);

} // End of main() method.

} // End of MonthlyBills class.

</sscce>

Andrew, you're my hero! I have spent several nights looking at this
simple program and it was driving me nuts. I don't think that I can
turn in your answer but I do thank you for your help. In our book it
has example of all the operators and what they mean but not so much as
to how to use them. The ! in front of the billType I would have never
figured out. Thanks again!

So when you refer to a string you have to use .value() to check for
equality but not for double? Is that right?

Thanks again,
David
 
A

Andrew Thompson

So when you refer to a string you have to use .value() to check for
equality

That is probably what happens, though I have never looked into
it that closely. (shrugs) I just use .equals().
..but not for double? Is that right?

My understanding is 'double' no, but 'Double' yes.
The primitives are open for direct comparison, as you surmise -
all objects need to be compared using the valueOf or toString()
or a defined comparator.

OTOH, there is a "gotcha" when it comes to floating point
representations of numbers. They should never be compared
for equality at all, since two doubles that appear on screen as
(hypothetical WAG)
1.00000000004 &
1.00000000004
..may *not* be equal. It comes down to the reality that
digital computers do not store floating point numbers
exactly.

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
 
A

Andrew Thompson

... I don't think that I can turn in your answer ..

Yeah, sorry. I even had your comment that this was 'homework'
in the back of my mind as I was looking over the code.

The thing is, I wanted to whow how short and convenient SSCCE
code can be, and besides some trivial changes to comments and
line lengths, that posted code is *very* similar to the code you
posted - only two lines are substantively different. Since you
obviously understand the 'how and why' - you can easily adapt
that back into your original code.

BTW - It seemed odd that comparison was done twice. It might
be a good idea to ask for 'stylistic/design advice' once the code
seems to be working. I make a point of trying to avoid making too
many changes to any piece of code that has a specific problem.
Instead I just fix the stated problem, & post the fixed version
back to the group.

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
 
D

DBJohnston0104

Yeah, sorry. I even had your comment that this was 'homework'
in the back of my mind as I was looking over the code.

The thing is, I wanted to whow how short and convenient SSCCE
code can be, and besides some trivial changes to comments and
line lengths, that posted code is *very* similar to the code you
posted - only two lines are substantively different. Since you
obviously understand the 'how and why' - you can easily adapt
that back into your original code.

BTW - It seemed odd that comparison was done twice. It might
be a good idea to ask for 'stylistic/design advice' once the code
seems to be working. I make a point of trying to avoid making too
many changes to any piece of code that has a specific problem.
Instead I just fix the stated problem, & post the fixed version
back to the group.

Thanks again. I am on a very, very basic level of Java. Honestly, I
don't even consider myself as even being on a level yet. But anyhow,
I had the two comparisons in there because it was the only way that I
could think of to make it exit the loop as soon as the billType "done"
was entered. Most of that code was already written in our assignment
and we just had to add the loop.

Is there a good webpage that's designed for beginners or is real easy
to understand and make use of. I can see myself really liking Java
but am a bit cloudy as I'm just starting off. Any favorites you'd
like to share?
Thanks,
David
 
R

Roedy Green

String stringAmount; // String version of bill amount.
double billAmount; // Amount of the bill.
double sum = 0; // Accumulates sum of bills.

Don't define local variables any sooner than you have to. Define them
at first use, and in the innermost nest possible.
 
R

Roedy Green

do
{
// This input statement asks the user to enter a bill type or the
word none.
billType = JOptionPane.showInputDialog("Enter bill type or the word
done to quit.");

if(billType != "done")
{
// This input statement asks your user to enter a bill amount.
stringAmount = JOptionPane.showInputDialog("Enter amount of
bill");

// This statement converts the string version of the amount to a
double.
billAmount = Double.parseDouble(stringAmount);

//This totals up the amount of the bills
sum += billAmount;
}

}while(billType != "done");

Try rearranging your loop so there is only one "done" test. Hint
"break" out of while(true) is a way of stopping a loop too.
 
D

DBJohnston0104

Try rearranging your loop so there is only one "done" test. Hint
"break" out of while(true) is a way of stopping a loop too.
--
Roedy Green Canadian Mind Products
The Java Glossaryhttp://mindprod.com- Hide quoted text -

- Show quoted text -

Thanks for all the help! I've re-downloaded the source file for the
assignment and, using what I've learned here, rewrote the code... and
it works. It's pretty exciting to be "officially" learning a
programming language. I do quite a bit of VBA in MS Office apps but
it's all been self-taught so this is a change for me. Anyway...
Thanks again!
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top