Computer Science Canada Simple Program Errors - Variables etc. |
Author: | Reira [ Thu Apr 03, 2008 5:21 pm ] | ||
Post subject: | Simple Program Errors - Variables etc. | ||
The following code keeps giving an "unexpected type" error and some other errors as well. Please help me find the mistakes and correct them... I am only one week into Java and already spent 3 fruitless hours on this thing, looking at tutorials, but can't find anything about this particular error. I hope you can help me get a fresh start with Java. This should be fairly easy for you to correct.
Thanks a lot. And yes, I'm a noob. |
Author: | HeavenAgain [ Thu Apr 03, 2008 5:41 pm ] | ||||
Post subject: | RE:Simple Program Errors - Variables etc. | ||||
when you have something like
next, your compiler should highlight the error for you, mostly the problem comes with your bad names for variable, missing a letter, or a word. look carefully at the variable names and correct them then we can improve your code further if you want |
Author: | Nick [ Thu Apr 03, 2008 6:00 pm ] |
Post subject: | RE:Simple Program Errors - Variables etc. |
(int) nickels = remainder2/5; I don't know if it matters but shouldn't it be nickels = (int) remainder2/5; |
Author: | Reira [ Thu Apr 03, 2008 6:03 pm ] | ||
Post subject: | Re: Simple Program Errors - Variables etc. | ||
Right, now this part works... thanks! Another thing... the program gets stuck on this line: remainer1 = krazyNumber - quarters*25; It there a problem with the way I did the calculation?
Thanks again. |
Author: | HeavenAgain [ Thu Apr 03, 2008 6:12 pm ] | ||
Post subject: | RE:Simple Program Errors - Variables etc. | ||
Quote: What I'm trying to do here is after the division, to eliminate any digits after the decimal point of the float value and to save that number as an int value
i can suggest you to try out the following code in a main
and again, remainer1, remainder1 , missed typed variable names what you have to understand is, how to cast, and what happens when you cast an value, and when you need to use casting |
Author: | Nick [ Thu Apr 03, 2008 6:13 pm ] | ||
Post subject: | RE:Simple Program Errors - Variables etc. | ||
see a problem? |
Author: | Reira [ Thu Apr 03, 2008 6:26 pm ] | ||
Post subject: | RE:Simple Program Errors - Variables etc. | ||
I've changed all remainder's to remainer's using find and replace. However, it is still stuck on that same line. It gives me "cannot find symbol - variable remainer1" Should I have declared the variable beforehand? Or is there just a calculation syntax problem?
So this should be the right syntax for casting, am I correct? |
Author: | HeavenAgain [ Thu Apr 03, 2008 6:37 pm ] | ||
Post subject: | RE:Simple Program Errors - Variables etc. | ||
you know what? o_o your "krazyNumber" should be a float, and quarters, dimes etc, should be int so when getting input, instead of nextInt(), you should be using nextFloat() and now as for your casting, you dont cast the float right away, you want it to do its calculation first, then cast it so it should be something like
and cannot find symbol error usually is when you are trying to use something that havn't been decleared, and so 'remainer1' must be decleared before using it |
Author: | Reira [ Thu Apr 03, 2008 7:46 pm ] |
Post subject: | RE:Simple Program Errors - Variables etc. |
Perfect! It works. Thanks a lot! |
Author: | HeavenAgain [ Thu Apr 03, 2008 7:53 pm ] | ||||
Post subject: | RE:Simple Program Errors - Variables etc. | ||||
no problem ![]() to start off, you should always have the first letter of your class name in capital, so in your case
and another thing
notice else if (blah);, there is no point having that there, because else if blah happens, you are going to do nothing anyways, so just move on. and the way of you block your codes, pretty weird |
Author: | Barbarrosa [ Thu Apr 03, 2008 8:37 pm ] |
Post subject: | Re: Simple Program Errors - Variables etc. |
Another note: If you use Eclipse, please indent everything properly. Keyboard shortcuts for this are: Ctrl+A (Select all) Ctrl+I (Indent selection) Other recommendations: NEVER assume the input is always going to be correct. Put that in a try/catch statement that consistently forces more input if there is an exception (for every time it occurs). *This is because of personal experience. Crashing the whole program seriously sucks when testing or debugging. I've seen people struggle because of it. [code] public static void main(String[] args) { // Loop testing class Scanner scan = new Scanner(System.in); boolean rep = false; int i = 0; while (rep == false){ try{ System.out.println("Integer input, please:"); i = scan.nextInt(); rep = true; } catch (Exception e) { scan.nextLine(); //java is still a little flawed... this is often necessary } System.out.println(""+i); } } [\code] Also, try your utmost to use significant variable names. Most of yours seem OK, but krazyNumber could be named better. You might also try constant values for how much a Quarter, Dime, and Nickel each are. Lastly, when dealing with coin values and the like, I would recommend more use of the "%" operator, as it gets you the remainder of any dividing operation [code] final int QUARTER_VALUE = 25; //value of a quarter int centsWOQuarters = firstTotal%QUARTER_VALUE; //total cents w/o quarters [\code] |
Author: | OneOffDriveByPoster [ Thu Apr 03, 2008 9:43 pm ] |
Post subject: | Re: Simple Program Errors - Variables etc. |
Barbarrosa @ Thu Apr 03, 2008 8:37 pm wrote: NEVER assume the input is always going to be correct. Put that in a try/catch statement that consistently forces more input if there is an exception (for every time it occurs).
The other solution is to use pipes and pipe your input. (Or if you use Eclipse, copy your prepared test input into the run area.) "ALWAYS" assume the input is always going to be correct in programming contests.*This is because of personal experience. Crashing the whole program seriously sucks when testing or debugging. I've seen people struggle because of it. |
Author: | HeavenAgain [ Thu Apr 03, 2008 9:48 pm ] |
Post subject: | RE:Simple Program Errors - Variables etc. |
sorry, maybe be offtopic here, but what is "pipes", and how do you "pipe" your input? never heard/used it before, care to show me some examples ![]() |
Author: | OneOffDriveByPoster [ Thu Apr 03, 2008 10:06 pm ] | ||||
Post subject: | Re: RE:Simple Program Errors - Variables etc. | ||||
HeavenAgain @ Thu Apr 03, 2008 9:48 pm wrote: sorry, maybe be offtopic here, but what is "pipes", and how do you "pipe" your input? never heard/used it before, care to show me some examples http://cs.senecac.on.ca/~albert.pang/ios100/doscmd.html (look at input redirection)
![]() Here is a file called cwd.txt. There is a program called DEBUG on most Windows computers where typing this in would print the current directory path.
Using input redirection:
|