|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (8 bit)
|
Dear Forum-
I'm new at programming in Java, but the following is what I have, just let me know if there is something in my code and setup that would prevent the program from running. When I try to run it, it actually executes; however, it prints out the following information in the run window: ________________________________________________________________________________ Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind ex out of range: -5 at java.lang.String.substring(String.java:1492) at java.lang.String.substring(String.java:1459) at jakenned.main(jakenned.java, Compiled Code) Press Enter to continue ________________________________________________________________________________ So if anyone has time, this is my actual code: import java.io.*; class jakenned { public static void main (String[] arg) throws Exception { //setting up all the input and output files and streams File plphrases = new File("plphrases.txt"); FileInputStream fis = new FileInputStream(plphrases); InputStreamReader isr = new InputStreamReader(fis); BufferedReader fileInput = new BufferedReader(isr); File piglatinoutput = new File("Report.text"); FileOutputStream fos = new FileOutputStream(piglatinoutput); PrintStream translatedfile = new PrintStream(fos); //printing out the program header information to the output file translatedfile.println("Programmer: James M. Kennedy"); translatedfile.println("CS 2984 Project 1 Fall 2001"); translatedfile.println(); //declare the input String inputstring; //priming read inputstring = fileInput.readLine(); //start the loop for this program while (inputstring != null) { //assign the input a value inputstring = fileInput.readLine(); //declare the specific sections of the input String partone; String parttwo; String partthree; String partfour; //declare the temporary place holder for the program String temp; //assign the specific sections of the input a value partone = inputstring.substring(0,3); parttwo = inputstring.substring(6,11); partthree = inputstring.substring(14,18); partfour = inputstring.substring(21,25); //make the temporary place holder get the last letter of the word //then put that letter at the beginning of partone temp = partone.substring(2); partone = temp.concat(partone); //make the temporary place holder get the last letter of the word //then put that letter at the beginning of parttwo temp = parttwo.substring(10); parttwo = temp.concat(parttwo); //make the temporary place holder get the last letter of the word //then put that letter at the beginning of partthree. temp = partthree.substring(17); partthree = temp.concat(partthree); //make the temporary place holder get the last letter of the word //then put that letter at the beginning of partfour temp = partfour.substring(24); partfour = temp.concat(partfour); //print out a line of translated piglatin to the //output file in the correct order. translatedfile.print(partfour); translatedfile.print(" "); translatedfile.print(partone); translatedfile.print(" "); translatedfile.print(partthree); translatedfile.print(" "); translatedfile.println(parttwo); } } } Thank you so much in advance for any help... Sincerely, James Kennedy DukeDiablo@Aol.com |
|
|
|
|
|
#2 |
|
Member (12 bit)
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
|
Well, first of all, you are, with this code, doing this:
* Your orignal string is at least 26 characters long. You are assigning: partone = chars 0-3 (4 characters long) parttwo = chars 6-11 (6 characters long) partthree = chars 14-18 (5 characters long) partfour = chars 21-25 (5 characters long) Now, look at this code of yours: //make the temporary place holder get the last letter of the word //then put that letter at the beginning of parttwo temp = parttwo.substring(10); parttwo = temp.concat(parttwo); What this is saying is, take everything after the 11th character from string "parttwo" and assign it to "temp". Buuut, parttwo is only SIX characters long, so it throws an exception because there IS no 11th character. To get the last character of parttwo, you can always use this: temp = parttwo.substring(parttwo.length()-1); or you could manually do the math. Because parttwo is 6 characters long, you need to do substring(5) if you want to do the math by hand. HTH
__________________
Paul M. Victorey ------------------ I am not responsible for any problems that may arise as a result of following my advice. This includes, but is not limited to, computer failure, loss of data, nuclear war, famine, boils, no clean laundry, your daughter running off with a biker gang, or armageddon. Take my advice at your own risk. Last edited by Paul Victorey; 09-17-2001 at 06:09 PM. |
|
|
|
|
|
#3 |
|
Member (8 bit)
|
Thanks so much
Mr. Victorey
Thanks so much for your help, after reading your reply I understand my error. Have a great week. Sincerely, James Kennedy DukeDiablo@Aol.com |
|
|
|
|
|
#4 |
|
Member (8 bit)
|
Mr. Victorey-
After some tweaking, my program spits out the correct output. Thanks so much. I do get this strange error code at the end, but the output file is right. Let me know if this is a serious problem. ________________________________________________________________________________ Exception in thread "main" java.lang.NullPointerException at jakenned.main(jakenned.java, Compiled Code) Press Enter to continue ________________________________________________________________________________ Thanks again, Sincerely, James Kennedy DukeDiablo@Aol.com |
|
|
|
|
|
#5 |
|
Member (12 bit)
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
|
Yes, I see why this happens.
You have this section of code: //priming read inputstring = fileInput.readLine(); //start the loop for this program while (inputstring != null) { //assign the input a value inputstring = fileInput.readLine(); Now, the priming read is correct, but because you want to stop as soon as you get null, so your next read should be at the END of the while(){} loop, not at the beginning -- this way, the test to determine if inputstring is null happens AFTER you read, but before you use the data that you read. Also, the way you currently have it, the very first line of the file will be ignored, because it will be read by the priming read, then the next line will be read immediately after the loop starts, so the first line gets lost. What happens currently is this: * After the last line has been processed, it checks to see if inputstring is null. It is NOT, because it still contains the last line read. So it continues, when it should stop. * It reads another line, which makes inputstring null. * Later, you try to use a substring of inputstring, which throws a NullPointerException. So, the way to fix this is to remove the readLine() inside the while loop at the beginning, and move it to the end, after you print to the file, but before you close the while loop. Last edited by Paul Victorey; 09-17-2001 at 10:33 PM. |
|
|
|
|
|
#6 |
|
Member (8 bit)
|
Mr. Victorey-
Thanks again for your help, and I now see my logic error with the second read. I moved it to the end of the loop. Also I am reading in that first line of code(just like you said I would). Thanks. Sincerely, James Kennedy |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|