help with char Array..must put letters in the array (instead of numbers)
im supposed to make a small program where i have to put letters (in alphabetical order in a char array) i kno how to put numbers
(i can put 1-5 in an array) in an array..but i dont know how to do it for characters...i post code..any comments would me most appreciated..
publicstaticvoid initialize (char [] list){
for (int i = 0; i < list.length; i++){
list[i] += 65;
}
}
if this was an int array list = i +1 (if i was supposed to put numbers in the array) but since this is chars i really dont know how to do this..i tried to put unicode (list [ i ] += 65..("A" for those who dont know) but it doesnt work..im getting question marks, and other weird symbols..
i tried this in a new program without the use of methods..
public class PuttingLettersinCharArray {
public static void main (String [] args) {
char [] list = new char [5];
for (int i = 0; i < list.length; i++) {
list[i] += 97 + i;
System.out.print(list[i]);
}
System.out.println();
}
}
this one seems to work but i still dont get why my original code doesn't work
@Op. What do you mean? Your original code works.
public class Test {
public static void main(String[] arguments) {
char[] data = new char[20];
initialize(data);
System.out.println(new String(data));
}
public static void initialize(char[] list) {
for (int i = 0; i < list.length; i++) {
list[i] += 65;
}
}
}
Prints:
AAAAAAAAAAAAAAAAAAAA
Kaj
Just change the line:list[i] += 65;to list[i] += 65 + i;If you want the alphabet.Kaj
u mean like this..
public static void initialize (char [] list) {
for (int i = 0; i < list.length; i++) {
list[i] += 65 + i;
}
}
i tried it but its outputting this..(ignore square brackets)
[?] [?] [ [╙ [猐 [竇
this is a method and im supposed to output A, B, C etc..i have another program that i run off to make this work..
Message was edited by:
moonmaster
> u mean like this..
>
> public static void initialize (char [] list) {
>for (int i = 0; i < list.length; i++) {
>list[i] += 65 + i;
> }
> }
>
> i tried it but its outputting this..(ignore square
> brackets)
>
> [?] [?] [ [╙ [猐 [竇
>
> this is a method and im supposed to output A, B, C
> etc..i have another program that i run off to make
> this work..
>
> Message was edited by:
> moonmaster
I don't know what you are doing, this.
public class Test {
public static void main(String[] arguments) {
char[] data = new char[26];
initialize(data);
System.out.println(new String(data));
}
public static void initialize(char[] list) {
for (int i = 0; i < list.length; i++) {
list[i] += 65 + i;
}
}
}
Prints:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
(Using 'A' instead of 65 makes it easier to understand, so I think you should change your code)
Kaj
i dont think u understand what im supposed to do...okay i need to make a METHOD called initialize that works for an array of chars..This initialize method should fill the array with characters A, B, C etc..Do u know what method overloading is? This is it..i'll show you a sample METHOD..(there's a reason why i put method in capitals)
public static void initialize (int [] list) {
for (int i = 0; i < list.length; i++) {
list[i] = i+1;
}
}
This is a method where i have an array of ints as a parameter and it prints out 1, 2, 3 etc.the difference is i need to make a METHOD that prints out A, B, C etc but it doesnt work..i hope u see my problem..
so in the example..i have another program that has the following lines
int[] test1 = new int[8];
Utilities.initialize(test1);
Utilities.printArray(test1);
printLine();
so this runs and reads off my program file, giving me my output (of course there's another method for printArray that prints it out..) do u understand..
Message was edited by:
moonmaster
What not to understand? you have been given the answer, or at least enough code to find the answer. Well, OK, maybe you are slow, or lazy, but here is the code I believe you are looking for, it is Christmas, after all!
public class Test{
public static void main(String[] args) throws Exception {
int[] intArr = new int[10];
char[] charArr = new char[26];
initialize(intArr);
printArray(intArr);
initialize(charArr);
printArray(charArr);
}
private static void initialize(int [] list)
{
for(int i = 0; i < list.length; i++)
{
list[i] = i;
}
}
private static void initialize(char[] list)
{
for(int i = 0; i < list.length; i++)
{
list[i] = (char)('A' + i);
}
}
private static void printArray(int[] list)
{
for(int i = 0; i < list.length; i++)
{
System.out.print(list[i]);
}
System.out.println();
}
private static void printArray(char[] list)
{
for(int i = 0; i < list.length; i++)
{
System.out.print(String.valueOf(list[i]));
}
System.out.println();
}
}
~Tim
MERRY CHRISTMAS EVERYONE!!
public static char[] initialize () {
char[] list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
return list;
}
unfortunately..this is not what im looking for..thx anyway guys..i'll guess i 'll keep trying..
then you need to explain what it is you are trying to do a whole lot better. I have given you a method tha initializes an int array with the number 0 thru 9, and a method that prints out that array.
I also included another method initialize that accepts a char [] (thus overloading the initialize method) and a last method that prints out the char array. (overloading the printArray(int[]) method)
~Tim
> unfortunately..this is not what im looking for..thx> anyway guys..i'll guess i 'll keep trying..You are in that case really bad in explaining what you want.
> You are in that case really bad in explaining what> you want.It might help if he explained it in capital letters, like 'METHOD.'
Maybe he meant that the METHOD's were supposed to be in another class. If thats the case and he couldn't take my code and use it than he needs to find a new career path.
//MY TESTING CLASS
public class Test{
public static void main(String[] args) throws Exception {
int[] intArr = new int[10];
char[] charArr = new char[26];
Utilities.initialize(intArr);
Utilities.printArray(intArr);
Utilities.initialize(charArr);
Utilities.printArray(charArr);
}
}
//MY UTILITIES CLASS
public class Utilities {
public static void initialize(int [] list)
{
for(int i = 0; i < list.length; i++)
{
list[i] = i;
}
}
static void initialize(char[] list)
{
for(int i = 0; i < list.length; i++)
{
list[i] = (char)('A' + i);
}
}
static void printArray(int[] list)
{
for(int i = 0; i < list.length; i++)
{
System.out.print(list[i]);
}
System.out.println();
}
static void printArray(char[] list)
{
for(int i = 0; i < list.length; i++)
{
System.out.print(String.valueOf(list[i]));
}
System.out.println();
}
}
~Tim
> public static void initialize (char [] list) {
>for (int i = 0; i < list.length; i++) {
>list[i] += 65 + i;
> }
> }
What is your input? If your array does not contain only empty values (0, the default value of a char), this won't give the alphabet.
Use the following, as Tim was using:
list[i] = (char) ('A' + i);
Don't forget that list += 65 + i means that you are adding the number 65 + i to the numberin list and put the result in list!
If you call this method where list is an array with random numbers your output will be random numbers....
If you call the method with a list that is all zeroes your output is the expected result, although you didn't see the error. What you really meant was
list[i] = 65 + i
or easier to read for the average user
list[i] = 'A' + i