Note that there are some explanatory texts on larger screens.

plurals
  1. POSubstring - StringIndexOutOfBounds exception java
    primarykey
    data
    text
    <p>I've been working on a program to collect data from a file and do some stuff to it (as evident in the code and pseudocode) however I'm having difficulty getting each element of the string into the right array. A line from a file looks like this: </p> <pre><code>1980 Aug 945 100 Allen </code></pre> <p>I'm wanting to use the substring method because that'd be the easiest in my opinion. Is there a better way of doing it? Here's my code thus far. Where does the problem lie exactly, and how should I fix it? Thanks! :) </p> <pre><code>import java.util.Scanner; import java.io.File; import java.io.IOException; import java.lang.String; public class Hurricanes2 { public static void main(String [] args) throws IOException { int counter = 0; String [] token = new String[1000]; String [] tokenElements = new String[128]; String [] hurricaneYear = new String[64]; String [] hurricaneName = new String[64]; String [] hurricaneMonth = new String[64]; int [] hurricaneCategory = new int[64]; double [] hurricanePressure = new double[64]; double tempKnots; double knotsToMph; double [] hurricaneWindSpeed = new double[64]; double categoryAverage; double pressureAverage; double speedAverage; String headerData = " Hurricanes 1980 - 2006\n\n Year Hurricane Category Pressure(MB) Wind Speed (MPH)\n========================================================================"; Scanner in = new Scanner(System.in); Scanner inFile = new Scanner(new File("hurcData2.txt")); System.out.println(headerData); /**---Use for-each (line:token) * Parse for year - &gt; year array * parse for name - &gt; name array * parse for knots - &gt; tempKnots * knotsToMph = tempKnots * 1.15078 * hurricaneWindSpeed[counter] = knotsToMph * enter if-else to calculate category (hurricaneCategory [] = 1,2,3,4, or 5): * 74-95 cat1 * 96-110 cat2 * 111 - 129 cat3 * 130-156 cat4 * 157 or higher cat 5 * * */ while(inFile.hasNextLine()){ token[counter] = inFile.nextLine(); String tempToken = token[counter]; hurricaneYear[counter] = tempToken.substring(0, 3); hurricaneMonth[counter] = tempToken.substring(6, 8); hurricanePressure[counter] = Double.parseDouble(tempToken.substring(10, 12)); hurricaneWindSpeed[counter] = Double.parseDouble(tempToken.substring(14, 16)); hurricaneName[counter] = tempToken.substring(17); counter++; } System.out.print("Lines: " + counter); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload