LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2005, 04:36 PM   #1
Gerardoj
Member
 
Registered: May 2003
Location: Somewhere over the Rainbow
Distribution: Slackware 9.x, Knoppix, Damn Small Linux, RedHat.
Posts: 465

Rep: Reputation: 30
Java ArrayIndexOutOfBoundsException:


HI Everyone:

I have a problem with my little code in Java, Im getting
What I'm missing here?

Thanks for your help.


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at keita.main(keita.java:5)


Code:
public class keita {
  public static void main(String args[]) {
    
            double num1  = Double.parseDouble(args[0]);
            double num2  = Double.parseDouble(args[1]);
            
            double average;
            average = (num1 + 1) /  2;
            
            System.out.println("Average" + average );
            
        }
     
}
 
Old 09-11-2005, 04:51 PM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
There's nothing wrong with the above code. The error you are getting is because when running the program (java keita), you are not parsing enough arguments, so the execution fails. If you try:

java keita 10 5

you will find that it works. To work around this, you could either check in an if-statement if the amount of arguments is equal to 2. If it's not, then print something like "insufficient arguments", or you could put your code inside a try-catch statement.

Regards!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 02:13 AM
Java Programming: Java Runtime Environment not found when trying to compile murbz Linux - Software 2 03-26-2009 03:04 AM
Web start java not working (java works fine) powadha Debian 5 06-05-2004 12:57 PM
Updated Java... now java apps dont work zidane2010 Linux - Software 6 05-04-2004 08:02 PM
2 Questions: java calling system commands? PERL vs Java? randomx Programming 28 11-28-2003 08:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration