Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

splitting char [] into smaller char's []  RSS feed

 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have a file that looks like this:

0001 #n word word word #d word word word
0002 #n word word word #d word word word
.......

And I want to pull out information from it I'll need later in my program.
This program is an exercise for university and we must not use String structures except for input/output.

My idea is to read the file into a String and then convert it into a char [] to read it with another method I want to create in a separate class.

how can I read from '0' to '#', save that into in a char [], then from 'n' to '#' and save it in a char [] and the same from 'd' to '\n' ???

I tried this: (just for the char that will be called "name")

This doesn't report any compilation issues, but it does nothing.


How can I solve this problem??

Thanks
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure this isn't an exercise in using REGEX? I don't see another good reason why your instructor would make things unnecessarily difficult for you here. Also, toCharArray() is a String method so that might not even be legal. You might have to read the file character by character to begin with because getting a String and not being allowed to do anything with it isn't going to help you.

If you do decide that getting a character array is legal/best, then it looks like you're on the right track, but you've made things way too complicated by adding a separate class and using class fields. Its probably best to have multiple static methods that take your main array as an argument and have them each return the appropriate smaller array. It might be easier to use ArrayList<Character> so you don't have to worry about knowing the size ahead of time and it also allows you to use the indexOf() to quickly find the endpoint of the data you're trying to extract.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!