File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Problem in Substring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in Substring" Watch "Problem in Substring" New topic

Problem in Substring

Ranet Saed

Joined: May 02, 2006
Posts: 7
Hi ,I have a text file and it contains a message ,I want to extract the last four characters in this file and then convert it to integer i did the following:
FileInputStream Received_Message=new FileInputStream ("MyFile.txt");
BufferedReader buff2=new BufferedReader(new InputStreamReader(Received_Message));

System.out.println("Your Received message is as following:");
while((recv_Message=buff2.readLine() )!=null){

four_characters=recv_Message.substring(recv_Message.length() - 4);
Four_characters_Integers = Integer.parseInt(four_characters);
i sure have try and catch and defined all varibles above
but the problem it gives me this message when i use this statement:
four_characters=recv_Message.substring(recv_Message.length() - 4);
the Exception is as following:
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(
at java.lang.Integer.parseInt(
at final_append.CopyBytes.main(
Exception in thread "main"
how can i solve this??plz any help in
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547

first something else...
- use code tags to format code you post!
- try to stick with the coding guidelines from sun (recv_Message -> recvMessage, Four_characters_Integers -> fourCharacterIntegers and so on). this code is hard to read!

the exception does not occur in the line

since it is an exception that is thrown by Integer.parseInt()!

but the line you mention is probably the problem: as the exception says you are passing null in the parseInt() call. what is the value of four_characters ? can you show the full code ?

Steve Bret

Joined: May 03, 2006
Posts: 6
This should do what you want:

try to follow the rules when naming variables/objects
variables should start with a small characters
So Receive_Message --> receiveMessage

Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
Wouldn't it be better if you use RandomAccessFile and directly call seek(length()-4) to move to last but 4th postion and read last 4 characters, rather than readnig whole file in a whilr loop.

Ranet Saed

Joined: May 02, 2006
Posts: 7
I have the same problem it gives me an ecxeption ,I tried your code Steve but i had this statement which you put in the exception:
Not a number on the last 4 characters
but the problem is the recv_message is as following:
Hello Bob, how are you? I am going to go to the movie tonight. I plan to see Gonewith the Wind. I really like Clark Gable. Would you like to go with me? I would really like to have some company. Your friend, Alice5113350515586806264156901277970003516182249744328267808527911381008517422384937086806339111024080220

i got it from appending two files
the last four characters 0220 i got them from representing the length of the message in four characters by this code :

String My_Meesage_Length = "" + len;
//Confirm number of characters in the string.
if((My_Meesage_Length.length() > 4)
|| (My_Meesage_Length.length() <= 0)){
System.out.println("Message length error.");
}//end if
//Prepend leading zeros if necessary
if(My_Meesage_Length.length() == 1){
aliceMsgLenAsStr = "000"
}else if(My_Meesage_Length.length() == 2){
My_Meesage_Length= "00" + My_Meesage_Length;
}else if(aliceMsgLenAsStr.length() == 3){
My_Meesage_Length = "0" + My_Meesage_Length;
}else if(My_Meesage_Length.length() == 1){
My_Meesage_Length = "000"
+ My_Meesage_Length;

so this may makes the problem more clear...
so by this the last four characters are numbers so why it keeps give me the same problem...plz any help
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
If you are using Java 5, you can use a Scanner:

[ May 06, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
samir Sadiki
Ranch Hand

Joined: Apr 22, 2006
Posts: 31
Hi Ranet,
Take a closer look at what you are doing here:

when u leave the loop, recv_Message is null and yet after that u r trying to get the last 4 characters from null!!!
try this instead:
samir Sadiki
Ranch Hand

Joined: Apr 22, 2006
Posts: 31
...and u might wanna reconsider ur variable names as well... lol.javascript:%20x()
Good luck
Ranet Saed

Joined: May 02, 2006
Posts: 7
Hi ....Thanks Samir a lot u really helped me with my problem ...i didnt take care of wut i did before...any way i want to ask about another problem i have in the same Class ...I have to append two strings but each one must be saved in a seperate text file ,so the requiered here is to append two text files ...I did that ,but the problem was that each string was in a separate line i dont know why ...for example if i have two text files file1.txt[Hi Bib how are You]
the result for the appending was
Hi Bob how are You

but sure i need this result:
Hi Bob how are You1243456778889990
my code is as folowing:

/ create writer for file to append to
BufferedWriter out = new BufferedWriter(
new FileWriter("file1.txt", true));
// create reader for file to append from
BufferedReader in = new BufferedReader(new FileReader("file2.txt"));
String str;
while ((str = in.readLine()) != null) {

so how can modify my code to make the right apending as I need???
Ranet Saed

Joined: May 02, 2006
Posts: 7
Hi plz i need an argent and good help in the problem of the two files to append
I agree. Here's the link:
subject: Problem in Substring
It's not a secret anymore!