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 4 of 4

Thread: Simple computation program with strings...

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple computation program with strings...

    Hi. I'm trying to make a simple calculator type program out of strings..

    you can easily see what i'm trying to do by looking at the code:

    import java.util.Scanner;


    public class Main {



    public static void main(String[] args)
    {

    Scanner sin = new Scanner(System.in);
    String returnval = sin.nextLine();
    System.out.println(math(returnval));

    }




    static double math(String compute)
    {



    String[] words = compute.split(" ");


    double x = Integer.parseInt(words[1]);
    double y = Integer.parseInt(words[2]);


    if(words[0].equalsIgnoreCase("Add"));
    {
    return x+y;
    }
    if(words[0].equalsIgnoreCase("Subtract"));
    {
    return x-y;
    }
    if(words[0].equalsIgnoreCase("Multiply"));
    {
    return x*y;
    }
    if(words[0].equalsIgnoreCase("Divide"));
    {
    return x/y;
    }

    }
    }

    i want the console to ask the user to enter for example "Multiply 5 6" and then it gives 30. But i'm getting an error which says "Unreachable code" - once i make more than one if statement... i don't know why this is happening.. also if i typed garbage [space] 5 [space] 6 it will still multiply the numbers... i want it to multiply 2 numbers ONLY if i typed 'multiply\Multiply' at the beginning

    Please help me try and fix the code, thank you


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Simple computation program with strings...

    Hi alias22 (for the 22nd time?)
    /end joke

    Please see the announcements page for this forum, and use code tags when posting code. Instructions on the code tag are on the linked page.

    The compiler in this case has found code that is never possible to reach. In your code:

    if (words[0].equalsIgnoreCase("Add"))
    ;


    There is a ; immediately after your if statement.
    This says the if conditional has completed. It means the same thing as:
    if (words[0].equalsIgnoreCase("Add")) {
    }



    So because you have:
    if (words[0].equalsIgnoreCase("Add"))
    ;
    return x + y;

    You terminate the if, and return from the method, leaving all following if statements to never have a chance to be executed, no matter the results of your first if.
    Note that this extra ; appears immediately after more than one of your if statements.

    Good luck with corrections and let us know what you find out!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple computation program with strings...

    Oh...My...God I can't believe I made that mistake

    lol!

    i was looking at this for about 2 hours and i didn't spot that, God my eyesight deceives me

    import java.util.Scanner;
     
     
    public class Main {
     
     
     
    public static void main(String[] args)
    {
     
    	Scanner sin = new Scanner(System.in);
    	String returnval = sin.nextLine();
    	System.out.println(math(returnval));
     
    }
     
     
     
     
    	static double math(String compute)
    	{
     
     
    		String[] words = compute.split(" ");
     
     
    		double x = Integer.parseInt(words[1]);
    		double y = Integer.parseInt(words[2]);
     
     
    				if(words[0].equalsIgnoreCase("Add"))
    				{
    					return x+y;
    				}
    				if(words[0].equalsIgnoreCase("Subtract"))
    				{
    					return x-y;
    				}
    				if(words[0].equalsIgnoreCase("Multiply"))
    				{
    					return x*y;
    				}
    				if(words[0].equalsIgnoreCase("Divide"))
    				{
    					return x/y;
    				}
     
    				return 0;
    	}
    }

    the code is now looking like this and yes, it works

    Thanks for helping me!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple computation program with strings...

    If you would use the javac command's -Xlint option, the compiler would give you this error for the way you coded the if statement:
    warning: [empty] empty statement after if
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Building a triangle using strings and loops...program trouble.
    By orbin in forum Loops & Control Statements
    Replies: 2
    Last Post: June 13th, 2012, 03:29 PM
  2. High / low integer computation algorithm????
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 12:40 PM
  3. please help with simple program!!!
    By jokneez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 14th, 2011, 10:42 AM
  4. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  5. simple program
    By Memb in forum Paid Java Projects
    Replies: 0
    Last Post: March 17th, 2010, 01:47 PM