Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 11th, 2006, 06:27 PM
Registered User
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default why ArrayIndex OutOfBoundsException?

import java.lang.*;
class NewClass
{
public static void main(String args[])
{
String s="null";
for(int x=0;++x<=args.length;)
s+=args[x];
System.out.println(s);
}}

in commandline
java NewClass 32 44

i m getting array index out of bounds exception..can some one explain why?its a very simple program..i dont understand wherez the problem..

 
Old March 12th, 2006, 04:44 AM
Registered User
 
Join Date: Mar 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to bhanuj_2785 Send a message via Yahoo to bhanuj_2785
Default

you are accessing '\0' character of args
because string.length() returns length including '\0'
:D

bhanuj
 
Old March 13th, 2006, 02:19 PM
Registered User
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks bhanuj...i realize that was quite a silly mistake of mine.

 
Old March 20th, 2007, 11:48 PM
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

we assign the String s="null"
this can treate a string but not a null
we can try this
String s=null;
the for loop is starting with 0 and u can perform x<=
the for loop rewritten
for(int x=1;x<=args.lenth;x++)


K.Mallikarjunarao
 
Old March 21st, 2007, 11:36 PM
Authorized User
 
Join Date: Sep 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

just change for loop as follows:

for(int x=0;x<args.length; x++)

 
Old April 26th, 2007, 08:38 PM
Registered User
 
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'M WRITING A PROGRAM THAT IS SUPPOSED TO GET THE USER TO ENTER IN A BUNCH OF NUMBERS AND THEN GIVE THEM A CHOICE AS TO WHAT THEY WANT TO CONVERT THE DISTANCE(S)IN METRES BUT I CAN"T GET IT TO WORK YOU SEE THE PROBLEM IS THAT IT KEEPS REPEATING THE INPUT AND DOESN'T STOP CAN ANYONE HELP HERE"S THE CODE: (I'M USING BLUEJ)

import javax.swing.*;
import java.text.*;
public class distance
{
    public static void main(String[] args)
        {
            // declaration of variables
            String numStr, qStr;
            double num, squareNum, cubeNum, metres = 0;
            int q;

                    qStr = JOptionPane.showInputDialog("How many numbers do you want to convert?");
                    q = Integer.parseInt(qStr);

                    double[ ] number = new double[q];


                    for (int index = 0; index < number.length; index++)
                    {
                            numStr = JOptionPane.showInputDialog("Enter number "+index);
                            number[index] = Double.parseDouble(numStr);
                    }

                    for (int index = 0; index < number.length; index++)
                    {
                    String answerStr = JOptionPane.showInputDialog("Metres to convert "+number[index]+
                                    "\n1. Convert to Kilometres"
                                    +"\n2. Convert to Inches"
                                    +"\n3. Convert to Feet"
                                    +"\n4. Exit Program");
                                    int answer = Integer.parseInt(answerStr);

                
                     while (answer != 4) THE PROBLEM IS AROUND HERE SOMWHERE
                        {
                            if (answer == 1)
                            {
                             showKilo(number[index]);
                            }
                            if (answer == 2)
                            {
                             showInch(number[index]);
                            }
                             if (answer == 3)
                             {
                             showFeet(number[index]);
                            }
                        }
                    }
        }

    public static void showKilo(double k)
    {
        double kilometres = k * 0.001;
        JOptionPane.showMessageDialog(null, k+" metres = "+kilometres+" kilometres");
    }

    public static void showInch(double i)
    {
        double inches = i * 39.37;
        JOptionPane.showMessageDialog(null, i+" metres = "+i+" inches");
    }

    public static void showFeet(double f)
    {
        double feet = f * 3.281;
        JOptionPane.showMessageDialog(null, f+" metres = "+f+" feet");
    }

}
 
Old April 27th, 2007, 12:15 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

Try this code, i've modified it.

import javax.swing.*;
import java.text.*;
public class Test
{
    public static void main(String[] args)
        {
            // declaration of variables
            String numStr, qStr;
            double num, squareNum, cubeNum, metres = 0;
            int q;

                    qStr = JOptionPane.showInputDialog("How many numbers do you want to convert?");
                    q = Integer.parseInt(qStr);

                    double[ ] number = new double[q];


                    for (int index = 0; index < number.length; index++)
                    {
                            numStr = JOptionPane.showInputDialog("Enter number "+index);
                            number[index] = Double.parseDouble(numStr);
                    }

                    for (int index = 0; index < number.length; index++)
                    {
                    int answer = 0;
                        do{
                        String answerStr = JOptionPane.showInputDialog("Metres to convert "+number[index]+
                                        "\n1. Convert to Kilometres"
                                        +"\n2. Convert to Inches"
                                        +"\n3. Convert to Feet"
                                        +"\n4. Exit Program");
                           answer = Integer.parseInt(answerStr);

                           if (answer == 1){
                               showKilo(number[index]);
                           }else if (answer == 2){
                                  showInch(number[index]);
                           }else if (answer == 3){
                                  showFeet(number[index]);
                           }
                        }while(answer != 4);
                    }
        }

    public static void showKilo(double k)
    {
        double kilometres = k * 0.001;
        JOptionPane.showMessageDialog(null, k+" metres = "+kilometres+" kilometres");
    }

    public static void showInch(double i)
    {
        double inches = i * 39.37;
        JOptionPane.showMessageDialog(null, i+" metres = "+i+" inches");
    }

    public static void showFeet(double f)
    {
        double feet = f * 3.281;
        JOptionPane.showMessageDialog(null, f+" metres = "+f+" feet");
    }

}


Regards,
Rakesh









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.