1 2 Previous Next 19 Replies Latest reply: Jun 7, 2007 2:57 PM by 807606 RSS

    simple question I think, int -> string

    807606
      hey I want to declare an objects name but the characters have to be numbers from a loop that changes i.e.

      for (int x=0;x<=99;x++){
                     for (int y=0;y<=99;y++){
                          new cell ""xy"" = (x,y,0);
      }
      }

      i want the 'cells' to be named 01, 02 etc. and have those numbers as shown.
      Can anyone help please?
        • 1. Re: simple question I think, int -> string
          807606
          You can't. variable names must begin with a character or an underscore.

          PS.
          • 2. Re: simple question I think, int -> string
            807606
            hey I want to declare an objects name but the
            characters have to be numbers from a loop that
            changes
            the 'cells' to be named 01, 02 etc. and have those
            numbers as shown.
            an anyone help please?
            Brief Question: Why?

            Longer Statement: This smells of bad code design. If we get to fully understand exactly what it is you are trying to acheive, we may be able to help you find a better way such as arrays or collections or OOP design. Please understand that the quality of the information you give us will have direct bearing on the quality of the answers you receive back.

            (Also, if you post code, please use code tags)

            /P
            • 3. Re: simple question I think, int -> string
              807606
              excuse my ignorance but do you mean that I cant have the quotation marks or that I cant ever declare x and y as names of an object?
              • 4. Re: simple question I think, int -> string
                807606
                excuse my ignorance but do you mean that I cant have
                the quotation marks or that I cant ever declare x and
                y as names of an object?
                well, you've got a java compiler, try it.
                • 5. Re: simple question I think, int -> string
                  791266
                  excuse my ignorance but do you mean that I cant have
                  the quotation marks or that I cant ever declare x and
                  y as names of an object?
                  I'm not sure that I understand what you mean, but I think that you are wrong on both cases :)
                  • 6. Re: simple question I think, int -> string
                    791266
                    What would this:

                    new cell ""xy"" = (x,y,0);

                    do?

                    I don't get it.
                    • 7. Re: simple question I think, int -> string
                      807606
                      Take a look at this, you can't name objects like that without using reflection which may be out of your range for now. You can however add Cells to a map and store them with a key which can be used as a name/reference to them. Take a look at this
                      import java.util.HashMap;
                      import java.util.Map;
                      
                      
                      public class CellHolder {
                           
                           public static void main(String[] args) {
                                Map<String, Cell> map = new HashMap<String, Cell>(100);
                                
                                for (int i = 0; i < 99; i++) {
                                     //String is the key which can be used to reference the value in the map
                                     map.put(String.valueOf(i), new Cell(i,i,i));
                                }
                      
                                map.get("1"); //Get Object with key of "1"
                           }
                           
                      }
                      
                      
                      class Cell {
                           
                           private int x;
                           private int y;
                           private int i;
                           public Cell (int x, int y, int i) {
                                this.i=i;
                                this.x=x;
                                this.y=y;
                           }
                      }
                      • 8. Re: simple question I think, int -> string
                        807606
                        How about something like... (having no idea what a "Cell" is...)
                        Cell[][] cells = new Cell[99][99];
                        for (int x=0;x<=99;x++){
                          for (int y=0;y<=99;y++){
                            cells[x][y] = new Cell(x,y,0);
                          }
                        }
                        • 9. Re: simple question I think, int -> string
                          807606
                          What would this:

                          new cell ""xy"" = (x,y,0);

                          do?
                          I know what it will do. It will cause a "syntax error" if you try to compile it. That's what it will do.
                          • 10. Re: simple question I think, int -> string
                            807606
                            x and y sure 1 and 2 no. The api provides a means that much more graceful involving the Collection, List and Map interfaces.

                            Let's take a fairly real world example, a spreadsheet. A spreadsheet contains cells that are identified by their row and column coordinates.
                            A1, C3 ... (You sunk my battleship!) What you're trying to do is name them 11, 33 etc ... But how would the system be able to tell the difference between Col 1 Row 11 and Col 11 Row 1 (Both would be 111) in your model. But that said, if you were to look inside the spreadsheet you might find a collection called cells or something similar that contains cell objects that each among other things contain the coordinates (row and column) where the cell is found.

                            Do you see what we're driving at?

                            PS.
                            • 11. Re: simple question I think, int -> string
                              791266
                              What would this:

                              new cell ""xy"" = (x,y,0);

                              do?
                              I know what it will do. It will cause a "syntax
                              error" if you try to compile it. That's what it will
                              do.
                              :)

                              I still want to know what the OP expected out of that.
                              • 12. Re: simple question I think, int -> string
                                807606
                                Just so you understand how to convert numerical values to String then take a look at this:
                                                 int x = 1234;
                                          String s = String.valueOf(x); //s = "1234"
                                As others pointed out you can't name variables with just numbers.
                                • 13. Re: simple question I think, int -> string
                                  791266
                                  Take a look at this, you can't name objects like that
                                  without using reflection
                                  I don't know what you mean, but I can't think of a way to do that using reflection.
                                  • 14. Re: simple question I think, int -> string
                                    807606
                                    Take a look at this, you can't name objects like
                                    that
                                    without using reflection
                                    I don't know what you mean, but I can't think of a
                                    way to do that using reflection.
                                    Yes, you're right, you can't assign a type name with reflection, only dynamically assign a type.

                                    :)

                                    Wait a min, can't you do both proving you have legal names?

                                    Message was edited by:
                                    helloWorld
                                    1 2 Previous Next