Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to read through a file, counting letter frequencies using an array for letters

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


       My code below is supposed to read text from a file and store it into a string,
       then count the frequency of each letter and store it into an array counters. When
       compiling i receive the arrayindexoutofbounds error for line 42 of my code which is the
       line: scanner2.close() .
   
     
 

   Also confused on what the \\A string pattern does on the use.delimiter() as a partner helped
   with this and forgot to add comments. thank you
 
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You can find documentation on what the "\A" pattern means here: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html

Also, are you sure every single character in your input will be a letter of the alphabet? Your formula for the array index subtracts 'A' from each character and if that character is not one from 'A' to 'Z' then you'll get a number that is out of range of your valid array indices.

For example, if your input contains any punctuation, then those will produce an invalid index.

Go into JShell and type this:

jshell> '.' - 'A'

and see what it displays as the result
 
Marshal
Posts: 76487
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Was whoever told you to use \\A a friend? I personally probably wouldn't change the delimiter from its default value. You may wish to filter the chars to restrict the inputs to English letters.
 
Skool. Stay in. Smartness. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic