Note that there are some explanatory texts on larger screens.

plurals
  1. POsimple java program not working -- while-loop
    primarykey
    data
    text
    <p>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.</p> <p>Any help would be greatly appreciated thanks!</p> <pre><code>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; } } </code></pre>
    singulars
    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.
 

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