Conversion from String to int..NumberFormatException??

Hey all
I am trying to read in a file that has nothing but numbers listed one per line. I am using the bufferedReader class and reading in by line. When I check to see if the one 'string' is the same as another, it says they are the same, when really they are different. I then thought to convert the numbers to integers after reading them in, but it gives me a number format exception. My process is simple:
1) line = in.readLine();
2) int variable = Integer.valueOf(line).intValue();
This gives me the damn exception. The numbers are three digits, e.g. 333, etc. Does anybody know how I can avert this problem or achieve the result I am looking for another way? All I want is to know what numbers are listed in their, without going through by hand checking. Thanks

hi...try this..
1.Use filereader instead of bufferedreader..OKAY
2. OR USE...
String str = br.readLine();
use... int sanjeev = Integer.parseInt(str);
3 OR...and helloooo one thing more...how u r checking that these 2 strings r same..
get CLEAR IDEA about equals method and "=="..

Similar Messages

  • Please help me with my code (has conversion from string to int)

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    public class Test3 extends MIDlet implements CommandListener
    {   Form mForm;
        Command mCommandQuit;
        Command mCommandItem;
        TextField input,prime1,prime2,prime3,output;
        Display mDisplay;
        int i,j=0,k1,k2,p,q,b;
        //int [] current=new int [1000];
        String mstring,a="";
        String [] temp=new String [1000];
        public void startApp()
        {System.out.println("startApp");
         mForm=new Form("RSA Encryption");
         mCommandQuit=new Command("QUIT",Command.EXIT,0);
         mCommandItem=new Command("ENCRYPT",Command.ITEM,0);
         mForm.append("Enter text:\n");
         input=new TextField(null,"",100, TextField.ANY);
         mForm.append(input);
         mForm.append("Enter first prime number(p):\n");
         prime1=new TextField(null,"",100, TextField.ANY);
         mForm.append(prime1);
         mForm.append("Enter second prime number(q):\n");
         prime2=new TextField(null,"",100, TextField.ANY);
         mForm.append(prime2);
         mForm.append("Enter d:\n");
         prime3=new TextField(null,"",100, TextField.ANY);
         mForm.append(prime3);
         mForm.addCommand(mCommandQuit);
         mForm.addCommand(mCommandItem);
         mDisplay=Display.getDisplay(this);
         mDisplay.setCurrent(mForm);
         mForm.setCommandListener(this);
        public void pauseApp()
        {System.out.println("pauseApp");
        public void destroyApp(boolean unconditional)
        {System.out.println("destroyApp");
        public void commandAction(Command c, Displayable d)
        {System.out.println("commandAction");
         if(c==mCommandQuit)
            notifyDestroyed();
         else if(c==mCommandItem)
         {p = Integer.parseInt(prime1.getString());
             q = Integer.parseInt(prime2.getString());
             b = Integer.parseInt(prime3.getString());
             //breaking up of big string into ints
             mstring=input.getString();
             temp[0]="";
             for(i=1;i<mstring.length();i++)
                {if (mstring.charAt(i) == ' ')
                    {j++;
                     temp[j]="";
                 else
                    {temp[j]=temp[j]+mstring.charAt(i);
             mForm.append("\n\nThe array is:\n");
             for(i=0;i<temp.length && temp!=null;i++)
    {k1=Integer.parseInt(temp[i]); ***********************
    k2=k1;
    for(j=1;j<b;j++)
    {k1=k1*k2;
    k1=k1 %(p*q);
    k2=k1 %(p*q);
    a=a+new Character((char)k2).toString();
    output=new TextField(null,a,100, TextField.ANY);
    mForm.append(output);
    }hi
    this code basically takes an input of string like " 179 84 48 48 155 " (with spaces)
    then it creates smaller strings in an array like "179","84","48","48","155" without the spaces
    then it creates int values 179,84,48,48,155 and finally after some math functions it prints the corresponding letters.
    the problem is that it is not printing the letter because of some exceptions-->java.lang.NumberFormatException .it comes in the line where i have put stars
    could anybody please help me print the letters                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    thanks for all ur help guys, but me and my team member solved it on our own. here is the new code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    public class Test3 extends MIDlet implements CommandListener
    {   Form mForm;
        Command mCommandQuit;
        Command mCommandItem;
        TextField input,prime1,prime2,prime3,output;
        Display mDisplay;
        int i,j=0,l,k1,k2,p,q,n,b;
        String mstring,a = "";
        String [] temp=new String [1000];
        public void startApp()
        {System.out.println("startApp");
         mForm=new Form("RSA Encryption");
         mCommandQuit=new Command("QUIT",Command.EXIT,0);
         mCommandItem=new Command("DECRYPT",Command.ITEM,0);
         mForm.append("Enter text:\n");
         input=new TextField(null,"",100, TextField.ANY);
         mForm.append(input);
         mForm.append("Enter first prime number(p):\n");
         prime1=new TextField(null,"",100, TextField.NUMERIC);
         mForm.append(prime1);
         mForm.append("Enter second prime number(q):\n");
         prime2=new TextField(null,"",100, TextField.NUMERIC);
         mForm.append(prime2);
         mForm.append("Enter d:\n");
         prime3=new TextField(null,"",100, TextField.NUMERIC);
         mForm.append(prime3);
         mForm.addCommand(mCommandQuit);
         mForm.addCommand(mCommandItem);
         mDisplay=Display.getDisplay(this);
         mDisplay.setCurrent(mForm);
         mForm.setCommandListener(this);
        public void pauseApp()
        {System.out.println("pauseApp");
        public void destroyApp(boolean unconditional)
        {System.out.println("destroyApp");
        public void commandAction(Command c, Displayable d)
        {System.out.println("commandAction");
         if(c==mCommandQuit)
            notifyDestroyed();
         else if(c==mCommandItem)
         {p = Integer.parseInt(prime1.getString());
             q = Integer.parseInt(prime2.getString());
             b = Integer.parseInt(prime3.getString());
             n=p*q;
             //breaking up of big string into ints
             mstring=input.getString();
             temp[0]="";
             for(i=1;i<mstring.length();i++)
                {if (mstring.charAt(i) == ' ')
                    {j++;
                     temp[j]="";
                 else
                    {temp[j]=temp[j]+mstring.charAt(i);
             l=j;
             mForm.append("\n\nThe result is:\n");
             for(i=0;i<l;i++)
                {k1=Integer.valueOf(temp).intValue();
    k2=k1;
    for(j=1;j<b;j++)
    {k2=k2*k1;
    k2=k2 %n;
    k1=k2 %n;
    a=a+new Character((char)k1).toString();
    output=new TextField(null,a,100, TextField.ANY);
    mForm.append(output);

  • Conversion from String to Int

    import java.io.*;
    import java.util.*;
    import java.lang.*;
    public static void main(String[] args) throws Exception{
    int Ze=0;
    String Temp = "512";
    Ze = parseInt(Temp);
    I got an error saying --->>
    cannot resolve symbol
    symbol : method parseInt(java.lang.String)
    I tried using ...Integer(Temp)
    and it gives me a similar error message.
    where Did i went wrong???

    http://java.sun.com/j2se/1.5.0/docs/api/index.html
    Bookmark that page. It's the index to the documentation of every class in Java 1.5.0. If you've got a different JDK, look on the download page and find the Documentation. Look for the API documentation in that.
    Go look up the Byte class and see. The API documentation tells every available method for every available class in the java.*, javax.*, and org.* packages. Better start getting used to using it now rather than later.

  • Conversion from string "20041023 " to type 'Date' is not valid.

    Hi ,
       I have a table with one of the column(EmpHiredate) datatype is char(10). It has value like "20141023". I need to display this value as date format(dd/mm/yyyy) in report. 
    Following methods i tried in textbox expression but no luck.
    =Format(Fields!EmpHireDate.Value,"dd/MM/yyyy")
    =Cdate(Fields!EmpHireDate.Value)
    Error:
    [rsRuntimeErrorInExpression] The Value expression for the textrun ‘EmpHireDate.Paragraphs[0].TextRuns[0]’ contains an error: Conversion from string "20041023  " to type 'Date' is not valid.
    Is it possible to convert string to date using SSRS textbox expression ? Can anyone help me with the solution.
    Thanks,
    Kittu

    Hi Jmcmullen,
         Found one more issue on the same. I have one value like "00000000" for the column(EmpHiredate)
    , when i use above expression values(ex:"20141023")
    are displaying in dd/MM/yyyy format in report except value like "00000000" and giving following error:
    [rsRuntimeErrorInExpression] The Value expression for the textrun ‘EmpHireDate.Paragraphs[0].TextRuns[0]’ contains an error: Conversion from string "0000/00/00" to type 'Date' is not valid.
    Even i tried to pass its original value("00000000") as below but no luck.
    =IIF(Fields!EmpHireDate.Value = "00000000","00000000",Format(CDATE(MID(Fields!EmpHireDate.Value,1,4) + "/" + MID(Fields!EmpHireDate.Value,5,2) + "/" + MID(Fields!EmpHireDate.Value,7,2)),"dd/MM/yyyy"))
    Also tried this:
    =IIF(Fields!EmpHireDate.Value = "00000000","2000/10/21",Format(CDATE(MID(Fields!EmpHireDate.Value,1,4) + "/" + MID(Fields!EmpHireDate.Value,5,2) + "/" + MID(Fields!EmpHireDate.Value,7,2)),"dd/MM/yyyy"))
    Please Suggest. 
    Thanks ,
    Kittu

  • Conversion from string "" to type 'Double' is not valid

    We're using BPC 7.5 MS and on patch level 111.02
    There's two front-end servers and one back-end.  we have about 100 users but concurrency is likely in the 50 range.
    We've been running extremely well for about 4 years but are now starting to run into problems.
    There are about 5-6 applications but none greater than 10 million records.  We optimize regularly.
    However over the past two months admin processes seem to take a lot longer.  Optimization now times out after about 1.25 hours and none used to take greater than 15 minutes.
    Today after processing dimensions we checked the application status and we were not able to check if it was available or not.  We got a pop up box saying
    conversion from string "" to type 'Double' is not valid
    I've processed dimensions and applications since and still this error persists.
    We're attempting a reboot to see if that helps but I've never seen this before and in combination with the slower admin processing I'm wondering if there's something drastic going to happen.
    Michael

    Hi Michael,
    for the poor performances you have to check the guides about performance on bpc if you have scheduled regurarly optimize during day and night (just verify that never factwb reach 50.000 and fac2 500.000 records) maybe you need to change some parameter on the server as MaxThreads.
    For the conversion string error on admin console see please this note 1803092 - Set application set status error
    Regards
         Roberto

  • Deprecated conversion from string constant to 'char*'

    Hi all
    I am working with strings and i cant figure out why the following
    warning appears at time of build.
    warning: deprecated conversion from string constant to 'char*'
    It appears for the line
    char *myName = "Apple.txt";
    Is there anyone who can help me?
    Help is welcome.
    Thanks in advance.

    Any reason why you aren't using NSString in place of char?
    char *myName = "Apple.txt";
    NSString *myName = @"Apple.txt";

  • Convert from String to Int

    Hi,
    I have a question.
    How do I convert from String to Integer?
    Here is my code:
    try{
    String s="000111010101001101101010";
    int q=Integer.parseInt(s);
    answerStr = String.valueOf(q);
    catch (NumberFormatException e)
    answerStr="Error";
    but, everytime when i run the code, i always get the ERROR.
    is the value that i got, cannot be converted to int?
    i've done the same thing in c, and its working fine.
    thanks..

    6kyAngel wrote:
    actually it convert the string value into decimal value.In fact what it does is convert the (binary) string into an integer value. It's the String representation of integer quantities that have a base (two, ten, hex etc): the quantities themselves don't have a base.
    To take an integer quantity (in the form of an int) and convert it to a binary String, use the handy toString() method in the Integer class - like your other conversion, use the one that takes a radix argument.

  • Leading Zeroes are lost when convert from string to int

    What I'm trying to do is simple yet the solution has seemed difficult to find.
    I have a system that requires 4 digit numbers as "requisitionNo". The system uses JSPs and accepts the 4 digit number that the user inputs (fyi - duplicate handling is already managed). The input number (rNumber) is of STRING type and in the action (using struts) is converted to an int:
    int requisitionNo = Integer.parseInt(rNumber);At that very line the issue is that when the user inputs a number with leading zeros such as: "0001" the 3 leading zeros are chopped off in the INT conversion. The application validation kicks in and says: "A 4 digit number is required" which is by design. The work around has been that the user has been putting in number that start with 9's or something like that, but this isn't how the system was intended to be used.
    How do I keep the leading zeroes from being lost instead of saving a number "1" to the database how do I keep it saving "0001" to the database? Would I just change everything to STRING on down to the database? or is there another number type that I can be using that will not chop off the leading zeroes? Please provide short code references or examples to be more helpful.

    Yeah, I have to agree here that leading zeroes make no sense. I figured that out when I started to look into this problem. The only requirement that exists is that the user wants it to be a 4 digit number due to some other requirement they have themselves.
    So what I'm gathering from what I've read in the responses thus far is that I should change the validation a bit to look at the STRING for the 4 required digits (which are really 4 characters; maybe I should add CLIENT side numeric validation; currently its doing server side numeric/integer validation; or maybe change up the server side validation instead???) and if they are ALL GOOD then let the application save the int type as it wants to. IE: Let it save "0001" as just "1" and when I come back to DISPLAY this saved number to the user I should append the string of "000" in front of the 1 for display purposes only? Am I understanding everyone correctly?

  • Parsing Strings to Int, NumberFormatException ??

    Hey everyone...
    I am trying to convert some string to an int value..
    Now this is what is exactly happening.. First i read some bytes into an array of bytes from a file. Then i use the toString() to convert it to a string. Once i get the string representation of it i use Integer.parseInt() to convert it to int. Now here i get a NumberFormatException, and i guess i know why. The file that has the bytes to be read represents char as 8 bits and in Java char are 16 bits. So could that be the problem. And secondly, when i convert the bytes to string, should'nt it convert it to a 16 bit char ??? I've also tried to use radix like Integer.parseInt(String s, radix 16) yet it does'nt work.. Thanks in advance....

    Thanks...
    Another question, how do i represent an US-ASCII enoding in the constructor...??? What i mean is what do i put inside this new String(byte_array, - - - - -) if i want to do them as ASCII ???

  • Parsing from string to int

    in my C# project I have an array type of String. the array gets numbers from type int.
    int numCell;
    string[] arr = new string[numCell];
    then i use it in a function like this:
    sum += int.Parse(arr[i]);
    and it works!
    but, when i dothe same thing in WPF it tells me:
    Input string was not in a correct format.
    what shouuld i do?

    Why did you mark your own question as answer? Please unmark it.
    >>if in C# it worked why wouldn't it work in wpf?
    As I have already mentioned, it is because of the actual value of arr[i]. It has nothing to do with Windows Forms or WPF but it is possible that your WPF application has put a different value in the array than your Windows Forms Application did. And if this
    specific value is not a valid number the parsing will always fail. So you should check the values that put into the array and probably use the int.TryParse method to do the parsing:
    int sum;
    if (!int.TryParse(arr[i], out sum))
    //the value of arr[i] was NOT a valid number...
    This will however not change the fact that for example the value "D1" cannot be converted to an int using the int.Parse or int.TryParse method.
    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

  • Conversion of String to Int

    Had some trouble with passing a string input via the command screen and changing it to an int. Im prob using the wrong code but im not sure if that is it.
    import javax.swing.JFrame;
    import java.util.Scanner;
    public class P524Driver
    public static void main ( String args[] )
         Scanner input = new Scanner(System.in);
         // Input the rows of the Diamond
         System.out.print ("Input the number of rows (Must be and oddnumber): ");
    int x = input.nextInt();
         String z;
         if (x > 0) //     Determine if the value entered was valid
         while ( x % 2 == 0 )
              System.out.print( "You Input a even number try again: ");
              x = input.nextInt();
              while ( x % 2 != 0)
              System.out.print( "Input the charater: ");
              z = input.next();
              int y = Integer.parseInt(z);
    {else{ System.exit(0);}
    }

    Ok I don't know if this is going to work with your input, but here goes. The way I declared user input was as so : BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); and the way I declared the integer value of the input was as so : int value = Integer.parseInt(console.readLine());Hope this helps!

  • Conversion from string to Date Query.

    Hi,
    I need a way to convert a date format which is in string to a date data type.
    String is in the following format : Tue Feb 27 16:00:31 PST 2007
    Have used the SimpleDateFormat class but it returns an unparseble data error.
    Any thoughts????
    Regards
    Sam W

    Please don't crosspost: http://forum.java.sun.com/thread.jspa?threadID=5142482

  • Run time error message - Conversion from string "Mobile Telecommunications" to type 'Boolean' is not valid.

    I get a error message when I select an element in the combo box, please review my variables:
     Public Sector As String
        Public Index As Decimal
    ...and code
     Select Case ComboBox1.Text
                Case "Banks"
                    Sector = "Bank" And Index = -0.086
                Case "Mobile Telecommunications"
                    Sector = "Mobile Telecommunications" And Index = -0.024
                Case "Real Estate Investment Trusts"
                    Sector = "Real Estate Investment Trusts" And Index = 0.132
            End Select

    And is a boolean operator. Try it like this instead:
    Select Case ComboBox1.Text
    Case "Banks"
    Sector = "Bank"
    Index = -0.086
    Case "Mobile Telecommunications"
    Sector = "Mobile Telecommunications"
    Index = -0.024
    Case "Real Estate Investment Trusts"
    Sector = "Real Estate Investment Trusts"
    Index = 0.132
    End Select
    Still lost in code, just at a little higher level.

  • Char array conversion from String: toCharArray()

    Greetings,
    Can anyone tell me why this code:
    import com.wuw.debug.Trace;
    public
    class charTest
       public static void
       main( String[] args )
           String strIn = new String( "strIn" );
           Trace.DTRACE( "strIn: "+strIn );
           Trace.DTRACE( "strOut: "+strIn.toCharArray() );
    }produces this output:
    [DTRACE]: strIn: strIn
    [DTRACE]: strOut: [C@1fef6f
    and not:
    [DTRACE]: strIn: strIn
    [DTRACE]: strOut: strIn

    Because:
    String.toCharArray returns an array of chars.
    An array is basically an object in java.
    Objects are converted to strings with the method toString - if it's not implemented in your class the string that method returns will be of the form classname@hashcode.
    In the case of a char array, the name of the class is "[C". The hashcode of you object seems to be "1fef6f" (in hex).
    You'll just have to remember that an array of chars is [i]not a string in java.

  • Conversion from string to date

    hi,
    Can someone tell me how can i convert a string "09/09/02" to date ......

    HaHaHa jboeing is Sooo instructional. Yes, please to read instructions, bang head for four hours and then do this -import java.util.*;
    import java.text.*;
    public class DateTest
       public static void main(String[] args)
          String dateStr = "09/09/02";
          Date date = new Date();
          SimpleDateFormat sDF = new SimpleDateFormat("MM/dd/yy");
          try{
             date = sDF.parse(dateStr, new ParsePosition(0));
          }catch(NullPointerException npe)
          {System.out.println("bad parse");}
          java.sql.Date sqlDate = new java.sql.Date(date.getTime());
          System.out.println(date.toString());
          System.out.println(sqlDate.toString());
    }

Maybe you are looking for