Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem in Using Substring

0 views
Skip to first unread message

mohamad

unread,
May 3, 2006, 8:09:00 AM5/3/06
to
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

Heiner Kücker

unread,
May 3, 2006, 8:41:57 AM5/3/06
to
mohamad wrote
> four_characters=recv_Message.substring(recv_Message.length() - 4);

System.out.println( "'" + four_characters + "'" );

// is the four_characters string correct ?
// remove blanks with four_characters.trim();
// remove leading zeros
// remove fractional digits an decimal point


> 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);

/**
* String in int umwandeln
*
* @param numStr String numerisch
* @return Zahl
*/
public static final int str2int(
/*final*/String numStr )
{
if (empty( numStr ))
{
return 0;
}

numStr = numStr.trim();

int retInt = 0;

try
{
retInt = ( new Integer( numStr ) ).intValue();
}
catch (Throwable e)
{
;
}

return retInt;
} // end method str2int

/**
* String in Integer-Object umwandeln
*
* @param numStr String numerisch
* @return Intger-Object
*/
public static final Integer str2Integer(
final String numStr )
{
Integer retInteger = null;

retInteger = new Integer( str2int( numStr ) );
return retInteger;
} // end method str2Integer

/**
* String in Integer-Object umwandeln mit Rückgabe null wen nicht lesbar
*
* @param numStr String numerisch
* @return Integer-Object
*/
public static final Integer str2IntegerStrict(
final String numStr )
{
Integer retInteger = null;

try
{
retInteger = new Integer( numStr );
}
catch (Throwable e)
{
;// nicht lesbar , return null
}

return retInteger;
} // end method str2IntegerStrict

/**
* Parsen eines Strings in ein Integer,
* unter Nachsicht auf führende Nullen
* und nachfolgende Buchstaben oder
* Sonderzeichen
*
* @author Heiner Kücker
*/
public static Integer str2IntegerLenient(
final String pStr )
{
if (pStr == null
|| pStr.length() == 0)
{
return null; // Garbage in, Garbage out
}
final String trimmedStr = pStr.trim();
StringBuffer tmpStrBuff = new StringBuffer();
int iPos = 0;
//laufen über führende Nullen
for (; iPos < trimmedStr.length() - 1
&& trimmedStr.charAt( iPos ) == '0'; iPos++ )
;
//sammeln aller Digits
for (; iPos < trimmedStr.length()
&& Character.isDigit( trimmedStr.charAt( iPos ) ); iPos++ )
{
tmpStrBuff.append( trimmedStr.charAt( iPos ) );
}
Integer retInteger = null;
try
{
retInteger = new Integer( tmpStrBuff.toString() );
}
catch (Throwable e)
{
;
}
return retInteger;
}

/**
* String in int umwandeln mit Tolerierung enthaltener nichtnumerischer
Zeichen . <br/>
* geparst wird bis zum Auftreten nichtnumerischer Zeichen.<br/>
* Entfernung führender Nullen läßt sich zuschalten
* (Parameter pRemoveLeadingZeros).<br/>
*
* @param numStr String numerisch
* @return int Zahl
*/
public static final int str2intTolerant(
/*final*/String numStr , //--
final boolean pRemoveLeadingZeros )
{
if (empty( numStr ))
{
return 0;
}

numStr = numStr.trim();

if (pRemoveLeadingZeros)
{
//Entfernen führende Nullen
while (numStr.length() > 1
&& numStr.startsWith( "00" ))
{
numStr = numStr.substring( 1 );
}
}

// Entfernen evtl. nichtnumerische Zeichen am ende
for (int i = 0; i < numStr.length(); i++ )
{
if (numStr.charAt( i ) < '0'
|| numStr.charAt( i ) > '9')
{

// nicht numerisches Zeichen
numStr = numStr.substring( 0 , i );

break;
}
}

int retInt = 0;

try
{
retInt = ( new Integer( numStr ) ).intValue();
}
catch (Throwable e)
{
;
}

return retInt;
} // end method str2intTolerant


--
Heiner Kücker
www.heinerkuecker.de
www.control-and-command.de

Heiner Kücker

unread,
May 3, 2006, 9:35:39 AM5/3/06
to
Heiner Kücker schrieb

/**
* String auf null, Länge 0 oder nur
* white spaces prüfen
*
* @param pStr zu prüfender String
* @return ob String empty (ja/nein)
*/
public static final boolean empty(
final String pStr )
{
return ( pStr == null //--
|| pStr.trim().length() < 1 );
} // end method empty

mohamad

unread,
May 5, 2006, 6:09:24 PM5/5/06
to
iam sorry i cant read your language u seem f German can u reply in
english???

Paul Ebermann

unread,
May 6, 2006, 10:24:04 PM5/6/06
to
"mohamad" skribis:

> iam sorry i cant read your language u seem f German can u reply in
> english???

Maybe you should use an english language newsgroup,
like comp.lang.java.*.

The "de" in "de.comp.lang.java" stands für "german".


Paul
--
Ich beantworte Java-Fragen per E-Mail nur gegen Bezahlung. Kostenpunkt
100 Euro pro angefangene Stunde. Bitte erwähne in der Anfrage, dass du
das akzeptierst, da ich sonst die E-Mail einfach ignoriere.
(In der Newsgroup fragen ist billiger und oft auch schneller.)

0 new messages