Problem In using Substring..

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2006
Posts: 12
Reputation: mohamad11 is an unknown quantity at this point 
Solved Threads: 0
mohamad11 mohamad11 is offline Offline
Newbie Poster

Problem In using Substring..

 
0
  #1
May 3rd, 2006
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){
System.out.println(recv_Message);
}

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(Integer.java:373)
at java.lang.Integer.parseInt(Integer.java:454)
at final_append.CopyBytes.main(CopyBytes.java:101)
Exception in thread "main"
how can i solve this??plz any help in
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem In using Substring..

 
0
  #2
May 3rd, 2006
/*
 * Pedantic.java
 *
 * 
 */

class Pedantic
{
    
  public static void main (String [] args) 
 { 
     String crap = "blah blah blah blah 2343";
     int length = crap.length();
     String c = crap.substring(length-4,length);
     int num = Integer.parseInt(c);
     System.out.println(num*2); //multiply by 2
  }

}

Actually I suspect it's something else. Hmm, are you sure the last four characters are in your file are definitely numbers?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Problem In using Substring..

 
0
  #3
May 3rd, 2006
Originally Posted by mohamad11
System.out.println("Your Received message is as following:");
while((recv_Message=buff2.readLine() )!=null){
System.out.println(recv_Message);
}

four_characters=recv_Message.substring(recv_Message.length() - 4);
Four_characters_Integers = Integer.parseInt(four_characters);
I haven't tried it, I would, however, assume that recv_Message is null
at the point where you try to get the last four characters, as your
while loop will not exit until it is. Try this:

  1. String last = "";
  2. System.out.println("Your Received message is as following:");
  3. while((recv_Message=buff2.readLine() )!=null){
  4. last = recv_Message;
  5. System.out.println(recv_Message);
  6. }
  7.  
  8. four_characters=last.substring(last.length() - 4);
  9. Four_characters_Integers = Integer.parseInt(four_characters);
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: DeepZ is an unknown quantity at this point 
Solved Threads: 0
DeepZ's Avatar
DeepZ DeepZ is offline Offline
Light Poster

Re: Problem In using Substring..

 
0
  #4
May 3rd, 2006
Well, the JVM is telling you that the last 4 characters of your string are not a number. So....
:?: Sometimes i wonder if i'm on the right planet :!:
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 12
Reputation: mohamad11 is an unknown quantity at this point 
Solved Threads: 0
mohamad11 mohamad11 is offline Offline
Newbie Poster

Re: Problem In using Substring..

 
0
  #5
May 5th, 2006
hi how could this happen if my message was as following:
5113350515586806264156901277970003516182249744328267808527911381008517422384937086806339111024080220
and it prints it ...so the last four caharcters are really numbers but it still gives me the same exception when i write the statement:
c = recv_Message.substring(length2-4,length2);
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem In using Substring..

 
0
  #6
May 5th, 2006
The only thing I can think of that is causing the problem must be the reading from the file part. (Basically what Deepz was saying)

Are you sure the end of the file has no extra spaces or newlines present?


Open your text file and keep pressing backspace so that the flashing cursor is right next to the last number in your file. Click save then try running the program again.

Why don't you post your entire code and your text file so we can see exactly what is going wrong.

And this is how I would read my file in:

  1. /*
  2.   * ped.java
  3.   *
  4.   * Created on 29 April 2006, 17:07
  5.   */
  6. import java.awt.*;
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10.  
  11. class Pedantic
  12. {
  13.  
  14. public static void main (String [] args)
  15. {
  16.  
  17. try {
  18. BufferedReader in = new BufferedReader(new FileReader("Myfile.txt"));
  19. String str;
  20. while ((str = in.readLine()) != null)//read in a line
  21. {
  22.  
  23. int length = str.length();
  24. String c = str.substring(length-4,length);
  25. int num = Integer.parseInt(c);
  26. //System.out.println(str);
  27. System.out.println(num*2); //multiply last four digit by 2
  28. }
  29. in.close();
  30. }
  31. catch (IOException e)
  32. {
  33.  
  34. }
  35.  
  36. }
  37.  
  38. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 12
Reputation: mohamad11 is an unknown quantity at this point 
Solved Threads: 0
mohamad11 mohamad11 is offline Offline
Newbie Poster

Re: Problem In using Substring..

 
0
  #7
May 6th, 2006
ok i will give my full source code for this Class in order you can help me in my Problem..
iam looking to implement digital signature and this class is responsible for appending the message to the Signature and extracting it from the message in order to make the verification process..
So i have the origional message stored in a text file,and also have the signature stored into another text file ...first of all i append these two files and it was appended but the problem was is that there was a line separator between the message and the Signature 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 tohave some company. Your friend
, Alice

511335051558680626415690127797000351618224974432826780852791138100851742238493708680633911102408
==============================================



i tried to solve my problem but i couldnt so i go on...i then store this in a new text file ...then i calculated the length of my origional message and represented it into four characters and then i appended it to the previous file and the result was 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 tohave some company. Your friend
, Alice

5113350515586806264156901277970003516182249744328267808527911381008517422384937086806339111024080220
================================================================================


note 0220 was my message length ,afetr that i store this result in a new file and tried to extract the last four characters to know the length of the origional message and here was the problem we talked about (in using substring and ParseInt ....i hope yiu understood my application and now here is the full source code:

  1.  
  2. package final_append;
  3. import java.io.*;
  4. public class CopyBytes {
  5. static String bobSignedMsg;
  6. static String SUBString;
  7. static int bobMsgLen;
  8. static String My_Message_Length ;
  9. static int len;
  10. static String recv_Message;
  11. static String Message_Length;
  12. static int length2;
  13. static String c;
  14. static int num;
  15. public static void main(String[] args) throws IOException {
  16. /*==========================Printing the input message========================*/
  17. try{
  18. FileInputStream Message_File=new FileInputStream ("C:/JBuilder4/lib/output8.txt");
  19. BufferedReader buff=new BufferedReader(new InputStreamReader(Message_File));
  20. String Input_Message=new String();
  21. System.out.println("Your input message is as following:");
  22. while((Input_Message=buff.readLine() )!=null){
  23. // Calculating the the length of our message in unit of bits
  24. System.out.println(Input_Message);
  25. len += Input_Message.length();
  26.  
  27. }
  28. System.out.println(len);
  29. /*==================here we will represent message length into four characters========================================================*/
  30. String My_Message_Length = "" + len;
  31. //Confirm number of characters in the string.
  32. if((My_Message_Length.length() > 4)
  33. || (My_Message_Length.length() <= 0)){
  34. System.out.println("Message length error.");
  35. System.exit(0);
  36. }//end if
  37. //Prepend leading zeros if necessary
  38. if(My_Message_Length.length() == 1){
  39. My_Message_Length = "000"
  40. + My_Message_Length;
  41. }else if(My_Message_Length.length() == 2){
  42. My_Message_Length = "00" + My_Message_Length;
  43. }else if(My_Message_Length.length() == 3){
  44. My_Message_Length = "0" + My_Message_Length;
  45. }else if(My_Message_Length.length() == 1){
  46. My_Message_Length = "000"
  47. + My_Message_Length;
  48. }
  49. System.out.println(My_Message_Length);
  50.  
  51. /*=========================Append the message length string to the=========
  52.   ========================= previously signed message=======================*/
  53. // create writer for file to append to
  54. BufferedWriter out = new BufferedWriter(
  55. new FileWriter("C:/JBuilder4/lib/output6.txt", true));
  56. // create reader for file to append from
  57. BufferedReader in = new BufferedReader(new FileReader("C:/JBuilder4/lib/output4.txt"));
  58. String str;
  59. while ((str = in.readLine()) != null) {
  60. out.write(str);
  61. }
  62. in.close();
  63. out.close();
  64. /*=================appending the length of message to My message=====================================================*/
  65. BufferedWriter bw = null;
  66. bw = new BufferedWriter(new FileWriter("C:/JBuilder4/lib/output6.txt", true));
  67. bw.write(My_Message_Length);
  68. bw.newLine();
  69. bw.flush();
  70.  
  71. //======================Sending the Message to the Receiver Side================
  72. recv_Message = null;
  73. FileInputStream Received_Message=new FileInputStream ("C:/JBuilder4/lib/output6.txt");
  74. BufferedReader buff2=new BufferedReader(new InputStreamReader(Received_Message));
  75. System.out.println("Your Received message is as following:");
  76. String four_characters = null;
  77. int Four_characters_Integers;
  78. while((recv_Message=buff2.readLine() )!=null){
  79. // Calculating the the length of our message in unit of bits
  80. System.out.println(recv_Message);
  81. four_characters = recv_Message.substring(recv_Message.length() - 4);
  82. Four_characters_Integers = Integer.parseInt(four_characters);
  83. System.out.println("last four characters are:" + four_characters);
  84. //length2 += recv_Message.length();
  85. //SUBString = recv_Message.substring(recv_Message.length() - 4);
  86. }
  87. } catch (FileNotFoundException e) {
  88. System.out.println("Cannot find file MyFile.txt \n" + e);
  89. } catch (IOException e) {
  90. System.out.println("IOException " + e);
  91. } catch (NumberFormatException e) {
  92. System.out.println("Not a number on the last 4 characters : " + recv_Message + "\n" + e);
in this stage it gave me the exception Not a number on the last 4 characters so why this happens????
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 12
Reputation: mohamad11 is an unknown quantity at this point 
Solved Threads: 0
mohamad11 mohamad11 is offline Offline
Newbie Poster

Re: Problem In using Substring..

 
0
  #8
May 7th, 2006
Any Help Plzzzzzz.........
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Problem In using Substring..

 
0
  #9
May 8th, 2006
Have you tried any of the suggestions given yet?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: aniseed is an unknown quantity at this point 
Solved Threads: 6
aniseed's Avatar
aniseed aniseed is offline Offline
Posting Whiz

Re: Problem In using Substring..

 
0
  #10
May 8th, 2006
Originally Posted by mohamad11
Any Help Plzzzzzz.........
You seem to have dozed off :rolleyes:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 5183 | Replies: 14
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC