Fill the code to convert int to String and then String to int.Code : int x = 9803; //Convert int to String String str1 = ; System.out.println(str1); String str2 = "1894"; //Convert string str2 to int int z = ; System.out.println(z); System.out.println(str);
Question
Fill the code to convert int to String and then String to int.Code : int x = 9803; //Convert int to String String str1 = ; System.out.println(str1); String str2 = "1894"; //Convert string str2 to int int z = ; System.out.println(z); System.out.println(str);
Solution
Here is the completed code:
int x = 9803;
//Convert int to String
String str1 = Integer.toString(x);
System.out.println(str1);
String str2 = "1894";
//Convert string str2 to int
int z = Integer.parseInt(str2);
System.out.println(z);
System.out.println(str1);
In this code, Integer.toString(x) is used to convert the integer x to a string, and Integer.parseInt(str2) is used to convert the string str2 to an integer.
Similar Questions
String note = "14 23.0 \n\nhello\n cs200";Scanner input = new Scanner(note);System.out.println("$" + input.next() + "$"); System.out.println("$" + input.nextLine() + "$"); System.out.println("$" + input.nextLine() + "$");
import java.io.*;import java.util.*;public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int Myint = scanner.nextInt(); double d = scanner.nextDouble(); scanner.nextLine(); String mystring = scanner.nextLine(); System.out.println(mystring); System.out.println(d); System.out.println(Myint); /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ }}
Reverse a string using StringBuffer.Fill the code snippet provided, to perform the above.Code : Scanner sc=new Scanner(System.in); String str=sc.next(); //Store str using StringBuffer StringBuffer sb = ; ; //reverse using sb String reverse = ; //Convert StringBuffer to String System.out.println(reverse);
the correct answerWhat will be the output of the following Java program? class mainclass { public static void main(String args[]) { char a = 'Z'; a++; System.out.print((int)a); } }Options9112290123
public class Hello{ public static void main(String args[]) { int num = 190; String rev_num = " " ; while (num!=0){ int digit = num%10; rev_num = rev_num+digit; num = num /= 10 ; } System.out.println((Integer.parseInt(rev_num)));} } Exception in thread "main" java.lang.NumberFormatException: For input string: " 091" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:654) at java.base/java.lang.Integer.parseInt(Integer.java:786)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.