Program: Change Due/Tendered

Benjamin Chau
Ranch Hand
Posts: 49
Here's a program that asks the user (a customer buying something) to enter two numbers: the amount owed and the amount tendered. It then figures out and displays the amount of change owed. My question to all is if I did it right. Are there any glaring mistakes y'all can see? The program has to loop, per the instructions provided below, so I chose a do...while statement and asked the user if he'd like to enter another set of amounts. I used the == symbol since I'm thinking the program is comparing the user's answer with the value for answer present (which I'm thinking is null). I tried to cover all forms of the answer "yes" (don't know if that's necessary) and used a single vertical-line symbol | for the Or operator since it's not Boolean. Thanks for all your help.

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.

More newlines to avoid very long lines. CR[/edit]
[ November 08, 2008: Message edited by: Campbell Ritchie ]

Henry Wong
author
Marshal
Posts: 21190
80
My question to all is if I did it right. Are there any glaring mistakes y'all can see?

Does the program compile? Does the program run? When you run it, does it do what it is supposed to do? And for a range of test data?

Answering those questions is much more productive than posting a wad of code, and asking for glaring mistakes.

Henry

marc weber
Sheriff
Posts: 11343
The logical "or" operator is used with boolean (or Boolean) operands. (See JLS - 15.22.) As you've discovered, attempting something like...

...will fail to compile, because String literals are not booleans.

Also, you should generally not attempt to compare Strings using the == operator. Instead, use String's equals method. See Strings, Literally.

[ November 07, 2008: Message edited by: marc weber ]

Benjamin Chau
Ranch Hand
Posts: 49
Marc,

Thanks for trying to help out. I posted this same message in one of my other posts but I wanted to make sure you see it too. I'm having trouble changing the Path Variable.

Of course, this is the message I get when I try to compile because the Path is incorrect:

'javac' is not recognized as an internal or external command,
operable program or batch file.

Others have tried to help me out and I've done as their advice says (change the Environmental Path Variable) but I still can't get it right. I'm running XP Media Edition and here's exactly what I'm doing:

1) I pull up Control Panel then double-click the System icon.

2) I click on the Advanced tab, then the box that reads: Environment Variables.

3) I click the button New under the System variables space and when a box pops up I type in Variable name and the Variable value, using JAVA as the name and the following directory for the value (since that's where the Java software is located on my computer). Here's the exact directory I type in for the Variable value:

C:\Program Files\Java\jre1.5.0_06\bin

4) I repeat the exact same steps as above (3)) for the space that reads: User variables for my name.

5) I open my Command Prompt window and try to compile a program, javac HelloWorld, for instance. Then I get that error message again.

'javac' is not recognized as an internal or external command,
operable program or batch file.

Now when I open up the Command Window and look at the default Path Variable, I see the same directory as always, unchanged, despite all the steps I've taken as described above. Here's the default Path Variable that I see in Command Prompt that never seems to go away:

C:\Documents and Settings\my name>

Any suggestions would be greatly appreciated...

Benjamin

Campbell Ritchie
Sheriff
Posts: 49370
62
Originally posted by Benjamin Chau:
Marc,

. . .
Now when I open up the Command Window and look at the default Path Variable . . here's the default Path Variable that I see in Command Prompt . . .

C:\Documents and Settings\my name>
That's not your PATH, that's your "current directory." You can change that with the cd command.

Go to Windows Explorer, open C:\, open Program Files, open Java, open jdk1.5.0_06, open bin. You should find a list of executable files there, including java.exe, javac.exe, javap.exe, appletviewer.exe, about 30 of them.

If you find them, then the entire series of folders from C: to bin is your PATH.

If you don't find javac.exe anywhere, then you haven't installed Java properly.

If that is the case, download the most recent version of Java which is called Java SE Development Kit (JDK) 6 Update 10, from this website. Make sure it has JDK in its name. Not JRE.

Then install it in the usual fashion. When it says "install in this directory: C:\Program Files\Java\jdk1.6.0_10" you can install it elsewhere, but write down carefully where you put it; when you add \bin that will become your PATH.

You have set your PATH incorrectly. This is what you ought to do:
• Go to System Properties->advanced->environment variables->system variables->PATH->edit.
• Add the PATH to what is already there, separating it from the other entries with a ;
• Delete any old java PATH entries.
• The PATH entry will start with C:\ and end with bin
• No need for a user variable; you want that PATH for all users.
• Delete the entries you created earlier called JAVA
• Go to the CLASSPATH variable
• Make sure there is . or .; or ;. or ;.; somewhere in it. Beginning middle or end, doesn't matter.
• You do not usually need to change your CLASSPATH as long as the . appears somewhere.
• Close the command line and open it anew.
• And good luck with it
•
marc weber
Sheriff
Posts: 11343
Originally posted by Benjamin Chau:
...I click the button New under the System variables space and when a box pops up I type in Variable name and the Variable value, using JAVA as the name and the following directory for the value...

Your system should already have a variable called "PATH," and you need to update this value instead of creating a new variable of your own.

It's important to leave anything that's already in the PATH variable as it is. All you want to do is add your java path to the end of what's already listed.

In Windows, use a semicolon to separate your new value from the others, and be sure you do not add any extra spaces.

[ November 08, 2008: Message edited by: marc weber ]

Benjamin Chau
Ranch Hand
Posts: 49
Hi Campbell,

OK I installed the Java software again and am going through the process of changing the Path Variable per your instructions. My questions are: 1) I don't see the CLASSPATH variable in the list of System variables. Where else can I find it? 2) There is a PATHEXT variable in the list of System variables which lists contents as:
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH. Do I use this one? I also see a Path variable but its contents are: C:\Program Files\PC Connectivity Solution\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\. Again, I'm not sure which one I use...

Thanks much,
Benjamin

Originally posted by Campbell Ritchie:
You have set your PATH incorrectly. This is what you ought to do:
• Go to System Properties->advanced->environment variables->system variables->PATH->edit.
• Add the PATH to what is already there, separating it from the other entries with a ;
• Delete any old java PATH entries.
• The PATH entry will start with C:\ and end with bin
• No need for a user variable; you want that PATH for all users.
• Delete the entries you created earlier called JAVA
• Go to the CLASSPATH variable
• Make sure there is . or .; or ;. or ;.; somewhere in it. Beginning middle or end, doesn't matter.
• You do not usually need to change your CLASSPATH as long as the . appears somewhere.
• Close the command line and open it anew.
• And good luck with it
• [/qb]

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

Campbell Ritchie
Sheriff
Posts: 49370
62
You don't create a JAVA environment variable and you don't use PATHEXT.

You use PATH as a system variable (so any user can use it).

Your present path looks like a normal PATH. It reads C:\Program Files\PC Connectivity Solution\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\ so you add

C:\Program Files\Java\jdk1.6.0_10\bin\ to it with ; separating the successive entries. Since you haven't set a JAVAHOME variable you don't need any %%

If there isn't a CLASSPATH variable at all, create one as a System variable and the new CLASSPATH should contain . That's all one . The . means "current directory." There is no need to add anything else to the CLASSPATH.

Campbell Ritchie
Sheriff
Posts: 49370
62
Originally posted by Campbell Ritchie:
You don't create a JAVA environment variable and you don't use PATHEXT.

You use PATH . . . add

C:\Program Files\Java\jdk1.6.0_10\bin\ to it . . .

Don't alter what is already in PATHEXT. The bit about jdk1.6.0_10 assumes you used that version and that you put it in its default location in Program Files.

marc weber
Sheriff
Posts: 11343
Originally posted by Campbell Ritchie:
...If there isn't a CLASSPATH variable at all, create one as a System variable and the new CLASSPATH should contain . That's all one . The . means "current directory." There is no need to add anything else to the CLASSPATH.

Or... Just don't create any CLASSPATH system variable at all. In the absence of an explicit CLASSPATH, Java's default is to check the current directory, so you really don't need one at this point.

Benjamin Chau
Ranch Hand
Posts: 49
Still didn't work...

Campbell Ritchie
Sheriff
Posts: 49370
62
What didn't work? Tell The Details in words of one syllable.

• Which version of Java did you install?
• Which folder did you install it to? It's usually C:\Program Files\Java|jdk1.6.0_01\bin or similar.
• Post your PATH environment variable, both user and system, using ctrl-C ctrl-V so as to post it exactly.
• Post your CLASSPATH variable, user and system versions, similarly.
• Post a snippet from your command-line, again with copy and paste.
• There is more, but I shall have to reboot before I can tell you that.

Campbell Ritchie
Sheriff
Posts: 49370
62
Microsoft Windows XP [Version 5.1.2600]

C:\Documents and Settings\Campbell>PATH
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program Files\Em
acs\\bin;C:\Program Files\Java\jdk1.6.0_10\bin;C:\Program Files\MySQL\MySQL Serv
er 5.0\bin;C:\Program Files\Microsoft Office\OFFICE11\Business Contact Manager\I
M;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsof
am Files\emacs-22.1-bin-i386\emacs-22.1\bin;C:\Program Files\Microsoft SQL Serve
r\90\Tools\binn\

C:\Documents and Settings\Campbell>SET PATH=C:\Program Files\Java\jdk1.6.
0_01\bin

C:\Documents and Settings\Campbell>javac -version
'javac' is not recognized as an internal or external command,
operable program or batch file.

C:\Documents and Settings\Campbell>SET PATH=C:\Program Files\Java\jdk1.6.
0_10\bin

C:\Documents and Settings\Campbell>javac -version
javac 1.6.0_10-beta

C:\Documents and Settings\Campbell>
This copy-and-paste shows the PATH on the Windows half of my PC. Note the spelling of the Java part of the PATH. It also shows how to set up the PATH temporarily for the lifetime of the window, and what happes if you write 01 instead of 10 in the PATH!

Please post the exact error messages you are seeing.

Campbell Ritchie
Sheriff
Posts: 49370
62
Please check your PATH and CLASSPATH like that. As we told you, you only need a . in the CLASSPATH, and may not even need that. The tiniest spelling error in the PATH or a space wrong will break your installation, but Windows is nOt CaSe SeNsItIvE.

Campbell Ritchie
Sheriff
Posts: 49370
62
And the line breaks at "1.6.
0_01" are not real line breaks, but where the writing reached the edge of the command line window.

Benjamin Chau
Ranch Hand
Posts: 49
Campbell,

I didn't quite understand what you meant by using ctrl-C and ctrl-V, but here's the directory where my development kit resides:

C:\j2sdk1.4.2_13

Here's the value for the Path Variable:

C:\Program Files\PC Connectivity Solution\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\j2sdk1.4.2_13

Here's the value for my CLASSPATH Variable:

.

There's nothing in the User Variables box. I don't think it's a problem with cutting/pasting the wrong thing...

Someone tried to help me today with this and they said the area where my programs were saved has to be somewhat in the same area where my development kit is. He did cd in Command Prompt to try to adjust for this but it still didn't work (did not compile) - same error message. I can't seem to copy/paste from the Command Prompt.

Benjamin

Originally posted by Campbell Ritchie:
What didn't work? Tell The Details in words of one syllable.

• Which version of Java did you install?
• Which folder did you install it to? It's usually C:\Program Files\Java|jdk1.6.0_01\bin or similar.
• Post your PATH environment variable, both user and system, using ctrl-C ctrl-V so as to post it exactly.
• Post your CLASSPATH variable, user and system versions, similarly.
• Post a snippet from your command-line, again with copy and paste.
• There is more, but I shall have to reboot before I can tell you that.

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

Campbell Ritchie
Sheriff
Posts: 49370
62
The JAVA part of the PATH has to end with bin. You need to add \bin to the jdk entry.

And why are you still using Java 1.4.2? It was superseded by Java5 4 years ago.

Campbell Ritchie
Sheriff
Posts: 49370
62
Putting your own files in the jdk folder is usually a mistake. Create a JavaWork folder somewhere and always use that.

The CLASSPATH with . only looks correct.

You are probably better off using the most recent edition of Java, but get your present installation working first. You can un-install your old Java, install a new Java version, and change the version numbers in the PATH easily enough.

To copy and paste from a Windows command window:
• Click the icon at the top left.
• Click edit->mark.
• Highlight the text required with your mouse.
• Push "enter" to copy to clipboard.

• Try opening a command window and enter

SET PATH=C:\j2sdk1.4.2_13\bin;%PATH%

and see whether you can get javac to work. If it does, it confirms you have a working Java installation.

It is so frustrating when you can't get anything to work like this, isn't it.
[ November 11, 2008: Message edited by: Campbell Ritchie ]

Benjamin Chau
Ranch Hand
Posts: 49
Campbell,

Thanks for sticking with me on this for so long. After tagging \bin to the end of the Environment Variable didn't do anything different, I typed the command you gave me into Command Prompt:

SET PATH=C:\j2sdk1.4.2_13\bin;%PATH%

I cut and paste my output below. Thanks to you, I know how to do that now. By the look of the output, I might be making headway??? Oh, and about putting all my programs into a JavaWork folder, if I have them all in a file but the path is long (for instance, I go through several folders to get to it like this:

Does that usually cause the compiler to have a hard time locating/compiling it???

Here's what I cut and paste from Command Prompt:

[code]

C:\Documents and Settings\Benjamin Q. Chau>SET PATH=C:\j2sdk1.4.2_13\bin;%PATH%

C:\Documents and Settings\Benjamin Q. Chau>javac HelloWorld
javac: invalid flag: HelloWorld
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are us
ed
-classpath <path> Specify where to find user class files
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-d <directory> Specify where to place generated class files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-help Print a synopsis of standard options

C:\Documents and Settings\Benjamin Q. Chau>

[\code]

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

Campbell Ritchie
Sheriff
Posts: 49370
62
You don't put the java work folder into your CLASSPATH at all.
You write

at the command prompt, or you do it in stages

cd School
cd SchoolName
cd ClassName
cd Assignments
cd ProgramsWritten
cd Program

until you get to the folder you want.

I don't usually like moving smilies, but I shall use one now:

The fact that you are getting that sort of error message is evidence that you have got a valid PATH. Success!

You are supposed to write

javac HelloWorld.java

then

java HelloWorld

Try that

Campbell Ritchie
Sheriff
Posts: 49370
62
But you have to be in the correct folder with your command line before you try.

Why not create a java folder in "my documents"? When you open the command line it points to "my documents" by default, so you just need to write

cd java

and you are the correct place

Maybe it's "documents and settings" where you should create the "java" folder.[/edit]
[ November 12, 2008: Message edited by: Campbell Ritchie ]

Benjamin Chau
Ranch Hand
Posts: 49
AWESOME!!! Thanks so much Campbell.

One last hurdle - I placed all my programs into a folder I named "WRITTENPROGRAMS" and moved it into my Documents and Settings folder and thus its directory is now:

C:\Documents and Settings\WRITTENPROGRAMS

Then I ran some Command Prompt commands and here's what I got:

Microsoft Windows XP [Version 5.1.2600]

C:\Documents and Settings\Benjamin Q. Chau>SET PATH=C:\j2sdk1.4.2_13\bin;%PATH%

C:\Documents and Settings\Benjamin Q. Chau>cd WRITTENPROGRAMS
The system cannot find the path specified.

C:\Documents and Settings\Benjamin Q. Chau>CD WRITTENPROGRAMS
The system cannot find the path specified.

C:\Documents and Settings\Benjamin Q. Chau>cd "WRITTENPROGRAMS"
The system cannot find the path specified.

C:\Documents and Settings\Benjamin Q. Chau>javac HelloWorld.java
1 error

C:\Documents and Settings\Benjamin Q. Chau>

Now, what am I doing wrong? aaarrgghh!

Henry Wong
author
Marshal
Posts: 21190
80
You are in the "C:\Documents and Settings\Benjamin Q. Chau" directory. You want to get to the "C:\Documents and Settings\WRITTENPROGRAMS" directory.

Do you think "cd WRITTENPROGRAMS" will get you there?

Now, what am I doing wrong? aaarrgghh!

I think you need to step back and take a rest. This is not a Java issue. This a change to the right directory issue...

Henry
[ November 12, 2008: Message edited by: Henry Wong ]

Benjamin Chau
Ranch Hand
Posts: 49
Henry,

I really don't appreciate the attitude. This is the second time I've noticed it. I'm in no way an expert, like yourself. If you disagree or disapprove of my posts, please let me know politely, and I will act accordingly.

Perhaps my thinking was way too irrational but I had thought that taking care of the directory path in order to configure the right Environmental Variable in order to run Java programs correctly IS within the subject matter of Java???

Benjamin

Do you think "cd WRITTENPROGRAMS" will get you there?

I think you need to step back and take a rest. This is not a Java issue. This a change to the right directory issue...

[ November 12, 2008: Message edited by: Henry Wong ][/QB]

Henry Wong
author
Marshal
Posts: 21190
80

I really don't appreciate the attitude. This is the second time I've noticed it. I'm in no way an expert, like yourself. If you disagree or disapprove of my posts, please let me know politely, and I will act accordingly.

The answer to you question was in the post. I sometimes post in a somewhat sarcastic manner, when it brings a bit of levity. For this, I am sorry.

I guess you have chosen to take my response as attitude, and simply ignore the answer (hint) that was in the response.

And BTW, one of the reasons why I rarely answer a question directly, is because people learn better when they figure out someone for themself. IMO, a hint (a question) is better than the answer.

Henry
[ November 12, 2008: Message edited by: Henry Wong ]

Benjamin Chau
Ranch Hand
Posts: 49
Campbell,

SUCCESS !!! Thanks to Henry's last post, a cocktail mixture of sarcasm, seasoned Java wit, and perhaps a smidgen offering of genuine help and assistance, I moved the WRITTENPROGRAMS folder to the folder with my name and now I can compile/run!!! Hallelujah!

Thanks so much...

Benjamin

Benjamin Chau
Ranch Hand
Posts: 49
Originally posted by Henry Wong:

I guess you have chosen to take my response as attitude, and simply ignore the answer (hint) that was in the response.

[ November 12, 2008: Message edited by: Henry Wong ]

Henry, I acknowledged it and used it. I can now run my programs, finally. See my last post. Thanks!

Campbell Ritchie
Sheriff
Posts: 49370
62
Success