| | |
Problem In using Substring..
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2006
Posts: 12
Reputation:
Solved Threads: 0
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
/*
* 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?
•
•
•
•
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);
at the point where you try to get the last four characters, as your
while loop will not exit until it is. Try this:
Java Syntax (Toggle Plain Text)
String last = ""; System.out.println("Your Received message is as following:"); while((recv_Message=buff2.readLine() )!=null){ last = recv_Message; System.out.println(recv_Message); } four_characters=last.substring(last.length() - 4); 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
----------------------------------------------
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
•
•
Join Date: May 2006
Posts: 12
Reputation:
Solved Threads: 0
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);
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);
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:
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:
Java Syntax (Toggle Plain Text)
/* * ped.java * * Created on 29 April 2006, 17:07 */ import java.awt.*; import java.io.*; import java.util.*; class Pedantic { public static void main (String [] args) { try { BufferedReader in = new BufferedReader(new FileReader("Myfile.txt")); String str; while ((str = in.readLine()) != null)//read in a line { int length = str.length(); String c = str.substring(length-4,length); int num = Integer.parseInt(c); //System.out.println(str); System.out.println(num*2); //multiply last four digit by 2 } in.close(); } catch (IOException e) { } } }
•
•
Join Date: May 2006
Posts: 12
Reputation:
Solved Threads: 0
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:
in this stage it gave me the exception Not a number on the last 4 characters so why this happens????
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:
Java Syntax (Toggle Plain Text)
package final_append; import java.io.*; public class CopyBytes { static String bobSignedMsg; static String SUBString; static int bobMsgLen; static String My_Message_Length ; static int len; static String recv_Message; static String Message_Length; static int length2; static String c; static int num; public static void main(String[] args) throws IOException { /*==========================Printing the input message========================*/ try{ FileInputStream Message_File=new FileInputStream ("C:/JBuilder4/lib/output8.txt"); BufferedReader buff=new BufferedReader(new InputStreamReader(Message_File)); String Input_Message=new String(); System.out.println("Your input message is as following:"); while((Input_Message=buff.readLine() )!=null){ // Calculating the the length of our message in unit of bits System.out.println(Input_Message); len += Input_Message.length(); } System.out.println(len); /*==================here we will represent message length into four characters========================================================*/ String My_Message_Length = "" + len; //Confirm number of characters in the string. if((My_Message_Length.length() > 4) || (My_Message_Length.length() <= 0)){ System.out.println("Message length error."); System.exit(0); }//end if //Prepend leading zeros if necessary if(My_Message_Length.length() == 1){ My_Message_Length = "000" + My_Message_Length; }else if(My_Message_Length.length() == 2){ My_Message_Length = "00" + My_Message_Length; }else if(My_Message_Length.length() == 3){ My_Message_Length = "0" + My_Message_Length; }else if(My_Message_Length.length() == 1){ My_Message_Length = "000" + My_Message_Length; } System.out.println(My_Message_Length); /*=========================Append the message length string to the========= ========================= previously signed message=======================*/ // create writer for file to append to BufferedWriter out = new BufferedWriter( new FileWriter("C:/JBuilder4/lib/output6.txt", true)); // create reader for file to append from BufferedReader in = new BufferedReader(new FileReader("C:/JBuilder4/lib/output4.txt")); String str; while ((str = in.readLine()) != null) { out.write(str); } in.close(); out.close(); /*=================appending the length of message to My message=====================================================*/ BufferedWriter bw = null; bw = new BufferedWriter(new FileWriter("C:/JBuilder4/lib/output6.txt", true)); bw.write(My_Message_Length); bw.newLine(); bw.flush(); //======================Sending the Message to the Receiver Side================ recv_Message = null; FileInputStream Received_Message=new FileInputStream ("C:/JBuilder4/lib/output6.txt"); BufferedReader buff2=new BufferedReader(new InputStreamReader(Received_Message)); System.out.println("Your Received message is as following:"); String four_characters = null; int Four_characters_Integers; while((recv_Message=buff2.readLine() )!=null){ // Calculating the the length of our message in unit of bits System.out.println(recv_Message); four_characters = recv_Message.substring(recv_Message.length() - 4); Four_characters_Integers = Integer.parseInt(four_characters); System.out.println("last four characters are:" + four_characters); //length2 += recv_Message.length(); //SUBString = recv_Message.substring(recv_Message.length() - 4); } } catch (FileNotFoundException e) { System.out.println("Cannot find file MyFile.txt \n" + e); } catch (IOException e) { System.out.println("IOException " + e); } catch (NumberFormatException e) { System.out.println("Not a number on the last 4 characters : " + recv_Message + "\n" + e);
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
----------------------------------------------
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
![]() |
Similar Threads
- How can fix the problem? (JavaScript / DHTML / AJAX)
- WebCrawler problem (Java)
- "cannot resolve symbol"problem (Java)
- Problem with String replaceAll method (Java)
- Big Problem, Generic Error (VB.NET)
Other Threads in the Java Forum
- Previous Thread: [help] Split word in Java
- Next Thread: how to ignore Separators???
Views: 5183 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






