473,605 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help a beginner java programmer!

22 New Member
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
Oct 20 '07 #1
26 4598
Ganon11
3,652 Recognized Expert Specialist
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.
Oct 20 '07 #2
Hazza
22 New Member
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
Oct 20 '07 #3
Hazza
22 New Member
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 == '-') 
Oct 20 '07 #4
Hazza
22 New Member
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.parse Char
The only error that remains is;
Cannot find symbol
Symbol = method parseChar(java. lang.String)
location = class java.lang.Strin g)
sign = Character.parse Char (arguments[1]);
with the little hat under the full stop in the last line (vista comman prompt)
Thanks in advance
Oct 20 '07 #5
Ganon11
3,652 Recognized Expert Specialist
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.
Oct 21 '07 #6
JosAH
11,448 Recognized Expert MVP
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
Oct 21 '07 #7
Hazza
22 New Member
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.parseFloa t(arguments[0]);
sign = arguments[1].charAt(0);
numberb = Float.parseFloa t(arguments[2]);





if (sign == '+') {


System.out.prin tln(numbera
+ " added to "
+ numberb
+ " is "
+ (numbera + numberb));}

else if (sign == '-'){


System.out.prin tln(numbera
+ " subtract "
+ numberb
+ " is "
+ (numbera - numberb));}

else if (sign == '*'){


System.out.prin tln(numbera
+ " multiplied by "
+ numberb
+ " is "
+ (numbera * numberb));}


else if (sign == '/'){


System.out.prin tln(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.Numbe rFormatExceptin : For input string "addition.j ava"
at sun.misc.Floati ngDecimal.readJ avaFormatString (Unknown Source)
at java.lang.Float .parseFloat(Unk nown source)
at calculator.main (test.java:10)

It says "addtion.ja va" 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.leng th > 0) {
numbera = Float.parseFloa t(arguments[0]);
numberb = Float.parseFloa t(arguments[1]);

}
System.out.prin tln(numbera
+ " added to "
+ numberb
+ " is "
+ (numbera + numberb) );
}
}
[code]
Thanks
Oct 21 '07 #8
JosAH
11,448 Recognized Expert MVP
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.Numbe rFormatExceptin : For input string "addition.j ava"
at sun.misc.Floati ngDecimal.readJ avaFormatString (Unknown 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
Oct 21 '07 #9
Hazza
22 New Member
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
Oct 21 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3672
by: jcnews | last post by:
I am a beginner programmer just starting to learn Java. Can someone tell me what is wrong with this applet? The output is: Circumference is 47.1225 Area is 176.70938 Volume is 1767.0938 I don't think that is the correct value of the volume because it is exactly 10 times the area.
5
1708
by: Thomas G. Marshall | last post by:
This message is sent to these newsgroups because they are no longer valid: comp.lang.java comp.lang.java.api comp.lang.java.bugs comp.lang.java.misc comp.lang.java.setup comp.lang.java.tech These are long since retired newsgroups. You may have ended up in one by
8
2369
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
6
1561
by: Alex | last post by:
Hello I am intersting in developing and my background is VBA used in Excel and a brief intro to Java. I am interested in learning beyond VB and feel that C++ would be a very good language to get to know and give a lot of flexibility (if not tough to learn). I would like some advice on the merits of learning C++ versus C# or Visual Basic or Java for that matter.
27
4357
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text instead of a number, or give an error message? Also, how can I make the program verify there are two valid entries in txtBox1 and txtBox2 to then ENABLE the button operators (ie +, -, /, *).
18
2910
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner? Are there better languages than c for a beginner? For instance visual basic or i should just keep the confidence of improving?
1
9613
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
15
5081
by: RAM | last post by:
Hello, I graduated computer science faculty and decided to became a programmer. Please help me to make a decision: Java or Microsoft .NET? What is the future of Java? Thanks! /RAM/
5
2735
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
0
8430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8078
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8298
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6752
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5892
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5452
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2441
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1279
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.