Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!

Benjamin Chau

Ranch Hand
+ Follow
since Oct 05, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Benjamin Chau

Thanks Campbell, I revised. Got a few more questions about this program if anyone can help, that would be greatly appreciated...

1. What's the syntax to make the display extend accuracy on decimal numbers? For instance I want to display $5.50 instead of $5.5.
2. I still don't have syntax to check if the user enters valid characters for the amountTendered and amountOwed. Any suggestions anyone? I think I still need to use an if...else statement.
3. In the last statement of my do...while loop, I want to also cover the possibility of the user just entering a "Y" or "y" instead of a "Yes" answer. Can I add on arguments somehow. I tried this but it didn't work:

while (answer.equalsIgnoreCase("YES" || "Y"))

Regardless, here's the code that works. I just want to refine the program a bit more.

10 years ago

Originally posted by Rob Prime:

Make that

Saves you the creation of the upper case string.




Rob, thanks for your help! I like that syntax a lot better than what I was using. Simplified is good. I'm still getting buckoo errors when I try to compile but I will post the working code when I have it. Thanks again!

Benjamin
10 years ago

Originally posted by Ernest Friedman-Hill:
There's also a "hasNextDouble()", which returns true if calling nextDouble() will work (i.e., won't throw an exception.)




Ernest, thanks for your help! We haven't covered exceptions yet being this is an intro. class. I looked up the hasNextDouble() method you suggested. It looks like something I can definitely use but the textbook was vague on it. Is it a method or a boolean variable. If it's a boolean I'd have to declare it as such before using but the "()" indicates it's a method???

Here's the section of code in question that I modified with hasNextDouble().



When I tried to compile, I got 67 errors. I'm surely needing more help. Thanks!

Benjamin

[ November 22, 2008: Message edited by: Benjamin Chau ]
[edit]New lines to avoid very long lines. CR[/edit]
[ November 22, 2008: Message edited by: Campbell Ritchie ]
10 years ago
Sorry about that Fred. My instructor apparently got many assignments from the Cattle Drive.

To the Moosehead Saloon: Thanks for all the great programming assignments I've submitted so far, including this one, "Time". I've been learning a lot!
10 years ago
My other post for this program took on a different path so I'd like to address this program again, since I'm still trying to figure it out. Here it is...

INSTRUCTIONS:
You are to create a simple change program. The user enters the amount due and the amount tendered. You are to calculate how much change they should receive. You are then to break down the change in $1 bills, quarters, dimes, nickels, and pennies such that the user will receive the least amount of coins. For example, $0.80 is 3 quarters and a nickel not 8 dimes or 80 pennies.

Remember to make the program user-friendly. Check for non-numeric characters and insufficient amounts tendered. Have the program loop so that I can test different cases.



I haven't tried to compile yet because of a few issues. The program is to determine if the user's input is valid. Is there certain syntax I can accomplish this? I know that my syntax above is wrong but that's what I want to accomplish. Here's the section of that code:



Here's what I'm thinking that would work better for that section of syntax. I didn't see any harm in assigning a variable to itself.



Any thoughts? Any help would be greatly appreciated. Thanks!

Benjamin

[ November 21, 2008: Message edited by: Benjamin Chau ]

[ November 21, 2008: Message edited by: Benjamin Chau ]
[ November 21, 2008: Message edited by: Benjamin Chau ]
10 years ago
I misplaced/added too many variables. Here's the code that works!

10 years ago
Gamini,

The alignment was off. I added editing tags. Check it now...
10 years ago
Henry, you're right about the carriage return. Thanks. Here's my output after getting rid of the "ln". It's getting closer - the organization of data is looking better but the logical output is strange now.

C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>java Times
0 1 2 3 4 5 6 7 8 900 000 000 000 000 000 000 000 000 000
011 011 111 211 311 411 511 611 711 811 922 022 222 422 622 822
1022 1222 1422 1622 1833 033 333 633 933 1233 1533 1833 2133 2433 2744 044
444 844 1244 1644 2044 2444 2844 3244 3655 055 555 1055 1555 2055 2555 3055
3555 4055 4566 066 666 1266 1866 2466 3066 3666 4266 4866 5477 077 777 1477
2177 2877 3577 4277 4977 5677 6388 088 888 1688 2488 3288 4088 4888 5688 6488
7299 099 999 1899 2799 3699 4599 5499 6399 7299 81
10 years ago
Henry, you're a genius. That was definitely the problem.

But now, I have another problem. Turns out I didn't need to declare a static variable "display" afterall since I'm really only needing it in one block of code. However, I reworked all the code and now it'll compile but the output is unorganized - not like it's supposed to look. Will you take a look at my code?



The programs looks like it's trying to display correctly but something is not right or out of place. I'll go back and edit/cut this post down to save space on the JavaRanch website but just want you to see it first.

Here's the output from the Command Prompt after I compiled and ran it:

C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>java Times
0 1 2 3 4 5 6 7 8 90
0 0
0
0 0
0
0 0
0
0 0
0

etc. etc. etc.
[ November 19, 2008: Message edited by: Benjamin Chau ]
10 years ago
Thanks All!

Campbell, I may need your help if I can't figure out how to set the Environment Variable correctly for the new install. If you I do the steps like last time, maybe it'll go smoothly but we'll see...

<keeping fingers crossed>
10 years ago
OK, I'm having trouble with another program. This program is for a multiplication table using nested loops. Here are the instructions:

Purpose: To learn how to convert integers to strings, how to format text, how to use nested loops.
Write a program that (algorithmically) shows a times table.
In other words, I want to type
java Times
and see

To do this, you will need to know how to:

use nested loops
convert integers to strings
format text

Here's the code I have so far:



And, here's the error message I get when I try to compile:

C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>javac Times.java
Times.java:15: cannot resolve symbol
symbol : variable display
location: class Times
display += i;
^
Times.java:20: cannot resolve symbol
symbol : variable display
location: class Times
display += " " + i *

^
Times.java:22: cannot resolve symbol
symbol : variable display
location: class Times
display += " " + i * j

^
Times.java:26: cannot resolve symbol
symbol : variable display
location: class Times
display += "\n";
^
Times.java:27: cannot resolve symbol
symbol : variable display
location: class Times
System.out.println(display);
^
5 errors

Now, I'm thinking the display variable can be used as such when I declare it as a string and set it equal to "" (which I understand to mean null). I write "display +=" because I'm thinking this involves a concatenation operation and you can do this with strings? But when you do it with integers it becomes an arithmetic operation? I'm still a little fuzzy about that. I believe the errors point to the display variable as being the main culprit why the program is not compiling.

Also, I'm still having trouble determining when I use the capital letter as in "String" to declare a variable as opposed to when to use the lowercase letter such as in "string". I know that the capital references the class, in this case, the String class, but when specifically, are each used?

Thanks all. Any help would be greatly appreciated...

Benjamin
[ November 19, 2008: Message edited by: Benjamin Chau ]
10 years ago
Question about using the older versions of Java:

I think this is the version of my Java development kit software: j2sdk1.4.2_13

Since this is an older version I've been told that I don't have certain utilities such as the java.util.Scanner. Is this true or am I just not accessing the utility correctly in the development kit?

When I move the development kit to the right directory to coordinate with my Environment Variable, the program can still not access the Scanner utility.
10 years ago
Yep, nice and simple. And you get a good feeling of accomplishment!
10 years ago
Thanks Campbell. That was silly of me to use boolean as a variable. These late nighters screw with your mind I guess. I'm going back to edit the code now.

James suggested I change the syntax around a bit in one of his earlier posts but I've been thinking that that would change the logic and make the Leap Year program not work correctly. So I took that into account in my editing...

Benjamin
10 years ago
That's it! YOU'RE AWESOME! THANKS! Don't know why I used the boolean variable that way...

Benjamin
10 years ago