-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArraytoString.java
62 lines (51 loc) · 1.52 KB
/
ArraytoString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//toString() cannot be used as char is in type of character array
//If it is integer,then toString() is used
//Cannot get direct char input
//only string input
//string to char and then to array and then to string
//using valueOf() method
/*
import java.util.*;
public class ArraytoString {
static String str1;
public static String ArrtoString(char[] a) {
str1 = String.valueOf(a);
return str1;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String to convert to character array :");
String str = sc.nextLine();
char[] ch = str.toCharArray();
System.out.print("The character array is : ");
for(int i=0;i<str.length();i++) {
System.out.print(ch[i]);
}
System.out.println();
System.out.print("The String is : ");
System.out.println(ArrtoString(ch));
}
}
*/
//using new String() method
import java.util.*;
public class ArraytoString {
static String str1;
public static String ArrtoString(char[] a) {
str1 = new String(a);
return str1;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String to convert to character array :");
String str = sc.nextLine();
char[] ch = str.toCharArray();
System.out.print("The character array is : ");
for(int i=0;i<str.length();i++) {
System.out.print(ch[i]);
}
System.out.println();
System.out.print("The String is : ");
System.out.println(ArrtoString(ch));
}
}