Wednesday, July 20, 2016

Another way to convert an int to String

import java.util.Scanner;
public class IntToStrMethodsDemo {
    public static void main(String[] args) {
        int num;
        num=readInt();
        String str=intToStr(num);
        System.out.println(intToStr(num)+1);
    }
    public static int readInt(){
        System.out.println("enter an int");
        return new Scanner(System.in).nextInt();
    }
    public static String intToStr(int num){
        return ""+num;
    }

}
Or use the code below :

String str = ""+new Scanner(System.in).nextInt();
        System.out.println(str+1);


It does the same

2 comments:

  1. Too basic information.Please increase the standard of posts

    ReplyDelete
  2. why would you do that? this creates a StringBuilder to concatenate the strings.

    ReplyDelete