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

Thread: Please HELP! JAVA CODE problem

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please HELP! JAVA CODE problem

    Hello everybody,

    I'm a beginner in this field & I am asking for help .. I wrote my code completely & it says [Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Calculator.main(Calculator.java:12)] .

    This is my code :
    ----------------------------------------


    public class Calculator {
    	public static void main(String [] args){
    		if (args.length != 1) {
    			System.out.println("Usage: java Calculator \"operand1 operator operand2\"");
    			System.exit(1);
    		}
     
    		int result = 0;
     
    		String[] tokens = args[0].split("  ");
     
    		switch (tokens[1].charAt(0) ){
    			case '+' : result = Integer.parseInt(tokens[0]) +
    					    Integer.parseInt(tokens[2]);
    				break;
     
    			case '-' : result = Integer.parseInt(tokens[0]) -
    					    Integer.parseInt(tokens[2]);
    				break;
     
    			case '*' : result = Integer.parseInt(tokens[0]) *
    					    Integer.parseInt(tokens[2]);
    				break;
     
    			case '/' : result = Integer.parseInt(tokens[0]) /
    					    Integer.parseInt(tokens[2]);
    		     }
     
    		     System.out.println(tokens[0] + ' ' + tokens[1] + ' ' + tokens[2] + " = " + result);
    		}	
    	}
    ---------------------------------
    DONE ! thank you for help in advance
    Last edited by iMagOoOd; March 23rd, 2014 at 09:33 AM.


  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: Please HELP! JAVA CODE problem

    exception in thread main java.lang.arrayindexoutofboundsexception 1
    At line ??? (shown in the error message) the code used an index of 1 in an array with less than 2 elements. Look at that line of the code and see why the code tried to use an index past the end of the array.

    Please edit your post and wrap your code with code tags:
    [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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Please HELP! JAVA CODE problem

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

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

    Default Re: Please HELP! JAVA CODE problem

    Thanks Norm the error message says it's in line 12 according to CMD (Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Calculator.main(Calculator.java:12))
    Quote Originally Posted by Norm View Post
    At line ??? (shown in the error message) the code used an index of 1 in an array with less than 2 elements. Look at that line of the code and see why the code tried to use an index past the end of the array.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

  5. #5
    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: Please HELP! JAVA CODE problem

    Did you look at line 12 to see why the code was using an index past the end of the array?
    An index of 1 is looking at the second element in the array. The error message says: the array has fewer than 2 elements.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please HELP! JAVA CODE problem

    I can't see what is the problem?
    Can you be more specific ?
    & how to solve it?

  7. #7
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Please HELP! JAVA CODE problem

    Take a look at ArrayIndexOutOfBoundsException (Java Platform SE 7 ) to better understand what the exception means.

    Next, can you find line 12 in your Java source file? Which line is it? Based on the exception the line must involve an array, and the index that is provided to access the value in the array is illegal. If you're still unsure why the index is illegal, check the length of the array by printing out the length, and/or print out the entire contents of the array.

    Remember, programming is not just about writing code. It's also about debugging code. Right now you're learning how to do the latter.

Similar Threads

  1. Java code problem?
    By Scorks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2013, 06:13 PM
  2. [SOLVED] New to JAVA, problem with first code, need to know how to fix.
    By hrenfrow in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2013, 11:17 AM
  3. New to Java having problem debugging my code.
    By OHJava in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 9th, 2013, 01:05 PM
  4. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  5. [SOLVED] Java Newbie Code Problem
    By lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2010, 03:05 PM