1

The title is probably not accurate but I hope that reading this post you can understand what I want to do.

I'm kind stuck in here. New in Java ME, that unfortunately has, as you know, reduced methods than the Java SE.

What I want to accomplish is this: I have a txt, with numbers in it separated by space. I want to put them in an array that can ""behave as an usual array in c++"" when one gets numbers separated by space into an array.

Now in J2ME what I've done (using Netbeans) is: I took the txt to a stream. Later sent the stream to a byte array and finally i converted it to a char array.

Let's say the original txt was: 98 2 12 13

Part of the code is:

InputStream is = getClass().getResourceAsStream("models.txt");
try{


   int st_pk1_len = is.available();

   byte st_pk1[] = new byte[st_pk1_len];

   is.read(st_pk1);

   char st_pk1_char[] = new String(st_pk1).toCharArray();

   System.out.println(st_pk1_char);

What I get printed is: 98 2 12 13

Although, my problem is that when I want to access index 0 I get only the number 9 and not 98. If I try to reach the number 12, I put the pointer to 3 but what I get is an empty space and so on.

I've searched and tried different methods that I've found without luck to convert that into the original numbers again.

It could be a stupid mistake from my side or something that I've haven't think of.

Isn't there a simple solution to this problem?


update It's working now! Array is working as a "regular"c++ char array. In case somebody else need it or have my same problem, here is how it looks like:

InputStream is = getClass().getResourceAsStream("st_pk1.txt");
int st_pk1_len = is.available(); 
byte st_pk1[] = new byte[st_pk1_len]; 
is.read(st_pk1); 
char st_pk1_char[] = new String(st_pk1).toCharArray(); 
String PreSplitted = new String(st_pk1); 
String AftSplit[] = Split(PreSplitted, " "); 

If you want to check: System.out.println(AftSplit[n]);

For the split method I used the second link in the Gnat's post. Split text in J2ME

Community
  • 1
  • 1
user1338101
  • 71
  • 1
  • 3
  • 9
  • thanks. I tried but I get "cannot find symbol symbol: method getBytes() location: variable st_pk1_char of type char[]". I have the import java.io.* ; – user1338101 Apr 17 '12 at 08:03
  • ooops! sorry I edited instead of make a comment. some one suggested " byte[] ba = st_pk1_char.getBytes();". the post above is my answer to it. – user1338101 Apr 17 '12 at 08:18

1 Answers1

1

You can treat the char array as String, containing tokens separated by space: new String(st_pk1) does that for you.

After that, you need to split it, like as described in couple other Stack Overflow questions:

Community
  • 1
  • 1
gnat
  • 6,213
  • 108
  • 53
  • 73
  • Hi, Thanks. I'm trying that one. I copied the class of the second link u posted in the same java file. Later I added this part: new String(st_pk1); String Splitted = new Split(st_pk1, " "); but i get "constructor Split in class helloworld.Split cannot be applied to given types; required: no arguments found: byte[],java.lang.String reason: actual and formal argument lists differ in length". What I'm missing? – user1338101 Apr 17 '12 at 09:14
  • @user1338101 you're missing that new isn't needed here - it's a method call not object instance creation. Invocation syntax should look like `String splitted = Split(st_pk1, " ")` – gnat Apr 17 '12 at 09:28
  • thanks. I changed it to : String PreSplitted = new String(st_pk1); String AftSplit = Split(PreSplitted, " "); I got this error: incompatible types required: java.lang.String found: java.lang.String[]... how can I fix it? – user1338101 Apr 17 '12 at 09:42
  • @user1338101 incompatible type indeed, easy to fix: `String[] splitted`. Given you questions I wonder are you familiar with Java language basics? if not consider studying some tutorial for beginners; with this you'll be able to solve such issues faster, not having to wait for instructions at Internet – gnat Apr 17 '12 at 09:46
  • i'll consider reading more about the basics. not today at 5 am tho. yeah it's not good to wait for instructions at internet. you forgot the [] in the previous post. – user1338101 Apr 17 '12 at 10:03
  • 1
    Ok it's working now! Array is working as a "regular"c++ char array. In case somebody else need it or have my same problem, here is how it looks like: InputStream is = getClass().getResourceAsStream("st_pk1.txt"); int st_pk1_len = is.available(); byte st_pk1[] = new byte[st_pk1_len]; is.read(st_pk1); char st_pk1_char[] = new String(st_pk1).toCharArray(); String PreSplitted = new String(st_pk1); String AftSplit[] = Split(PreSplitted, " "); If you want to check: System.out.println(AftSplit[n]); for the split method I used the one posted in the second link posted by Gnat. – user1338101 Apr 17 '12 at 12:15