Results 1 to 4 of 4
  1. #1
    erin.ctm is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default How to replace int with asterisk

    Hi there!
    I am writing a multi-dim array and was asked to store an asterisk (*) in every position in the table, and prints the table. What method do I use to replace the positions with an asterisk?

    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Make sure it's an array of String or of char, and then use nested for loops, one for each dimension, to loop through the array setting each element to an asterisk. By the way, you won't be "replacing" an int with an asterisk because this can't be an int array if it will hold non-numeric data.

  3. #3
    erin.ctm is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    Make sure it's an array of String or of char, and then use nested for loops, one for each dimension, to loop through the array setting each element to an asterisk. By the way, you won't be "replacing" an int with an asterisk because this can't be an int array if it will hold non-numeric data.

    Gotcha on the int part :-). It is a char (duh). The part I'm lost on is how to "loop through the array setting each element to an asterisk"- actually changing to the asterisk.

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by erin.ctm View Post
    Gotcha on the int part :-). It is a char (duh). The part I'm lost on is how to "loop through the array setting each element to an asterisk"- actually changing to the asterisk.
    Again you will likely want to use nested for loops. So if you have a 1 dimensional array, you'd use one for loop, if a 2-dimensional array, then two loops, one nested in the other, if a 3-dimensional array, then 3 loops each nested in the other, etc...

    For instance if you had a 3-Dimensional array of char you'd loop through it like so:

    Java Code:
          char[][][] myChars = new char[3][2][7];
          
          for (int i = 0; i < myChars.length; i++) {
             for (int j = 0; j < myChars[i].length; j++) {
                for (int k = 0; k < myChars[i][j].length; k++) {
                   // set your array item here
                }
             }
          }

Similar Threads

  1. Using an asterisk to split a string.
    By Cruncher in forum New To Java
    Replies: 2
    Last Post: 06-13-2010, 04:12 PM
  2. Create an Asterisk Pattern in Java
    By kaurpower in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 02:00 AM
  3. [SOLVED] Help Me with Printing Asterisk Triangular
    By AFstudent in forum New To Java
    Replies: 20
    Last Post: 11-12-2008, 04:59 PM
  4. asterisk java
    By abhi in forum New To Java
    Replies: 4
    Last Post: 01-22-2008, 08:09 AM
  5. Asterisk-Java 0.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-02-2007, 09:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •