Hello,
I am teaching myself java and am completely stuck. I am using the java development kit and vista command prompt to compile and interpret programmes. I am getting error codes which don't make sense because they refer to a code that seems all right to me. Here is the code: -
class calculator {
-
public static void main(String[] arguments) {
-
float numbera = 100;
-
float numberb = 200;
-
-
if (arguments.length > 0) {
-
numbera = Float.parseFloat(arguments[0]);
-
sign = Char.parseChar (arguments[1]);
-
numberb = Float.parseFloat(arguments[2]);
-
}
-
-
if (sign == +)
-
-
-
System.out.println(numbera
-
+ "added to "
-
+ numberb
-
+ " is "
-
+ (numbera + numberb));
-
-
if (sign == -)
-
-
-
System.out.println(numbera
-
+ "subtract "
-
+ numberb
-
+ " is "
-
+ (numbera - numberb));
-
-
if (sign == *)
-
-
-
System.out.println(numbera
-
+ "multiplied by "
-
+ numberb
-
+ " is "
-
+ (numbera * numberb));
-
-
if (sign == /)
-
-
-
System.out.println(numbera
-
+ "divided by "
-
+ numberb
-
+ " is "
-
+ (numbera / numberb));
-
-
}
-
}
-
-
I am getting 6 error codes, all saying "illegal start of expression". They are at:
The last bracket of
The last bracket of
The multiplication sign of
The last bracket of
The division sign of
The last bracket of
I have been trying for a long time to find a solution but to no avail. Please could someone help me?.
Thank you
26 4333
The +, -, *, and / characters alone are arithmetic operators. If you want to compare sign to the characters, use '+', '-', '*', or '/', which are the characters, not the operators.
I realised that I should have used if else statements. Here is the revised code -
class calculator {
-
public static void main(String[] arguments) {
-
float numbera = 100;
-
float numberb = 200;
-
-
if (arguments.length > 0) {
-
numbera = Float.parseFloat(arguments[0]);
-
sign = Char.parseChar (arguments[1]);
-
numberb = Float.parseFloat(arguments[2]);
-
}
-
-
-
-
if (sign == +) {
-
-
-
System.out.println(numbera
-
+ "added to "
-
+ numberb
-
+ " is "
-
+ (numbera + numberb));}
-
-
else if (sign == -){
-
-
-
System.out.println(numbera
-
+ "subtract "
-
+ numberb
-
+ " is "
-
+ (numbera - numberb));}
-
-
else if (sign == *){
-
-
-
System.out.println(numbera
-
+ "multiplied by "
-
+ numberb
-
+ " is "
-
+ (numbera * numberb));}
-
-
else if (sign == /){
-
-
-
System.out.println(numbera
-
+ "divided by "
-
+ numberb
-
+ " is "
-
+ (numbera / numberb));}
-
-
}
-
}
-
-
I am still getting six error messages for "illegal start of expression at for the last bracket ( this type of bracket )) of the if ( concerning addition) statement,the last bracket ( this type of bracket )) of the first else if ( concerning subtraction) statement,the last bracket ( this type of bracket )) and multiplication sign (*) of the second else if ( concerning multiplication) statement, and the last bracket ( this type of bracket )) and division sign of the last else if ( concerning division) statement,
When I say last bracket (of this type)) I meant on the line with the statement,
if (sign == +) {,
not after the rest of it. Thank you in advance to anyone who helps
Sorry Ganon I posted the second thing before I saw your reply. here is the code i am now using -
-
-
class calculator {
-
public static void main(String[] arguments) {
-
float numbera = 100;
-
float numberb = 200;
-
-
if (arguments.length > 0) {
-
numbera = Float.parseFloat(arguments[0]);
-
sign = Char.parseChar (arguments[1]);
-
numberb = Float.parseFloat(arguments[2]);
-
}
-
-
-
-
if (sign == '+')
-
-
-
System.out.println(numbera
-
+ "added to "
-
+ numberb
-
+ " is "
-
+ (numbera + numberb));
-
-
else if (sign == '-')
-
-
-
System.out.println(numbera
-
+ "subtract "
-
+ numberb
-
+ " is "
-
+ (numbera - numberb));
-
-
else if (sign == '*')
-
-
-
System.out.println(numbera
-
+ "multiplied by "
-
+ numberb
-
+ " is "
-
+ (numbera * numberb));
-
-
else if (sign == '/')
-
-
-
System.out.println(numbera
-
+ "divided by "
-
+ numberb
-
+ " is "
-
+ (numbera / numberb));
-
-
}
-
}
-
-
-
now the errors i am getting are 6 cannot find symbol errors.
(
cannot find symbol
symbol : variable (depends)
location: class calculator
They look like this.
1. Its the sign in the - sign = Char.parseChar (arguments[1]);
-
statement
2. The first Char in the - sign = Char.parseChar (arguments[1]);
-
statement
3. And all the signs in the if and else if statements (example
I got rid of all but one of the errors by declaring the "sign" variable at the beginning as a char variable so the first few lines look like this: -
-
class calculator {
-
public static void main(String[] arguments) {
-
float numbera = 100;
-
float numberb = 200;
-
char sign = '+';
-
I also changed Char to Character.parseChar
The only error that remains is;
Cannot find symbol
Symbol = method parseChar(java.lang.String)
location = class java.lang.String)
sign = Character.parseChar (arguments[1]);
with the little hat under the full stop in the last line (vista comman prompt)
Thanks in advance
If you know that arguments[1] has the sign, why not use the .charAt() function to grab the first character of that String? This function returns a character, so there's no need to use parseChar.
If you know that arguments[1] has the sign, why not use the .charAt() function to grab the first character of that String? This function returns a character, so there's no need to use parseChar.
... or alternatively make variable sign a String and do this: -
String sign= arguments[1];
-
-
if (sign.equals("+")) { ... }
-
if (sign.equals("-")) { ... }
-
if (sign.equals("*")) { ... }
-
if (sign.equals("/")) { ... }
-
kind regards,
Jos
Thanks Ganon - now everything works except multiplication.
I am mystified to why this is, because the code is exactly the same to the other mathematical functions.
Here is my full code [code]
class calculator {
public static void main(String[] arguments) {
float numbera = 100;
float numberb = 200;
char sign = '*';
numbera = Float.parseFloat(arguments[0]);
sign = arguments[1].charAt(0);
numberb = Float.parseFloat(arguments[2]);
if (sign == '+') {
System.out.println(numbera
+ " added to "
+ numberb
+ " is "
+ (numbera + numberb));}
else if (sign == '-'){
System.out.println(numbera
+ " subtract "
+ numberb
+ " is "
+ (numbera - numberb));}
else if (sign == '*'){
System.out.println(numbera
+ " multiplied by "
+ numberb
+ " is "
+ (numbera * numberb));}
else if (sign == '/'){
System.out.println(numbera
+ " divided by "
+ numberb
+ " is "
+ (numbera / numberb));}
}
}
[code]
I can type in java calculator 6 + 8 or 8 / 5 or 78 - 5 but not anything with a multiplication sign in it like 8 * 7.
The error message looks like this.
Exception in thread "main" java.lang.NumberFormatExceptin: For input string "addition.java"
at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
at java.lang.Float.parseFloat(Unknown source)
at calculator.main(test.java:10)
It says "addtion.java" in the error message, and I do have a file in the folder of this progrm, but there is no reference to either to either program. In case it should help, here is the code for it, which is a prototype for calculator:
[code]
class addition {
public static void main(String[] arguments) {
float numbera = 100;
float numberb = 200;
if (arguments.length > 0) {
numbera = Float.parseFloat(arguments[0]);
numberb = Float.parseFloat(arguments[1]);
}
System.out.println(numbera
+ " added to "
+ numberb
+ " is "
+ (numbera + numberb) );
}
}
[code]
Thanks
Thanks Ganon - now everything works except multiplication.
I am mystified to why this is, because the code is exactly the same to the other mathematical functions.
<code snipped>
I can type in java calculator 6 + 8 or 8 / 5 or 78 - 5 but not anything with a multiplication sign in it like 8 * 7.
The error message looks like this.
Exception in thread "main" java.lang.NumberFormatExceptin: For input string "addition.java"
at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
Add the following statement at the beginning of your main method and you'll
see why a single '*' on the command line won't work as you expected: -
for (int i= 0; i < argument.length; i++)
-
System.out.println("argument["+i"]="+argument[i]);
-
kind regards,
Jos
Sorry, I don't quite understand - where do i put that statement in my code and what does it do?
If i put your code directly after the public static void main(String[] arguments) {
I get 3 error messages saying that there is a ; expected (with the hat under the last ) of your code), that it is not a statement with the hat under the last put one square bracket, and that a normal bracket is expected, with the hat under the thrid ".
Thanks for your help
Sorry, I don't quite understand - where do i put that statement in my code and what does it do?
If i put your code directly after the public static void main(String[] arguments) {
I get 3 error messages saying that there is a ; expected (with the hat under the last ) of your code), that it is not a statement with the hat under the last put one square bracket, and that a normal bracket is expected, with the hat under the thrid ".
Thanks for your help
Do this: -
public static void main(String[] argument) {
-
for (int i= 0; i < argument.length; i++)
-
System.out.println("argument["+i"]="+argument[i]);
-
-
// your code goes here
-
// ...
-
kind regards,
Jos
I did that and got the same error messages.
Here is my complete code (with yours included)
[code]
class calculator {
public static void main(String[] arguments) {
for (int i= 0; i < argument.length; i++)
System.out.println("argument["+i"]="+argument[i]);
float numbera = 100;
float numberb = 200;
char sign = '+';
numbera = Float.parseFloat(arguments[0]);
sign = arguments[1].charAt(0);
numberb = Float.parseFloat(arguments[2]);
if (sign == '+') {
System.out.println(numbera
+ " added to "
+ numberb
+ " is "
+ (numbera + numberb));}
else if (sign == '-'){
System.out.println(numbera
+ " subtract "
+ numberb
+ " is "
+ (numbera - numberb));}
else if (sign == '*'){
System.out.println(numbera
+ " multiplied by "
+ numberb
+ " is "
+ (numbera * numberb));}
else if (sign == '/'){
System.out.println(numbera
+ " divided by "
+ numberb
+ " is "
+ (numbera / numberb));}
}
}
[code]
Thanks for your help
I did that and got the same error messages.
Here is my complete code (with yours included) -
public static void main(String[] arguments) {
-
-
-
for (int i= 0; i < argument.length; i++)
-
System.out.println("argument["+i"]="+argument[i]);
-
Thanks for your help
Duh, my mistake: you named your command line arguments 'arguments', not
'argument' as I did; make that code snippet like this: -
public static void main(String[] arguments) {
-
-
-
for (int i= 0; i < arguments.length; i++)
-
System.out.println("arguments["+i"]="+arguments[i]);
-
kind regards,
Jos
i updated my code and i am still getting the same error messages
thanks
i updated my code and i am still getting the same error messages
thanks
Found it; silly me: I should be just lazy on a Sunday morning; not posting wrong
code; change that println statement to this: -
System.out.println("arguments["+i+"]="+arguments[i]);
-
Note that extra '+' sign I forgot in the previous (incorrect) versions.
kind regards,
Jos
Can you show me the *exact* code snippet again? There's must've been some
bit rot happening when you copied/pasted my code. All I wanted you to see is
what the values of the individual argument elements are; no more no less.
kind regards,
Jos
Here is my code [code]
class calculator {
public static void main(String[] arguments) {
for (int i= 0; i < arguments.length; i++)
System.out.println("arguments["+i"]="+arguments[i]);
float numbera = 100;
float numberb = 200;
char sign = '+';
numbera = Float.parseFloat(arguments[0]);
sign = arguments[1].charAt(0);
numberb = Float.parseFloat(arguments[2]);
if (sign == '+') {
System.out.println(numbera
+ " added to "
+ numberb
+ " is "
+ (numbera + numberb));}
else if (sign == '-'){
System.out.println(numbera
+ " subtract "
+ numberb
+ " is "
+ (numbera - numberb));}
else if (sign == '*'){
System.out.println(numbera
+ " multiplied by "
+ numberb
+ " is "
+ (numbera * numberb));}
else if (sign == '/'){
System.out.println(numbera
+ " divided by "
+ numberb
+ " is "
+ (numbera / numberb));}
}
}
[code]
thanks for your help
See my previous reply; I goofed in my code snippet; above you can see the
corrected version; sorry for the inconvenience.
kind regards,
Jos
See my previous reply; I goofed in my code snippet; above you can see the
corrected version; sorry for the inconvenience.
kind regards,
Jos
Sorry I didn't see that, it now compiles correctly. All the functions except * work, and I get lists of he arguments used for those. When I use * I get 13 arguments,with [0] as the first number i typed in, [13] as the last number, and all the ones in between as random .java and .class files in the folder of calculator. I am new so i'm probably asking stupid questions but - Why is this? and How do i get it to work?
Thanks for your help
Sorry I didn't see that, it now compiles correctly. All the functions except * work, and I get lists of he arguments used for those. When I use * I get 13 arguments,with [0] as the first number i typed in, [13] as the last number, and all the ones in between as random .java and .class files in the folder of calculator. I am new so i'm probably asking stupid questions but - Why is this? and How do i get it to work?
Thanks for your help
Open up a command (cmd) window and type 'dir *'. What do you see? What did
the star '*' do?
kind regards,
Jos
Open up a command (cmd) window and type 'dir *'. What do you see? What did
the star '*' do?
kind regards,
Jos
Oh- it runs through my files?
Ok - so i cannot use that operator in this way?
Oh- it runs through my files?
Ok - so i cannot use that operator in this way?
Sorry I cannot respond for about 20 mins
Sorry I cannot respond for about 20 mins
No need to worry; this is not a chat box (although it looks like it sometimes ;-)
For now the easiest way out use a lower case 'x' character for the multiplication.
I think you don't want to do it the hard way now.
kind regards,
Jos
No need to worry; this is not a chat box (although it looks like it sometimes ;-)
For now the easiest way out use a lower case 'x' character for the multiplication.
I think you don't want to do it the hard way now.
kind regards,
Jos
Thanks a lot!!!! :-)
Sorry but I have another query about the same program.
I have now refined it, and want to use switch programs to give an error message if the user doesn't type in the exact things required.
Here is the code
[code]
class calculator {
public static void main(String[] arguments) {
float numbera = Float.parseFloat(arguments[0]);
char sign = arguments[1].charAt(0);
float numberb = Float.parseFloat(arguments[2]);
switch (numbera) {
case float:{
switch (numberb) {
case float:{
switch (sign) {
case '+':{
System.out.println(numbera
+ " added to "
+ numberb
+ " is "
+ (numbera + numberb));}
case '-':{
System.out.println(numbera
+ " subtract "
+ numberb
+ " is "
+ (numbera - numberb));}
case 'x':{
System.out.println(numbera
+ " multiplied by "
+ numberb
+ " is "
+ (numbera * numberb));}
case '/':{
System.out.println(numbera
+ " divided by "
+ numberb
+ " is "
+ (numbera / numberb));}
default:
System.out.println("Please enter two numbers with a mathematical operator between them, seperated by spaces");
}}
default:
System.out.println("Please enter two numbers with a mathematical operator between them, seperated by spaces");
}}
default:
System.out.println("Please enter two numbers with a mathematical operator between them, seperated by spaces");
}
}
}
[code]
There are two error messages for the two case float:{ (the command prompt says '.class' expected) but I am not sure how to make it so that it is in the case of numbera and numberb not being floats run the default statement.
Thanks to anyone who helps
[quote=Hazza]Sorry but I have another query about the same program.
I have now refined it, and want to use switch programs to give an error message
if the user doesn't type in the exact things required.
Here is the code -
class calculator {
-
public static void main(String[] arguments) {
-
-
-
float numbera = Float.parseFloat(arguments[0]);
-
char sign = arguments[1].charAt(0);
-
float numberb = Float.parseFloat(arguments[2]);
-
-
switch (numbera) {
-
-
case float:{
-
-
switch (numberb) {
-
-
case float:{
-
-
switch (sign) {
-
-
[ ... ]
-
I don't know what you're trying to do there.
The parseFloat() method throws an Exception when the String isn't a valid representation
of a floating point number. If it doesn't do that, you can be sure that your numbera
(and numberb) contain valid floating point values. If it does throw an exception you
can be sure that the user didn't supply a valid expression; you can 'catch' the
exception and tell the user so. You can even tell the user that s/he didn't enter
a valid operator.
Don't try to squeeze everything into that single main method. Add your own methods
to do part of the work. Especially getting a floating point number begs for its own
little method: -
private float getFloat(String string) {
-
-
try {
-
return ... ; // parse the string and return the float result
-
}
-
catch (NumberFormatException nfe) {
-
// tell the user /she goofed and:
-
System.exit(1);
-
}
-
}
-
Your turn now ...
kind regards,
Jos
Thanks for your help
You're welcome of course. Can you manage from here? Remember: better define
another (simple) method instead of trying to cram more and more complicated
control flow logic in one method (such as the main() method).
kind regards,
Jos
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
4 posts
views
Thread by jcnews |
last post: by
|
5 posts
views
Thread by Thomas G. Marshall |
last post: by
|
8 posts
views
Thread by Grrrbau |
last post: by
|
6 posts
views
Thread by Alex |
last post: by
|
27 posts
views
Thread by MHoffman |
last post: by
|
18 posts
views
Thread by mitchellpal |
last post: by
|
1 post
views
Thread by David Van D |
last post: by
|
15 posts
views
Thread by RAM |
last post: by
|
5 posts
views
Thread by macca |
last post: by
| | | | | | | | | | |