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 8212485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:46:27+00:00 2026-06-07T10:46:27+00:00

So I’m fairly new to programming, and I’m working on a simple practice program

  • 0

So I’m fairly new to programming, and I’m working on a simple practice program that finds a letters order in the alphabet. This should have been pretty easy…but for some reason I get a StringIndexOutOfBoundsException when I add a while loop. So the program does do what it should the first time…but will not allow me to test again with out re-running the program. I tested the while loop with nothing but a simple print statement inside and it worked, so I’m confused as to why the while loop isn’t working with my alphabet program.

Any help would be greatly appreciated
thanks!

import java.io.*;
public class test {


    public static void main(String[] args) throws IOException 
    {
        BufferedReader in;
        in = new BufferedReader (new InputStreamReader (System.in));
        boolean again=true;
        String response;

        while (again)
        {

            System.out.println("Enter a letter to find it's order in the alphabet");

            char theLetter = (char) in.read();

            System.out.println(theLetter + " is the " + convertLetter(theLetter) + "th letter of the alphabet");

            System.out.println("want to play again?");

            response = in.readLine();

            if (response.charAt(0)=='n')
            {
                again=false;
            }
        }

        System.out.println("end program");
    }


    public static int convertLetter(char TheLetter)
    {
        //number value 'a'=97
        //number value 'b'=98
        //number value 'c'=99

        //subtracting 'a' from any other number will reveal how many places away that number is from the start
        //thus finding it's chronological place in the alphabet

        int NumberValue= (int)TheLetter;
        int a = 'a';

        int CalulateOrder = (NumberValue - a) + 1; 

        return CalulateOrder;

    }
}
  • 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-07T10:46:30+00:00Added an answer on June 7, 2026 at 10:46 am

    When you hit enter for the original char, that newline is still in the buffer since you only call read() and only get 1 character, leaving the newline in the buffer from hitting enter. So when you call readLine it simply hits that newline and returns an empty string.

    You can test this by typing something with more than one character when it first asks you for a character, and it will go for a second loop since the readLine will return a non-empty string.

    To fix this change your original read() to readLine() so that it gets the newline caused from you hitting enter, then just grab the first character from the string.

    This should fix it:

        import java.io.*;
    
    public class test {
    
        public static void main(String[] args) throws IOException {
            BufferedReader in;
            in = new BufferedReader (new InputStreamReader (System.in));
            boolean again=true;
            String response;
    
            while (again) {
                System.out.println("Enter a letter to find it's order in the alphabet");
                response = in.readLine();
                if (response.length() > 0) {
                    char theLetter = response.charAt(0);
    
                    System.out.println(theLetter + " is the " + convertLetter(theLetter) + "th letter of the alphabet");
                    System.out.println("want to play again?");
    
                    response = in.readLine();
                    if (response.length() > 0 && response.charAt(0)=='n') {
                        again=false;
                    }
                }
            }
            System.out.println("end program");
        }
    
        public static int convertLetter(char TheLetter) {
            return (TheLetter - 'a') + 1;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.