This site requires JavaScript, please enable it in your browser!
Greenfoot back
askgriff
askgriff wrote ...

2013/10/30

Reading one number from a string (array?)

askgriff askgriff

2013/10/30

#
I would like to have a string variable -- for example: line1 = "0020333334561111111234555500000"; line2 = "A00023454B2345266C23546734D267"; I want to be able to read the 12th character in (in the examples it would be 6 or 3) and do something with that character. A data table of sorts. I know I can do it with commas, but I would like to pack some code together just because. LOL Ideas?
JetLennit JetLennit

2013/10/30

#
Try something like
char let = line1.charAt(11)
int num = (int) let.toInt();
askgriff askgriff

2013/10/31

#
Awesome. So if I give a command like: char A01 = row1.charAt(1); How do I use the information? Something like: if (A01 = "1") {addObject(new Object(), X, Y);} I get "incompatible types" with that, but I'm sure it's something I'm setting up incorrectly.
danpost danpost

2013/10/31

#
A01 is of char type and "1" is of String type. You cannot compare the two. Also, a single equals sign is used to assign values, not to compare values (like '=='). What you want is
if (A01 == '1')
askgriff askgriff

2013/10/31

#
So is it possible to do an "if" statement using a char? For example:
if (A01 == 1) {addObject(new Object(), X, Y);}
Or do I have to convert the char to a string some how?
askgriff askgriff

2013/10/31

#
I read another thread suggesting something like this:
A00 = row1.charAt(1) - '0';
bourne bourne

2013/10/31

#
chars are kinda interchangeable with ints. Each character (google ascii table) has a designated value i.e. '1' != 1 The char '0' has value 48, and the proceeding digits continue from there, so subtracting '0' from a digit char will give the numerical value of the digit
askgriff askgriff

2013/10/31

#
OH! Duh. Thank you. Wow... LOL Of course.
You need to login to post a reply.