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 10 of 10

Thread: char [ ] issue

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

    Exclamation char [ ] issue

    Hi there, I have been working on a school project worth %15 of my grade, I am half done and it is due tomorrow... I am making a card/war game where 4 numbers are generated, they pick the number(s) of the array.
    i.e.


    value [rnumx] [rnumy];
    value [rnumx2] [rnumy2];

    Which ever number is bigger that player wins. Simple enough...

    The numbers pick 4 arrays, 2 value arrays, and 2 card array. (player 1 and player 2)
    cards [0] [0] = "Ace of Spades";
     
    value [0] [0] = 1;
    The problem I am having is I need to output the array, "cards [rnumx] [rnumy]" on the screen as a visual like this,
    char[] eg = {'E' 'G' };
    My teacher left me a paper that says I may have to do something with string manipulation, using code like,
    charAt (int index)
    length ()
    I have no idea how to use these, he never really told me how, I figure I need some for loop to loop getting the length with charAt, but I still, have no idea. If anyone could help, I would appreciate it.
    Thanks!
    -Pie


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: char [ ] issue

    I need to output the array, "cards [rnumx] [rnumy]" on the screen as a visual like this,
    I assume that cards is a 2 dimensional array of String.
    A single element of the array: cards [rnumx] [rnumy] is a String.

    I don't understand how the one dimensional char relates to a String. The String class has some methods for working with char arrays. Can you explain what you are trying to do and give an example?

    Have you read the API doc for the String class and its methods like charAt()?
    To learn what new classes and methods, do I often write a small testing program and write lines of code for many of the methods and print out the results to help understand what they do.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Piemeup (January 22nd, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: char [ ] issue

    Quote Originally Posted by Norm View Post
    I assume that cards is a 2 dimensional array of String.
    A single element of the array: cards [rnumx] [rnumy] is a String.

    I don't understand how the one dimensional char relates to a String. The String class has some methods for working with char arrays. Can you explain what you are trying to do and give an example?

    Have you read the API doc for the String class and its methods like charAt()?
    To learn what new classes and methods, do I often write a small testing program and write lines of code for many of the methods and print out the results to help understand what they do.
    Thanks for the reply Norm, yes cards is a multi dimensional array of 2, being, cards [3] [12]. I was using the char for things like this and it works fine.
    char[] n1 = {Character.forDigit (value [rnumx] [rnumy], 10) };

    That being value is an int.
    I am trying to use the char [] command to output cards [] [] with the rnumx and rnumy int's generating a random card. I want it so that the cards array, is printed out on the screen like I can with the code above, but that is with an integer.

    I have also read the API doc. They're still a little confusing because from my understanding I have to get the length () of the word, and have a for loop counting the amount of times or something like that with charAt.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: char [ ] issue

    I am trying to use the char [] command to output cards [] []
    cards[0][0] is a String and you can use any String class method with it:
     int len = card[0][0].length();

    printed out on the screen like I can with the code above
    Please give an example of the contents of a cards array element (a String)
    and what you want printed on the screen.
    Say the String was: "XYZ 123" what do you want printed on the screen.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Piemeup (January 22nd, 2013)

  7. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: char [ ] issue

    Quote Originally Posted by Norm View Post
    cards[0][0] is a String and you can use any String class method with it:
     int len = card[0][0].length();


    Please give an example of the contents of a cards array element (a String)
    and what you want printed on the screen.
    Say the String was: "XYZ 123" what do you want printed on the screen.
    There are 52 cards, so it would be,

    cards [0] [0] = "Ace of Spades";

    For an example.
    I am trying to get them printed with
    chars [] (Blah blah blah)
    then using,
    g.drawChars (etc, etc, etc, etc...)

    -Thanks

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: char [ ] issue

    Can you make a real example?
    I don't see how you get Blah, blah, blah from "Ace of Spades"?
    or etc,etc,etc

    For example: Given the String: "XYZ 123" what do you want to see on the screen?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: char [ ] issue

    Sorry, I hope this is better

    //Declaring cards//
                String[] [] cards = new String [4] [13];
                //spades
                cards [0] [0] = "Ace of Spades";
                cards [0] [1] = "Two of Spades";
                cards [0] [2] = "Three of Spades";
                cards [0] [3] = "Four of Spades";
                cards [0] [4] = "Five of Spades";
                cards [0] [5] = "Six of Spades";
                cards [0] [6] = "Seven of Spades";
                cards [0] [7] = "Eight of Spades";
                cards [0] [8] = "Nine of Spades";
                cards [0] [9] = "Ten of Spades";
                cards [0] [10] = "Jack of Spades";
                cards [0] [11] = "Queen of Spades";
                cards [0] [12] = "King of Spades";
                //There is more, just not showing them...

    Then, generating the values, and outputting them, This is how I want the String to output, except,

    char[] car1 = {cards [rnumx] [rnumy]};
    g.drawChars (car1, 0, car1.length, 50, 300);

    if (r1 != 1 && r2 != 0)
                    {
                        //Take 2 int, gen nums, run through value, put out in char []
     
                        rnumx = (int) (Math.random () * 3);
                        rnumy = (int) (Math.random () * 13);
     
                        rnumx2 = (int) (Math.random () * 3);
                        rnumy2 = (int) (Math.random () * 13);
     
                        pnum1 = value [rnumx] [rnumy];
                        pnum2 = value [rnumx2] [rnumy2];
     
                       //Having the numbers set up so they take away if needed so they can be put into the char []
     
                        if (rnumy > 8)
                        {
                            char[] n1 = {'1', Character.forDigit (rnumy - 9, 10) };
                            g.drawChars (n1, 0, n1.length, 50, 300);
                        }
     
                        else
                        {
                            char[] n1 = {Character.forDigit (value [rnumx] [rnumy], 10) };
                            g.drawChars (n1, 0, n1.length, 50, 300);
                        }
     
                        if (rnumy2 > 8)
                        {
                            char[] n2 = {'1', Character.forDigit (rnumy2 - 9, 10) };
                            g.drawChars (n2, 0, n2.length, 500, 300);
                        }
     
                        else
                        {
                            char[] n2 = {Character.forDigit (value [rnumx2] [rnumy2], 10) };
                            g.drawChars (n2, 0, n2.length, 500, 300);
                        }

    That's what a bit of my code is looking like at the moment, there is still more of course, but if it would be easier giving you the entire thing, just let me know, sorry if I am not good at explaining.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: char [ ] issue

    Why are you using drawChars() in place of drawString()?

    What happens when you compile this:
    char[] car1 = {cards [rnumx] [rnumy]};

    What is the purpose of all that code you posted?


    Why haven't you answered this question I've asked several times?

    For example: Given the String: "XYZ 123" what do you want to see on the screen?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: char [ ] issue

    I was never told about the use of drawString(), I only knew about drawChars().

    When I compile the code, it says "The type of the initializer, "java.lang.String", is not assignable to the variable, of type "char".

    The purpose of the code post was because I was not sure what you wanted, and to help show you what I am doing.

    I don't know what you want me to do with the "XYZ 123". Declare the {cards [XYZ] [123]}?
    If I change it to that it says, "No field named "XYZ" was found in type "visual".

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: char [ ] issue

    I thought this meant you wanted to print some thing on the screen:
    is printed out on the screen
    So I asked for an example of what would be seen on the screen when it is printed to the screen.

    I still don't know what you want to work with char values for. Why not use Strings?


    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  2. default Value of char
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 12th, 2011, 09:25 AM
  3. How to take a char value in scanner
    By andaji in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 02:50 AM
  4. [SOLVED] Char cannot be dereferenced
    By zukipuu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2010, 11:13 PM
  5. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 03:18 PM