Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-17-2001, 04:20 PM   #1
Member (8 bit)
 
Join Date: Jan 2001
Location: Christiansburg, Virginia
Posts: 246
Send a message via AIM to DukeDiablo
Question Need help with a simple Java program

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
DukeDiablo is offline   Reply With Quote
Old 09-17-2001, 06:05 PM   #2
Member (12 bit)
 
Paul Victorey's Avatar
 
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.
Paul Victorey is offline   Reply With Quote
Old 09-17-2001, 06:45 PM   #3
Member (8 bit)
 
Join Date: Jan 2001
Location: Christiansburg, Virginia
Posts: 246
Send a message via AIM to DukeDiablo
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
DukeDiablo is offline   Reply With Quote
Old 09-17-2001, 08:02 PM   #4
Member (8 bit)
 
Join Date: Jan 2001
Location: Christiansburg, Virginia
Posts: 246
Send a message via AIM to DukeDiablo
Talking THANKS AGAIN

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
DukeDiablo is offline   Reply With Quote
Old 09-17-2001, 10:26 PM   #5
Member (12 bit)
 
Paul Victorey's Avatar
 
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.
Paul Victorey is offline   Reply With Quote
Old 09-18-2001, 05:39 AM   #6
Member (8 bit)
 
Join Date: Jan 2001
Location: Christiansburg, Virginia
Posts: 246
Send a message via AIM to DukeDiablo
Talking Thanks again

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
DukeDiablo is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:26 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2