Simple Program Errors - Variables etc.
Author |
Message |
Reira
|
Posted: 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.
code: |
import java.io.InputStreamReader;
import java.util.Scanner;
public class assssss
{
public static void main (String [] args)
{
int nickels = 0;
int dimes = 0;
int quarters = 0;
{
Scanner abcdefReader = new Scanner(System.in);
System.out.println("Please input an about of cents amounting to less than one dollar.");
int krazyNumber = abcdefReader.nextInt ();
if ( krazyNumber > 100 )
System.out.println("you suck");
else if ( krazyNumber <= 99 );
(int) quarters = number/25;
if ( quarters != 0 )
System.out.println("The amount of nickels is " + quarters);
else if ( quarters == 0 )
System.out.println(" ");
{
int remainer1 = krazyNumber - quarters*25;
(int) dimes = remainder1/10;
}
if ( krazyNumber > 100 )
System.out.println("you suck");
else if ( krazyNumber < 99);
(int) quarters = krazyNumber/25;
if ( quarters != 0 )
System.out.println("The amount of nickels is " + quarters);
else if ( quarters == 0 )
System.out.println(" ");
{
int remainer1 = krazyNumber - quarters*25;
(int) dimes = remainder1/10;
}
if (dimes != 0)
System.out.println("The amount of dimes is " + dimes);
else if ( dimes == 0 )
System.out.println(" ");
{
int remainder2 = remainder1 - dimes*10;
(int) nickels = remainder2/5;
}
if (nickels != 0)
System.out.println("The amount of nickels is " + nickels);
else if ( nickels == 0 )
System.out.println(" ");
int remainder3 = remainder2 - nickels*5;
if (remainder3 != 0)
System.out.println("The amount of cents is " + remainder3);
else if ( remainder3 == 0 )
System.out.println(" ");
else
System.out.println ("you suck!");
}
}
}
|
Thanks a lot. And yes, I'm a noob. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
HeavenAgain
|
Posted: Thu Apr 03, 2008 5:41 pm Post subject: RE:Simple Program Errors - Variables etc. |
|
|
when you have something like code: | (int) quarters = number/25; | what the (int) does is to cast the item into an int value, which you dont need here, since number should be an int, and int divided by int gives you int, so it really should be code: | quarters = number/25; | without the (int), you can apply this change to about 5 other erros occured
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 |
|
|
|
|
|
Nick
|
Posted: 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; |
|
|
|
|
|
Reira
|
Posted: 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?
code: |
//Write a program that makes change for amounts less than one dollar.
//Input to the program should be a positive integer less than 100
//(check to make sure it is less than 100). This represents an amount of money
//in cents. Output should be the original amount of money together with a set
//of coins (quarters, dimes, nickels and cents) that could make up that amount.
//The program should produce change containing the minimum amount of the coins
//for the amount. The output should be in natural form.
// E.g. 58 cents : 2 quarters, 1 nickel, and 3 cents
//NOT 58 cents : 2 quarters, 0 dimes, 1 nickels, 3 cents
import java.io.InputStreamReader;
import java.util.Scanner;
public class assssss
{
public static void main (String [] args)
{
float nickels = 0;
float dimes = 0;
float quarters = 0;
{
Scanner abcdefReader = new Scanner(System.in);
System.out.println("Please input an about of cents amounting to less than one dollar.");
int krazyNumber = abcdefReader.nextInt ();
if ( krazyNumber > 100 )
System.out.println("you suck");
else if ( krazyNumber <= 99 );
quarters = (int) krazyNumber/25;
if ( quarters != 0 )
System.out.println("The amount of nickels is " + quarters);
else if ( quarters == 0 )
System.out.println(" ");
{
remainer1 = krazyNumber - quarters*25;
dimes = remainder1/10;
}
if ( krazyNumber > 100 )
System.out.println("you suck");
else if ( krazyNumber < 99);
quarters = (int) krazyNumber/25;
if ( quarters != 0 )
System.out.println("The amount of nickels is " + quarters);
else if ( quarters == 0 )
System.out.println(" ");
{
remainer1 = krazyNumber - quarters*25;
dimes = (int) remainder1/10;
}
if (dimes != 0)
System.out.println("The amount of dimes is " + dimes);
else if ( dimes == 0 )
System.out.println(" ");
{
int remainder2 = remainder1 - dimes*10;
nickels = (int) remainder2/5;
}
if (nickels != 0)
System.out.println("The amount of nickels is " + nickels);
else if ( nickels == 0 )
System.out.println(" ");
int remainder3 = remainder2 - nickels*5;
if (remainder3 != 0)
System.out.println("The amount of cents is " + remainder3);
else if ( remainder3 == 0 )
System.out.println(" ");
else
System.out.println ("you suck!");
}
}
}
|
Thanks again. |
|
|
|
|
|
HeavenAgain
|
Posted: 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 code: | int x = 7;
int y = 3;
int anInt = x/y;
float anFloat = x/y;
float castedFloat = (float)x/y;
(int) error = x/y; // this is somewhat what you are doing
| and print out the values of each
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 |
|
|
|
|
|
Nick
|
Posted: Thu Apr 03, 2008 6:13 pm Post subject: RE:Simple Program Errors - Variables etc. |
|
|
code: | else if ( quarters == 0 )
System.out.println(" ");
{
remainer1 = krazyNumber - quarters*25;
dimes = remainder1/10;
} |
see a problem? |
|
|
|
|
|
Reira
|
Posted: 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?
code: |
int quarters = (int) krazyNumber/25;
|
So this should be the right syntax for casting, am I correct? |
|
|
|
|
|
HeavenAgain
|
Posted: 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 code: | int quarters = (int) (krazyNumber/25); |
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 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Reira
|
Posted: Thu Apr 03, 2008 7:46 pm Post subject: RE:Simple Program Errors - Variables etc. |
|
|
Perfect! It works. Thanks a lot! |
|
|
|
|
|
HeavenAgain
|
Posted: Thu Apr 03, 2008 7:53 pm Post subject: RE:Simple Program Errors - Variables etc. |
|
|
no problem , now would you like to improve your code better?
to start off, you should always have the first letter of your class name in capital, so in your case code: | public class Assssss | prehaps something more meaningable would be nicer too
and another thing code: | if ( krazyNumber > 100 )
System.out.println("you suck");
else if ( krazyNumber < 99); |
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 |
|
|
|
|
|
Barbarrosa
|
Posted: 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] |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: 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).
*This is because of personal experience. Crashing the whole program seriously sucks when testing or debugging. I've seen people struggle because of it. 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. |
|
|
|
|
|
HeavenAgain
|
Posted: 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 thanks. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: 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 thanks. 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.
code: | a 100
jmp 142
a 142
mov si,102
mov ah,47
int 21
jnc 150
mov ax,4c01
int 21
xor bl,bl
cmp bl,[si]
jz 159
inc si
jmp 152
mov bl,24
mov [si],bl
mov dx,102
mov ah,9
int 21
mov ax,4c00
int 21
g =100
q
|
Using input redirection: code: | C:\Temp>debug < cwd.txt
-a 100
1447:0100 jmp 142
1447:0102
-a 142
1447:0142 mov si,102
1447:0145 mov ah,47
1447:0147 int 21
1447:0149 jnc 150
1447:014B mov ax,4c01
1447:014E int 21
1447:0150 xor bl,bl
1447:0152 cmp bl,[si]
1447:0154 jz 159
1447:0156 inc si
1447:0157 jmp 152
1447:0159 mov bl,24
1447:015B mov [si],bl
1447:015D mov dx,102
1447:0160 mov ah,9
1447:0162 int 21
1447:0164 mov ax,4c00
1447:0167 int 21
1447:0169
-g =100
TEMP
C:\Temp> |
|
|
|
|
|
|
|
|