Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Problem with calcylator.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with calcylator.

    Hey

    Iīm trying to make a calcylator in java. First you write the first number, then the second number and then the unit(+,-/ or *). But i only get lots of errors the whole time and i canīt see my problem.
    Hereīs my code
     import java.util.Scanner;
     
    class KO{
        public static void main(String args[]){
    	     Scanner Alexander = new Scanner(System.in);
    		 double fnum, snum, unit, answer;
    		 System.out.println("Write fnum: ");
    		 fnum = Alexander.nextDouble();
    		 System.out.println("write snum: ");
    		 snum = Alexander.nextDouble();	
    		 System.out.println("+,-,/,*: ");
    		 unit = Alexander.nextDouble();	
    		  if (unit = +) { 
    		  answer = fnum + snum;
    		  }if (unit = -) {
             answer = fnum - snum;		  
    		  System.out.println(answer);
    		}  
    	}	 
    }

    And the errors is.

    calculator2.java:13; error: illegal start of expression
    if (unit = +) {

    calculator2.java:15: error illegal start of expression
    } if (unit = -) {

    Sincrerly
    JustACode


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with calcylator.

    i only get lots of errors
    Please copy the full text of the error messages and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calcylator.

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error messages and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    Fixed it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with calcylator.

    if (unit = +) {
    Two things wrong with the syntax here:
    = is the assignment operator, == is for testing equality
    + is the addition operator. If you wanted a String, enclose it in "s like this: "+"

    What is the purpose of that statement?
    unit is a numeric variable of type double
    Are you trying to compare its value to something?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calcylator.

    This is what iīm trying to do. When you start the program, the text "Enter first number" will be shown. Then you write some numbers and press enter. The text "Enter second number" will be shown. Write some numbers ad click enter the text "choose a unit" will be show. This is here my problem. I have problems to make that if you choose +, sum is fnum + snum = answer, if you choose * the sum is fnum * snum, if you choose - the sum is fnum - snum and if you choose / the sum is fnum / snum.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with calcylator.

    Did you change the code to read the arithmetic operators as Strings and not as numbers?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calcylator.

    Quote Originally Posted by Norm View Post
    Did you change the code to read the arithmetic operators as Strings and not as numbers?
    What exactly do you mean i should do?

  8. #8
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Problem with calcylator.

    You're reading in your +-*/ with nextDouble() but none of those are doubles.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with calcylator.

    Read the API doc for the Scanner class to find a method that reads a String.
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  2. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM