Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting Char array to String

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,

One way of converting a char array to string is the following way.

char data[] = {'a', 'b', 'c'};
String str = new String(data);

However, i have a scenario as follows
I donot know the number of elements in the char array.

char data[]=new char[10];

data[0]='a';
data[1]='b';
data[2]='c';
data[3]='d';data[4]='e';data[5]='f';


String str= new String(data);
System.out.println(str);

The output displayed is
abcdef[[[[ . I would like to get rid of the null characters at the end. How should i declare the char array.


The following are incorrect ways
1) char data[];
data[0]='a'

2) char data[]=new char[];

Can someone suggest me how to declare a char array without any fixed length.

Thanks
Santosh

 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The String class does have a constructor that lets you specify a portion of the char array to use.

Can someone suggest me how to declare a char array without any fixed length.



Not possible. Java doesn't support variable size arrays. For that, take a look at using one of the collection classes.

Henry
 
Santosh Kum
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply.

But creates problem for my program.
My task is to replace a word in a String.(Without using inbuilt functions like split or using Tokenizer).
Can anybody provide any hints how to do this.

I tried using a character array but it is becoming messy as converting a char array to string is proving a bit of problem.







Snippet of my awkward code


String str="I am having breakfast";
String replacableword="breakfast";
String newword="dinner";
//String newstring="";

// The output after replacing the word is stored in a char array called newstring
char newstring []=new char[40];
char arrayofwords[][]=new char[30][40];
int i=0;
int w=0;
int len=str.length();



a: while(i<len)
{

if((str.charAt(i))==' ')
{
i++;
}

int l=0;
while(str.charAt(i)!=' ')
{


arrayofwords[w][l]=str.charAt(i);
i++;l++;

if(i==len)
{
w++;
break a;
}


}
w++;

}




Thanks
Santosh >
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Santosh Kum wrote:
char data[]=new char[10];

data[0]='a';
data[1]='b';
data[2]='c';
data[3]='d';data[4]='e';data[5]='f';


String str= new String(data);
System.out.println(str);

The output displayed is
abcdef[[[[ . I would like to get rid of the null characters at the end. How should i declare the char array.



String str = new String(data, 0, 6);

(Note that Mr. Wong pointed you in this direction.)
 
Marshal
Posts: 79523
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot replace part of a String; Strings are immutable and have no methods allowing you to do that. You can use the replace/replaceAll methods to create a new String with the alterations, but the best thing to do for Strings you wish to alter is to use StringBuilder.

I won't tell you about using reflection because this is the beginners' forum.
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic