Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert byte array into string array?

 
Karthik Balasubramanian
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am working on the Bodgitt and Scarper assignment. I was trying to arite this function:
public String[] read(int recNo) throws RecordNotFoundException;
I created a byte array, where the recors values are read into. But when I look at the method definition its return type is array of string. I dont know how to convert byte[] into string[]...sorry for asking this basic question.please help me out guys...
Thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the things you had to do for the SCJP was to look at the Java 2 API.
So I suggest that you take 30 seconds (it took me 20) to find the solution...
But you will probably realize that what you are trying to do is not exactly what you need to do.
[ December 14, 2003: Message edited by: Barry Gaunt ]
 
Zak Nixon
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthik,
byte[] array = new byte[2];
String str = new String(array);
This is how I converted an arbitrary array of bytes to a String.
Zak Nixon
SCJP 1.4
:roll:
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return type : Is it String or String[]?
 
Zak Nixon
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return type is a String.
You will have to perform the String s = new String(byteArray); for
every record field you have and then build a String[] after all
of the fields have been read in.
Hope it helps.
Zak
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karthik,
If you want to take some specific Charset into account (instead of the platform default one), you may use the String constructor which accepts a charset name as second parameter.
That's if your input is a byte array. In case your input is a ByteBuffer, have a look at Charset.decode().
Best,
Phil.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11911
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vish,

Originally posted by Karthik Balasubramanian:
public String[] read(int recNo) throws RecordNotFoundException;
Originally posted by Vish Kumar:
The return type : Is it String or String[]?

The return type is String[]
This method reads a record and returns an array of fields which make up that record - each field is a String, therefore it is an array of Strings.
You might choose to read the entire record as one byte array, and then break that byte array into each individual field and convert it to a String. Alternatively you might choose to read each individual field as a byte array, and convert that to a String.
Regards, Andrew
 
Karthik Balasubramanian
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you all guys. As you guys suggested, I converted them into string then copied into a atring array.
Thanks for your help.
Karthik
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic