Reputation: 15
I'm trying to use a for loop to enter characters a-z into a string array, but I'm not having much luck converting characters to string values so they'll actually go into the string array. I keep getting null values as my output. Could anyone provide some tips on how to get characters into a string array? This is what I have so far:
String[] letters = new String[26];
for (char ch = 'a'; ch <= 'z'; ch++)
{
int i = 0;
letters[i] = String.valueOf(ch);
i++;
}
System.out.println(Arrays.toString(letters));
Upvotes: 1
Views: 3005
Reputation: 608
For your particular purpose, with Java 8 Streams, you don't even need a loop.
String[] letters = IntStream.rangeClosed('a', 'z').mapToObj(i -> Character.toString((char)i)).toArray(String[]::new);
System.out.println(Arrays.toString(letters));
To break it down:
IntStream.rangeClosed(int, int)
makes a Stream
of int
s from the first int
to the second, inclusive of both endpoints. We use this because there is no CharStream class (for some reason), but we can still use char
s 'a'
and 'z'
, which will be implicitly converted to their int
value.mapToObj
takes a function which will convert each int
of the Stream
into an object. It gets a little messy here, as there is no single step conversion from int
to String
, we first need the int
interpreted as a character value. So, we cast each int
(named i
) to a char
, and then wrap that in a conversion from char
to String
: i -> Character.toString((char)i)
. This will leave us with a Stream<String>
.String[]
, as per your question. Stream
has a toArray
method, but this will give us an annoying Object[]
result. Instead, we will supply the method we want to have used to build the array. We don't want anything fancy, so we'll just use the standard initializer for a String
array: toArray(String[]::new)
.After that, letters
will be equal to an array of String
s, and each one will successively be a letter from a
to z
.
If you don't have access to Java 8 or simply don't like the above solution, here's a simplified version of your above code that removes the need for the index:
String[] letters = new String[26];
for (char c = 'a'; c <= 'z'; c++) letters[c - 'a'] = Character.toString(c);
System.out.println(Arrays.toString(letters));
In Java, char
s can be treated as int
s because below the surface, they are both stored as numbers.
Upvotes: 0
Reputation: 54148
for (char ch = 'a'; ch <= 'z'; ch++){
int i = 0;
letters[i] = String.valueOf(ch);
i++;
}
You need to understand what it does : at EACH iteration you're initializing the variable i
to 0
so you will never write in an other place than letters[0]
You need only to set it to 0, and then increment it, so just put the instruction before the loop
An other easy way would be only :
char[] letters = "abcdefghijklmnopqrstuvwxyz".toCharArray();
Upvotes: 0
Reputation: 128
your inserting each time to letters[0], keep Variable i outside the loop.
String[] letters = new String[26];
int i = 0;
for (char ch = 'a'; ch <= 'z'; ch++)
{
letters[i++] = String.valueOf(ch);
}
System.out.println(Arrays.toString(letters));
Upvotes: 0
Reputation: 809
Move int i = 0;
outside the loop like Eran said or just don't use another counter but determine index by ordinal character representation:
String[] letters = new String[26];
for (char ch = 'a'; ch <= 'z'; ch++) {
int index = (int) ch - 97;
letters[index] = String.valueOf(ch);
}
System.out.println(Arrays.toString(letters));
Additionally, you don't have to use another local variable and could just do letters[(int) ch - 97] = ...
of course.
Upvotes: 0
Reputation: 446
String[] letters = new String[26];
int i = 0;
for (char ch = 'a'; ch <= 'z'; ch++)
{
letters[i] = String.valueOf(ch);
i++;
}
System.out.println(Arrays.toString(letters));
Try this. i=0 should be outside the loop.
Upvotes: 5