Connecting Tech Pros Worldwide Help | Site Map

Please help a beginner java programmer!

Newbie
 
Join Date: Oct 2007
Posts: 22
#1: Oct 20 '07
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:
Expand|Select|Wrap|Line Numbers
  1. class calculator {
  2.     public static void main(String[] arguments) {
  3.     float numbera = 100;
  4.     float numberb = 200;
  5.  
  6.         if (arguments.length > 0) {
  7.             numbera = Float.parseFloat(arguments[0]); 
  8.         sign = Char.parseChar (arguments[1]);
  9.         numberb = Float.parseFloat(arguments[2]);
  10.     }
  11.  
  12.            if (sign == +)
  13.  
  14.  
  15.     System.out.println(numbera
  16.     + "added to "
  17.     + numberb            
  18.     + " is "
  19.         + (numbera + numberb));
  20.  
  21.     if (sign == -)
  22.  
  23.  
  24.     System.out.println(numbera
  25.     + "subtract "
  26.     + numberb            
  27.     + " is "
  28.         + (numbera - numberb));
  29.  
  30.     if (sign == *)
  31.  
  32.  
  33.     System.out.println(numbera
  34.     + "multiplied by "
  35.     + numberb            
  36.     + " is "
  37.         + (numbera * numberb));
  38.  
  39.     if (sign == /)
  40.  
  41.  
  42.     System.out.println(numbera
  43.     + "divided by " 
  44.     + numberb            
  45.     + " is "
  46.         + (numbera / numberb));
  47.  
  48.     }
  49.  
  50.  
I am getting 6 error codes, all saying "illegal start of expression". They are at:

The last bracket of
Expand|Select|Wrap|Line Numbers
  1.  if (sign == +) 
The last bracket of
Expand|Select|Wrap|Line Numbers
  1.  if (sign == -) 
The multiplication sign of
Expand|Select|Wrap|Line Numbers
  1.  if (sign == *) 
The last bracket of
Expand|Select|Wrap|Line Numbers
  1.  if (sign == *) 
The division sign of
Expand|Select|Wrap|Line Numbers
  1.  if (sign == /) 
The last bracket of
Expand|Select|Wrap|Line Numbers
  1.  if (sign == /) 
I have been trying for a long time to find a solution but to no avail. Please could someone help me?.
Thank you
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Oct 20 '07

re: Please help a beginner java programmer!


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.
Newbie
 
Join Date: Oct 2007
Posts: 22
#3: Oct 20 '07

re: Please help a beginner java programmer!


I realised that I should have used if else statements. Here is the revised code
Expand|Select|Wrap|Line Numbers
  1. class calculator {
  2.     public static void main(String[] arguments) {
  3.     float numbera = 100;
  4.     float numberb = 200;
  5.  
  6.         if (arguments.length > 0) {
  7.             numbera = Float.parseFloat(arguments[0]); 
  8.         sign = Char.parseChar (arguments[1]);
  9.         numberb = Float.parseFloat(arguments[2]);
  10.     }
  11.  
  12.  
  13.  
  14.            if (sign == +) {
  15.  
  16.  
  17.     System.out.println(numbera
  18.     + "added to "
  19.     + numberb            
  20.     + " is "
  21.         + (numbera + numberb));}
  22.  
  23.     else if (sign == -){
  24.  
  25.  
  26.     System.out.println(numbera
  27.     + "subtract "
  28.     + numberb            
  29.     + " is "
  30.         + (numbera - numberb));}
  31.  
  32.     else if (sign == *){
  33.  
  34.  
  35.     System.out.println(numbera
  36.     + "multiplied by "
  37.     + numberb            
  38.     + " is "
  39.         + (numbera * numberb));}
  40.  
  41.     else if (sign == /){
  42.  
  43.  
  44.     System.out.println(numbera
  45.     + "divided by " 
  46.     + numberb            
  47.     + " is "
  48.         + (numbera / numberb));}
  49.  
  50.     }
  51.  
  52.  
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
Newbie
 
Join Date: Oct 2007
Posts: 22
#4: Oct 20 '07

re: Please help a beginner java programmer!


Sorry Ganon I posted the second thing before I saw your reply. here is the code i am now using
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. class calculator {
  4.     public static void main(String[] arguments) {
  5.     float numbera = 100;
  6.     float numberb = 200;
  7.  
  8.         if (arguments.length > 0) {
  9.             numbera = Float.parseFloat(arguments[0]); 
  10.         sign = Char.parseChar (arguments[1]);
  11.         numberb = Float.parseFloat(arguments[2]);
  12.     }
  13.  
  14.  
  15.  
  16.            if (sign == '+') 
  17.  
  18.  
  19.     System.out.println(numbera
  20.     + "added to "
  21.     + numberb            
  22.     + " is "
  23.         + (numbera + numberb));
  24.  
  25.     else if (sign == '-')
  26.  
  27.  
  28.     System.out.println(numbera
  29.     + "subtract "
  30.     + numberb            
  31.     + " is "
  32.         + (numbera - numberb));
  33.  
  34.     else if (sign == '*')
  35.  
  36.  
  37.     System.out.println(numbera
  38.     + "multiplied by "
  39.     + numberb            
  40.     + " is "
  41.         + (numbera * numberb));
  42.  
  43.     else if (sign == '/')
  44.  
  45.  
  46.     System.out.println(numbera
  47.     + "divided by " 
  48.     + numberb            
  49.     + " is "
  50.         + (numbera / numberb));
  51.  
  52.     }
  53.  
  54.  
  55.  
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
Expand|Select|Wrap|Line Numbers
  1.  sign = Char.parseChar (arguments[1]);
  2.  
statement
2. The first Char in the
Expand|Select|Wrap|Line Numbers
  1.  sign = Char.parseChar (arguments[1]);
  2.  
statement
3. And all the signs in the if and else if statements (example
Expand|Select|Wrap|Line Numbers
  1.  else if (sign == '-') 
Newbie
 
Join Date: Oct 2007
Posts: 22
#5: Oct 20 '07

re: Please help a beginner java programmer!


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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class calculator {
  3.     public static void main(String[] arguments) {
  4.     float numbera = 100;
  5.     float numberb = 200;
  6.     char sign = '+';
  7.  
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
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#6: Oct 21 '07

re: Please help a beginner java programmer!


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.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by Ganon11

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:

Expand|Select|Wrap|Line Numbers
  1. String sign= arguments[1];
  2.  
  3. if (sign.equals("+")) { ... }
  4. if (sign.equals("-")) { ... }
  5. if (sign.equals("*")) { ... }
  6. if (sign.equals("/")) { ... }
  7.  
kind regards,

Jos
Newbie
 
Join Date: Oct 2007
Posts: 22
#8: Oct 21 '07

re: Please help a beginner java programmer!


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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#9: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by Hazza

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:

Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i < argument.length; i++)
  2.    System.out.println("argument["+i"]="+argument[i]);
  3.  
kind regards,

Jos
Newbie
 
Join Date: Oct 2007
Posts: 22
#10: Oct 21 '07

re: Please help a beginner java programmer!


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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#11: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by Hazza

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:

Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] argument) {
  2.    for (int i= 0; i < argument.length; i++)
  3.       System.out.println("argument["+i"]="+argument[i]);
  4.  
  5.    // your code goes here
  6.    // ...
  7.  
kind regards,

Jos
Newbie
 
Join Date: Oct 2007
Posts: 22
#12: Oct 21 '07

re: Please help a beginner java programmer!


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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#13: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by Hazza

I did that and got the same error messages.

Here is my complete code (with yours included)

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[] arguments) {
  2.  
  3.  
  4.   for (int i= 0; i < argument.length; i++)
  5.       System.out.println("argument["+i"]="+argument[i]);
  6.  
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:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[] arguments) {
  2.  
  3.  
  4.   for (int i= 0; i < arguments.length; i++)
  5.       System.out.println("arguments["+i"]="+arguments[i]);
  6.  
kind regards,

Jos
Newbie
 
Join Date: Oct 2007
Posts: 22
#14: Oct 21 '07

re: Please help a beginner java programmer!


i updated my code and i am still getting the same error messages
thanks
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#15: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by Hazza

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:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("arguments["+i+"]="+arguments[i]);
  2.  
Note that extra '+' sign I forgot in the previous (incorrect) versions.

kind regards,

Jos
Newbie
 
Join Date: Oct 2007
Posts: 22
#16: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by JosAH

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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#17: Oct 21 '07

re: Please help a beginner java programmer!


See my previous reply; I goofed in my code snippet; above you can see the
corrected version; sorry for the inconvenience.

kind regards,

Jos
Newbie
 
Join Date: Oct 2007
Posts: 22
#18: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by JosAH

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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#19: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by Hazza

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
Newbie
 
Join Date: Oct 2007
Posts: 22
#20: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by JosAH

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?
Newbie
 
Join Date: Oct 2007
Posts: 22
#21: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by Hazza

Oh- it runs through my files?
Ok - so i cannot use that operator in this way?

Sorry I cannot respond for about 20 mins
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#22: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by Hazza

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
Newbie
 
Join Date: Oct 2007
Posts: 22
#23: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by JosAH

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!!!! :-)
Newbie
 
Join Date: Oct 2007
Posts: 22
#24: Oct 21 '07

re: Please help a beginner java programmer!


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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#25: Oct 21 '07

re: Please help a beginner java programmer!


[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

Expand|Select|Wrap|Line Numbers
  1. class calculator {
  2.     public static void main(String[] arguments) {
  3.  
  4.  
  5.         float numbera = Float.parseFloat(arguments[0]); 
  6.             char sign = arguments[1].charAt(0);
  7.             float numberb = Float.parseFloat(arguments[2]);
  8.  
  9.     switch (numbera) {
  10.  
  11.     case float:{
  12.  
  13.     switch (numberb) {
  14.  
  15.     case float:{
  16.  
  17.     switch (sign) {
  18.  
  19. [ ... ]
  20.  
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:

Expand|Select|Wrap|Line Numbers
  1. private float getFloat(String string) {
  2.  
  3.    try {
  4.       return ... ; // parse the string and return the float result
  5.    }
  6.    catch (NumberFormatException nfe) {
  7.       // tell the user /she goofed and:
  8.      System.exit(1);
  9.    }
  10. }
  11.  
Your turn now ...

kind regards,

Jos
Newbie
 
Join Date: Oct 2007
Posts: 22
#26: Oct 21 '07

re: Please help a beginner java programmer!


Thanks for your help
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#27: Oct 21 '07

re: Please help a beginner java programmer!


Quote:

Originally Posted by Hazza

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
Reply