Re: Array listing problem
Code java:
public class NewClass55 {
public static void main(String[] args) {
int abcd = 0;
char[] alphabetArray = null;
for (char k = 'A'; k <= 'Z'; k++, abcd++) {
}
alphabetArray = new char[abcd];
int index = 0;
for (char i = 'A'; i <= 'Z'; i++) {
alphabetArray[index] = i;
if (i == 'Z') {
System.out.print((alphabetArray[index]) + "\n");
} else {
System.out.print((alphabetArray[index]) + ", ");
}
}
}
}
Just a quick fix for you.
Will now print "A,B,C,...,Z" for you.
Some of the code can just be removed if you like, as its just formatting.
If you're after a very basic example:
Code java:
public class NewClass55 {
public static void main(String[] args) {
int abcd = 0;
char[] alphabetArray = new char[26];
for (char i = 'A'; i <= 'Z'; i++, abcd++) {
alphabetArray[abcd] = i;
System.out.println(alphabetArray[abcd]);
}
}
}
Re: Array listing problem
Thanks for your answer!
Well, my main problem is that I want to do some other things with this program in the second for loop, but for that I need to read in the data from the array!
(I'd like to count that how much from the different letters in the ABC are in any custom word, and display those letters, and their number count from which are more than 0)
And I think this will require at least 2 loops, but I'm stuck at this step:(
Re: Array listing problem
Sorry could you clarify what your specifications are? :P
also
Code java:
for (int i = 0; i < alphabetArray.length; i++) {
if (alphabetArray[i] == 'A') {
System.out.println("Hello World!");
}
}
Is that what you mean't by reading data from the Array? I.e inspecting it?
The first code I posted only stores @ one index, where as the second has an index for each char, which lets you inspect different values at different indexes.
Sorry If this post wasn't of any help, but I'm unclear as to what you need exactly :P
Re: Array listing problem
Thanks, it was helpful! But if you try this out with the upper code it doesn't work, it doesn't prints out "Hello World!" but in theory (at least I think) it should, that is what I don't understand!
Well, at the beginning I'd like to store a word in a String, for example:
Code Java:
String text="apple";
text=text.toUpperCase();
(convert to uppercase because lowercase/uppercase now doesn't matter)
after that store the letters from the word in an array:
Code Java:
int[] textArray = new int[text.length()];
for (int i = 0; i < text.length(); i++) {
textArray[i] = text.charAt(i);
}
and at the second for loop I'm thinking of comparing the alphabetArray with the textArray, and print those numbers to the screen which are found! Well the only problem left to count how much of them are...
For example currently: A:1, P:2, L:1, E:1
But the comparison doesn't works, because I can't inspect the array, that's why first I tried to print all the numbers in my second for loop, but that didn't work either, and for me the "Hello World!" test didn't work either (maybe I did something wrong?)
Quote:
Originally Posted by
newbie
Sorry could you clarify what your specifications are? :P
also
Code java:
for (int i = 0; i < alphabetArray.length; i++) {
if (alphabetArray[i] == 'A') {
System.out.println("Hello World!");
}
}
Is that what you mean't by reading data from the Array? I.e inspecting it?
The first code I posted only stores @ one index, where as the second has an index for each char, which lets you inspect different values at different indexes.
Sorry If this post wasn't of any help, but I'm unclear as to what you need exactly :P
Re: Array listing problem
Code java:
public class NewClass55 {
public static void main(String[] args) {
int abcd = 0;
char[] alphabetArray = new char[26];
for (char i = 'A'; i <= 'Z'; i++, abcd++) {
alphabetArray[abcd] = i;
System.out.println(alphabetArray[abcd]);
}
for (int i = 0; i < alphabetArray.length; i++) {
if (alphabetArray[i] == 'A') {
System.out.println("Hello World!");
}
}
String text = "apple";
String toUpperCase = text.toUpperCase();
char[] textArray = new char[text.length()];
for (int i = 0; i < text.length(); i++) {
textArray[i] = toUpperCase.charAt(i);
}
System.out.println(textArray);
for (int i = 0; i < alphabetArray.length; i++) {
for (int c = 0; c < textArray.length; c++) {
if (textArray[c] == alphabetArray[i]) {
System.out.println("Matched Something!" + textArray[c]);
}
}
}
}
}
Now don't take that as complete, but it should hopefully get you started.
Bit of a jumbled class, as I left earlier things in, so you might want to clear it up as well.
Re: Array listing problem
I don't think you can increment a char. A char could be lots of things.
Anyway, you could have a String that stores "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
then you have
char[] alphabet = thatString.toCharArray();
Re: Array listing problem
Code :
public class NewClass55 {
public static void main(String[] args) {
int abcd = 0;
char[] alphabetArray = new char[26];
int[] CountArray = new int[26];
for (char i = 'A'; i <= 'Z'; i++, abcd++) {
alphabetArray[abcd] = i;
CountArray[abcd] = 0;
}
String text = "apple";
String toUpperCase = text.toUpperCase();
char[] textArray = new char[text.length()];
for (int i = 0; i < text.length(); i++) {
textArray[i] = toUpperCase.charAt(i);
}
for (int i = 0; i < alphabetArray.length; i++) {
for (int c = 0; c < textArray.length; c++) {
if (textArray[c] == alphabetArray[i]) {
CountArray[i]++;
System.out.print(textArray[c] + ": "+CountArray[i]+", ");
}
}
}
}
}
It works like this, more or less... now there's only one problem:
Apple -> "A: 1, E: 1, L: 1, P: 1, P: 2"
It counts the P 2 times(in this case), is there any way to show this only: "A: 1, E: 1, L: 1, P: 2" ?
Re: Array listing problem
Thanks for the help, I finished it today, I think it is good!
Code :
public class NewClass55 {
public static void main(String[] args) {
int abcd = 0;
char[] alphabetArray = new char[26];
int[] CountArray = new int[26];
for (char i = 'A'; i <= 'Z'; i++, abcd++) {
alphabetArray[abcd] = i;
CountArray[abcd] = 0;
}
String text = "apple";
String toUpperCase = text.toUpperCase();
char[] textArray = new char[text.length()];
for (int i = 0; i < text.length(); i++) {
textArray[i] = toUpperCase.charAt(i);
}
for (int i = 0; i < alphabetArray.length; i++) {
for (int c = 0; c < textArray.length; c++) {
if (alphabetArray[i] == textArray[c]) {
CountArray[i]++;
}
}
}
for (int i = 0; i < alphabetArray.length; i++) {
if(CountArray[i]>0)
System.out.print(alphabetArray[i] + ": "+ CountArray[i]+", ");
}
}
}