7 Replies Latest reply: Dec 30, 2003 11:34 AM by 807582 RSS


      how can I convert the int (wich i get by this method) to a string?
      //is there a function that doeas that in the java.lang or do I have to create one?

      String file="something.txt";
                File inputFile = new File(file);
           FileReader in = new FileReader(inputFile);
           int c;
           while ((c = in.read()) != -1)
      //HERE: text+=int2String(c);
                System.out.println("data loaded:\n"+text);
           }catch(IOException e){
        • 1. Re: int2String
          BufferedReader br = new BufferedReader(in);
          // now use br.readLine and so forth to read the contents as strings.
          • 2. Re: int2String
            why do it that way?
            BufferedReader in = new BufferedReader(new FileReader(new File("myFile.txt")));
            String inputLine;
            while (inputLine = in.readLine() != -1){
                // inputLine is a string..so you can do whatever you want with it here..no need for the conversion
            if you want to convert an int to a string..
            String s = "" + c;

            • 3. Re: int2String
              • 4. Re: int2String
                if the file is a text file the int that u r reading needs to be coverted to char because every char has an int value and that value is what u r reading.
                //this line convertes the int c to a char
                char charValueOfInt=(char)c;
                if u want to append it to a string u can use this line
                public String int2String(int c)
                    //this line convertes the int c to a char
                    char charValueOfInt=(char)c;
                    //return the string value of the int
                    return new String(charValueOfInt);
                BUT i recommend u use StringBuffer and not string because of performence so instead of
                String text=new String();
                //some loop
                u should use
                StringBuffer text=new StringBuffer ();
                //some loop
                //append the char
                • 5. Re: int2String
                  this is what I get as a result of your code, can anyone help me here?
                  D:\oracle\1111>javac caja001.java
                  caja001.java:78: incompatible types
                  found : java.lang.String
                  required: boolean
                  while (inputLine = in.readLine() != -1){
                  caja001.java:78: operator != cannot be applied to java.lang.String,int
                  while (inputLine = in.readLine() != -1){
                  2 errors

                  • 6. Re: int2String
                         public String int2String(int c){
                         char charValueOfInt=(char)c;
                         String a=String.valueOf(charValueOfInt);
                    //this one is the real working function. thank you all.
                    • 7. Re: int2String
                      oops..i forgot the enclosed parenthesis

                      while ((inputLine = in.readLine()) != null)

                      NOT while (inputLine = in.readLine() != null)