Hi im working on this program im getting a runtime error that says this ArrayIndexoutofbounds exception 0.


heres my code

import java.util.Scanner;
public class MillerCalculateTime
{
  public static void main(String[] args)
  {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter miles:    ");
      double miles = sc.nextDouble();
      System.out.print("Enter Miles per hour: ");
      double mph = sc.nextDouble();
      
      System.out.println("\n\nEstimated travel time");
      double initial = miles / mph;
      String splitter = Double.toString(initial);
      String[] splittersegment = splitter.split(".");
      System.out.println("Hours: " + splittersegment[0]);
      String min = "." + splittersegment[1];
      double minutes = Double.parseDouble(min);
      double minute = minutes * 60;
      int minu = (int) minute;
      System.out.println("Minutes: " +minu);
      
      
    }
}

the code that is being highlighted for the error is this

System.out.println("Hours: " + splittersegment[0]);

any insight would be greatly appreciated

The array index out of bounds error is caused by you calling an array slot number that is one larger than the array.

In this case you did not give the array any length.

This is how you split a string.

String[] [B]StringArrayName[/B] = [B]StringName[/B].split(".", 0);

The split method is defined as the following :
public String[] split(String regex)

regex means regular expression for more infprmation about it click here
when you useed the dot "." you used a special character so you need to escape it by "\" back slash but back slash is also a special character so you need to escape it also!!
So you need to change the line 15 to :

String[] splittersegment = splitter.split("\\.");

and it will work.
for more information about using split click here

commented: great info! +2

naief solved it, i didnt realize the special character xD thanks!

The array index out of bounds error is caused by you calling an array slot number that is one larger than the array.

In this case you did not give the array any length.

This is how you split a string.

String[] [B]StringArrayName[/B] = [B]StringName[/B].split(".", 0);

Sorry but you are wrong in this point.
The number which you added in the method means the times the method will split the string.
for more information read this

Also if you change the line with the line you wrote the program rise an exception.

you are welcome eikal

I did check my code against a college lvl java book and what I posted is what is says to do I would like to know where you found that information on the number telling java runtime how many times to split the string. If you don't mind. Thanks.

I did check my code against a college lvl java book and what I posted is what is says to do I would like to know where you found that information on the number telling java runtime how many times to split the string. If you don't mind. Thanks.

If you checked the link in my post you will find where I found that information.
actualy it is from oracle.com [Java™ Platform Standard Ed. 6]

Sorry to tell you but you are wrong about how the number relates to the regular expression. And I did read the material you linked to right after you posted it what you posted does not match. That is why I asked you where you found that material.

here is what I found:

If the number is positive it the splitter method is applied n-1 times. If it is zero it applies the split method as many times as possible and removes trailing empty strings. If the number is negative the split method will be applied as many times as possible.

and here is what it says:

"If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded."

great job fixing his problem, but read your manuals a little more carefully next time.

you were right about needing to escape the char thanks for the correction.

Well sorry for not meantioning every thing about this method ; I mean the case when letting n = 0 or n < 0 but what I said in general is that this number "means the times the method will split the string" ie the times "the pattern will be applied",as the source said, or the times "splitter method is applied", as you said, which is the same at all as think.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.