Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Array listing problem

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array listing problem

    Hello!
    I'm a beginner in programming, I tried to create a program.. an array and fill it with the ABC, I think it should be OK, and after that I try to list it, but it doesn't works well, I can only see: "Z Z Z Z Z"
    What could be the problem?

    public class NewClass55 {
        public static void main(String[] args) {
     
        int abcd=0;
        for (char k = 'A'; k <= 'Z'; k++) {
            abcd++;
        }
     
        char[] array = new char[abcd];
     
          for (int j = 0; j < abcd; j++) {
                    for (char i = 'A'; i <= 'Z'; i++) {  
                        array[j] = i;
                    }
              }
     
        for (int j = 0; j < abcd; j++) {
                    System.out.println(array[j]);
              }
     
      }
    }

    Thanks in advance!
    Last edited by helloworld922; January 17th, 2011 at 05:05 AM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Array listing problem

    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:
    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]);
            }
        }
    }
    Last edited by newbie; January 15th, 2011 at 02:38 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Array listing problem

    Sorry could you clarify what your specifications are?

    also
            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
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:
        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:
        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 View Post
    Sorry could you clarify what your specifications are?

    also
            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
    Last edited by helloworld922; January 17th, 2011 at 05:06 AM.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Array listing problem

    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.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. The Following User Says Thank You to newbie For This Useful Post:

    saito1234 (January 15th, 2011)

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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();

  9. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array listing problem

    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" ?

  10. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array listing problem

    Thanks for the help, I finished it today, I think it is good!

    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]+", ");
     
          }   
        }
    }

Similar Threads

  1. Listing files in a folder in a .jar
    By kulan8 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 22nd, 2010, 08:18 AM
  2. array problem
    By aizen92 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 18th, 2010, 11:06 AM
  3. Problem with array
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 27th, 2010, 12:17 PM
  4. Listing the alphabet in lower and uppercase
    By Nemphiz in forum Object Oriented Programming
    Replies: 2
    Last Post: May 25th, 2010, 05:25 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM

Tags for this Thread