simple question I think, int -> string

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?

[345 byte] By [pimastera] at [2007-11-27 6:45:42]
# 1
You can't. variable names must begin with a character or an underscore. PS.
puckstopper31a at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 2

> 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

petes1234a at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 3
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?
pimastera at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 4
> 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.
petes1234a at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 5
> 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 :)
kajbja at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 6
What would this:new cell ""xy"" = (x,y,0);do?I don't get it.
kajbja at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 7

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;

}

}

_helloWorld_a at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 8

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

}

}

kevjavaa at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 9
> 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.
petes1234a at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 10

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.

puckstopper31a at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 11

> > 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.

kajbja at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 12

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.

_helloWorld_a at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 13
> 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.
kajbja at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 14

> > 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_

_helloWorld_a at 2007-7-12 18:17:45 > top of Java-index,Java Essentials,Java Programming...
# 15

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

I don't know what you mean, but you can't declare a reference using reflection (there no point in doing that), so you don't create variable names dynamically.

kajbja at 2007-7-21 22:05:04 > top of Java-index,Java Essentials,Java Programming...
# 16

ok, sorry to be the cause of this confusion I will restate:

what I intended to create was a 2D cartesian grid of 'cells' each of which had an additional integer variable (called state) so that each cell in the grid also had a value of state that I could alter later.

i.e. the cells had an x and y coordinate and a state. I also wished to assign each cell in the grid a name by its x-y coordinate so I could later say cell x-y has such and such a value of state. is that any clearer? if so any further suggestions? cheers dudes.

pimastera at 2007-7-21 22:05:04 > top of Java-index,Java Essentials,Java Programming...
# 17

> ok, sorry to be the cause of this confusion I will

> restate:

> what I intended to create was a 2D cartesian grid of

> 'cells' each of which had an additional integer

> variable (called state) so that each cell in the grid

> also had a value of state that I could alter later.

> i.e. the cells had an x and y coordinate and a state.

> I also wished to assign each cell in the grid a name

> by its x-y coordinate so I could later say cell x-y

> has such and such a value of state. is that any

> clearer? if so any further suggestions? cheers dudes.

Do you know how to create 2d arrays?

Cell[][] grid = new Cell[xSize][ySize];

//You can then write...

grid[x][y] = new Cell(arguments);

grid[x][y].getSomething();

Kaj

Ps. See reply #8 for another example.

kajbja at 2007-7-21 22:05:04 > top of Java-index,Java Essentials,Java Programming...
# 18

> ok, sorry to be the cause of this confusion I will

> restate:

> what I intended to create was a 2D cartesian grid of

> 'cells' each of which had an additional integer

> variable (called state) so that each cell in the grid

> also had a value of state that I could alter later.

> i.e. the cells had an x and y coordinate and a state.

> I also wished to assign each cell in the grid a name

> by its x-y coordinate so I could later say cell x-y

> has such and such a value of state. is that any

> clearer? if so any further suggestions? cheers dudes.

Why not use a 2-D array? You can have an array of whatever object your heart desires. Why try to do something incredibly complex (actually impossible) when an easy to implement solution is readily available?

petes1234a at 2007-7-21 22:05:04 > top of Java-index,Java Essentials,Java Programming...
# 19

Much clearer. Here's the first thought that leapt into my head ... Probably not a good one, but you never know.

public class Cell

{

private int xCoordinate = 0 ;

private int yCoordinate = 0 ;

private int state = 0 ;

public String getName(){

return (String.valueOf(xCoordinate) + ":" + String.valueOf(yCoordinate)) ;

}

}

What you have here is the very basic skeleton of a Class. Note that it does not have a name attribute and it's getName() composites from it's coordinates what its name should be. Why? What if you move it from one location to another? If this were an immutable class (which there's a good argument for by the way) you would create a Cell when information (or whatever) was placed inside it and it could not be altered as such but that's another topic.

Then I would create a Collection of these buggers to use as needed.

Just my 2 krupplenics worth your milage may of course vary.

PS.

puckstopper31a at 2007-7-21 22:05:04 > top of Java-index,Java Essentials,Java Programming...