3 Replies Latest reply: Feb 26, 2009 1:17 PM by 843785 RSS

    converting strings to numbers

    843785
      import java.io.*;
      import java.util.*;
      
      class D{
           public static void main(String[] args)throws IOException{
           Scanner in = new Scanner (System.in);
           
           //51 3 15 * - 9 65 13 / - + example of expression to enter
           System.out.print("Enter PostFixExpression :");
           String s = in.next();//I am aware that I cannot read them as characters so I am using string
         
          while(!s.equals("")){
               if(String.isDigit(s)){//line 13
                    System.out.printf("%d",s);
               }else{
                    System.out.println("I am not a digit ");
               }
               s = in.next();
          }
         
                
           }//main
      }//class
      error: cannot find symbol method isDigit(java.lang.String) line 13
      when I use Character.isDigit(s), it separates the digits, for example 51 becomes a 5 and a 1. How can I manipulate the data so that the digits in the number stays together?

      --------------------------------------------------------------------------------
        • 1. Re: converting strings to numbers
          3004
          chetah wrote:
          error: cannot find symbol method isDigit(java.lang.String) line 13
          Right. As the error message tells you, String does not have a static isDigit method that takes a String arg.
          when I use Character.isDigit(s), it separates the digits, for example 51 becomes a 5 and a 1.
          No, that's not what isDigit does. It simply tells you whether a given character is a digit or not. You have to separate them yourself first.
          How can I manipulate the data so that the digits in the number stays together?
          Well, if you want to examine the individual characters to test whether they're digits, you'll have to break the String into chars. So look at String's methods to see if you can find one that will help you with that. Note that Strings are immutable, so you already know that your original String will remain untouched.
          • 3. Re: converting strings to numbers
            843785
            deleted, because of crosspost.