Strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • majik1213
    New Member
    • Sep 2007
    • 4

    #1

    Strings

    could someone please help me figure out why the console will display only 0 but never 455555?

    Code:
    public class Test
    {
        public static void main(String[] args)
        {
            String testString = "0";
    if (testString.equals("0"))
        {
            String okay=testString.replace("0", "455555");
         }   
            int test = new Integer("+okay+");
                     System.out.print("test");
        }
    }
    also, when I run the program, I get the following error (no compiler error, interestingly enough)

    java.lang.Numbe rFormatExceptio n: For input string: "+okay+"
    at java.lang.Numbe rFormatExceptio n.forInputStrin g(NumberFormatE xception.java:4 8)
    at java.lang.Integ er.parseInt(Int eger.java:447)
    at java.lang.Integ er.<init>(Integ er.java:620)
    at Test.main(Test. java:10)
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by majik1213
    could someone please help me figure out why the console will display only 0 but never 455555?

    Code:
    public class Test
    {
        public static void main(String[] args)
        {
            String testString = "0";
    if (testString.equals("0"))
        {
            testString.replace("0", "455555");
         }   
            int test = new Integer(testString);
                     System.out.print(""+test+"");
        }
    }
    Strings are immutable (read all about it). You cannot change their value. You can change the value being pointed to by a String variable however so you'd have to do
    [CODE=java] testString = testString.repl ace("0", "455555");[/CODE]
    to see the results you want

    Comment

    • majik1213
      New Member
      • Sep 2007
      • 4

      #3
      Originally posted by r035198x
      Strings are immutable (read all about it). You cannot change their value. You can change the value being pointed to by a String variable however so you'd have to do
      [CODE=java] testString = testString.repl ace("0", "455555");[/CODE]
      to see the results you want

      you are a saint, my friend

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by majik1213
        you are a saint, my friend
        I know a few people who would doubt that ...

        Comment

        • kreagan
          New Member
          • Aug 2007
          • 153

          #5
          Originally posted by majik1213
          could someone please help me figure out why the console will display only 0 but never 455555?

          Code:
           
                  int test = new Integer("+okay+");
                           System.out.print("test");
          also, when I run the program, I get the following error (no compiler error, interestingly enough)

          java.lang.Numbe rFormatExceptio n: For input string: "+okay+"
          at java.lang.Numbe rFormatExceptio n.forInputStrin g(NumberFormatE xception.java:4 8)
          at java.lang.Integ er.parseInt(Int eger.java:447)
          at java.lang.Integ er.<init>(Integ er.java:620)
          at Test.main(Test. java:10)
          You don't get a compiler error because the Integer constructor also takes Strings along with ints. The NumberFormatExc eption occurrs because "+okay+" is not an int.

          int test = New Integer( "1234556"); is valid.

          Comment

          Working...