Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:56:41+00:00 2026-06-04T04:56:41+00:00

Where do I edit this code to find the first letter of each word

  • 0

Where do I edit this code to find the first letter of each word from my input file, keeping the frequency and percentage, rather than every single character? e.g. where could I implement a charAt(0) or what do I need to change/add?

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class FirstWordLetters
{
    public static void main(String[] args) throws FileNotFoundException
    {
        char[] capital = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N',
                'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

        char[] small = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };


        //Input Scanner into the system        
        Scanner scan; 
        //2. Locate file using the scanner class - search to computer data location
        try {
            scan = new Scanner(new File("F:/programming principles/Programming Principles - PART B/enciphered.txt"));
        } catch (Exception e) { //throw exception e (meaning prevent any runtime errors)
            System.out.println("File not found");
            return;
        }
        //3. Set up int's (to count, and for the complete count.)
        //the aplhabet has 26 characters so the new int will be 26. The mem. space of the array.
        int[] count = new int[26];
        int completeTotal = 0;
        //4. Start scanning the system
        //Scan every line and notify user that each line has been read properly. 
        //each time line has been read store value in array and increment by one
        while(scan.hasNextLine()) {
            String line = scan.nextLine();
            System.out.println("Line read: " + line);
            char[] digit = line.toCharArray();
            for(int i = 0; i < digit.length; i++) {
                for(int j = 0; j < 26; j++) {

                    if(digit[i] == capital[j] || digit[i] == small[j]) {
                        count[j]++;
                        completeTotal = completeTotal + 1;
                        break;
                    }
                }
            }
        }
        //5. Display results
        //Print the overall data - letter, frequency and percentage.
        System.out.println("");
        System.out.println("First Word Count"); //notify user of what has been counted? (full count)
        for (int i = 0; i < 26; i++) //increment each letter frequency by one each time
        {
            System.out.print(" " + small[i]);
            System.out.print(" " + count[i]);
            //calculate and display percentage for the full count
            if (count[i] > 0)
                System.out.println(" " + (((float) count[i]/completeTotal)*100) + "%");
            else
                System.out.println(" 0%");

        }
    }    
} //end of source code.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:56:43+00:00Added an answer on June 4, 2026 at 4:56 am

    This code will do it. inputString is your entire file stored as a single string. Also note that if any word begins with a non-alphabetical character it will crash. Also, it assumes that every single word is separated with a space or a new line.

        int[] charCounts = new int[26];
    
        //Separate the string into individual words
        //The string " /n" tells it to look for spaces and newline characters "/n"
        StringTokenizer st = new StringTokenizer(inputString, " /n");
    
        //Loop until all the words are processed
        while (st.hasMoreTokens()) {
    
            //Select the next word
            String word = st.nextToken();
    
            //Convert the string to upper case so that lower case and upper case characters are represented the same
            word = word.toUpperCase();
    
            //Get the first character from the word
            char firstChar = word.charAt(0);
    
            //Convert the character to an integer representing it as an ASCII code
            int charCode = (int) firstChar;
    
            //Increment the count for that character by 1 
            //(We subtract 65 from the ASCII code because the array starts at 0 but 'A' is at 65)
            charCounts[charCode - 65]++;
        }
    
        //Obviously replace this section with whatever you like. It's just to show you how to get the values out again.
        for (int i = 0; i < charCounts.length; i++){
            System.out.println((char)(i + 65) + ": " + charCounts[i]);
        }
    

    For the string “asduhasa ioajsdu ijqwjfoscn kopeurfc eqiwdfjs/nijoasdij iohwefscd aosd8ch”, this outputs:

    A: 2
    B: 0
    C: 0
    D: 0
    E: 1
    F: 0
    G: 0
    H: 0
    I: 4
    J: 0
    K: 1
    L: 0
    M: 0
    N: 0
    O: 0
    P: 0
    Q: 0
    R: 0
    S: 0
    T: 0
    U: 0
    V: 0
    W: 0
    X: 0
    Y: 0
    Z: 0
    

    If there’s anything you don’t understand, I’ll do my best to explain it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how does one use code to do this: produce 15 random numbers [EDIT: from
ive a similar problem as this guy: Generate SQL CE database from EF Code-First
EDIT: This code now works correctly, I only left it in case someone finds
I am getting a StackOverflow Error in this code: EDIT [XmlAttribute(ID)] public string ID
EDIT: I realized that this code compiles and works: #include <iostream> template<class Something> class
edit: I'm not looking for you to debug this code. If you are familiar
Edit: I put this snippet of code in jsbin: http://jsbin.com/eneru I am trying to
I have this html code that i want to edit with jQuery. Here is
Code as reference: http://jsbin.com/aboca3/2/edit In this example above (thank you SLaks) I am truncating
Answer solved in edit below I had this piece of code Dictionary<Merchant, int> remaingCards

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.