0

How can i take input for an integer array in a single line without spaces. Number can be only from 0-9 so every thing is single digit.

Input:-

123456

Output:-

a[0]=1 a[1]=2 a[2]=3 a[3]=4 a[4]=5 a[5]=6

Please help me with this issue.

Adarsh
  • 827
  • 9
  • 23

5 Answers5

6

Assuming you are getting input as String you could do something like (streams and lambdas are available since Java 8)

String input = "123456";
int[] array = input.chars().map(i->i-'0').toArray();

System.out.println(Arrays.toString(array));

Output: [1, 2, 3, 4, 5, 6]

This code can be considered as shorter version of

String input = "123456";
int[] array = new int[input.length()];
int i = 0;
for (char c : input.toCharArray())
    array[i++] = c - '0';

Anyway this solution is based on idea that integer value or char represents its index in Unicode table. Since digits are ordered as 0,1,2,3,4,5,6,7,8,9 we are interested in differences between position of checked character and position of '0'. For example '3' is placed at index 51 while index of '0' is 48 so int value = '3'-'0' is translated as int value = 51 - 48 which creates integer with value 3.


Or if that is an option you could get char[] array instead of int[] which will contain all characters from string by simply using toCharArray

String input = "123456";
char[] array = input.toCharArray();

If you are also interested in result as String[] then since Java 8 you can simply use

String input = "123456";
String[] array = input.split("");

instead of cryptic (even for those with basic regex knowledge): split("(?!^)"), split("(?<!^)"), split("(?<=\\d)") or whatever regex you can think of.

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Yours also works but i need a simpler solution so i prefer the previous one no offense. :) thanks:) – Adarsh Sep 05 '14 at 15:59
  • @Audi What do you mean by simpler? IMHO `toCharArray` is most descriptive and simplest solutions. Also if you are interested in `String[]` result then as stated in my answer you can simply use `split("")` (this feature was added precisely to avoid regexes like `split("(?!^)")`). – Pshemo Sep 05 '14 at 16:10
2

Take in in as a String and use split() -method like this:

"123".split("(?!^)")

will produce an array of strings

array ["1", "2", "3"]

billerby
  • 1,977
  • 12
  • 22
0

If you assume your integers are all single-digit, you can do the following, but I don't see the interest of doing so :

 Scanner sc     = new Scanner(System.in);
 String  s      = sc.nextLine();
 int[]   values = new int[s.length()]; 
 for (int i=0 ; i<values.length ; i++) 
     values[i] = s.charAt(i) - '0';
Dici
  • 25,226
  • 7
  • 41
  • 82
0

One way to do this is to realize that while the Scanner class parses things using a separator by default, there is a method called findInLine() which "Attempts to find the next occurrence of the specified pattern ignoring delimiters."

You can create a Pattern which matches a single digit, and then ask the Scanner to parse through sans-separator like so:

Scanner scan = new Scanner("123456");
Pattern digit = Pattern.compile("\\d");
String nextInt = scan.findInLine(digit);
while (nextInt != null) {
    System.out.println(nextInt);
    nextInt = scan.findInLine(digit);
}


output:
1
2
3
4
5
6
azurefrog
  • 10,785
  • 7
  • 42
  • 56
0

try this

"123456".toCharArray()

Genjuro
  • 7,405
  • 7
  • 41
  • 61