• Post Reply Bookmark Topic Watch Topic
  • New Topic

char array initialization dynamically  RSS feed

 
Sneh Bansode
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have this code.

System.out.println(" Enter the no. of vechicles");
int p= new Interger.parseInt(stdin.readLine());
char strtsnh[] = new char[p];
System.out.println(" Enter the names of the cars in the street");
for(i=0;i<p.lenth;i++) // p is the array lenght
{
strtsnh[i]= Char.parseChar(stdin.readLine()); // is this wrong?
}
I want to enter the names of the cars dynamically from the user and store them in the array. how can i do this in this program?
 
Wayan Saryada
Ranch Hand
Posts: 119
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are way to many errors in your code. It doesn't even compile. For example there is no such thing as p.length. There is no Char.parseChar() can be found in the Java API.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And also ... taking guesses on what is implied from the pseudo code ... it looks like you are trying to store each car's name, which is a string, into a single character. This doesn't sound reasonable, does it?

Henry
 
Sneh Bansode
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k... sorry.. got my mistakes.. thanks a lot!!
so i think i have to initialize my array directly like
char strtsnh[] = {'hyundai','bmw'};
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sneh Bansode wrote:char strtsnh[] = {'hyundai','bmw'};

This is not valid Java code. A char can only store a single character. An array of chars can store only a single character in each entry.

Are you a C programmer? In Java, strings are stored in String objects, not in character arrays. You probably want something like this.

 
Sneh Bansode
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya actually i am more into C. got it.. thanks!
used it the program, it ran without errors.!!!
thanks a lot.!!
 
Josh Rowe
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper is right, the code compiles and should run. Although, when using Java array initializers, I was taught that the short hand form was etc.. I would do it this way to make you have to type less. If that works for you. Another thing to keep in mind is that C and Java conventions are different, not just the syntax. In Java, the convention is to use the array declaration with the type and then array brackets followed by the name of the reference variable. ex. while in C this statement would look like Java simply allows this syntax or convention to conform to C developers and make transitions to Java from C easier, but the use of C conventions in Java is discouraged.I like to keep conventions different for different languages, and by doing this, you can keep yourself from making silly syntax errors that you'll pay for later, especially in bigger programs. I guess, in the end, it is up to what style you want. You can also create strings without calling the String constructor explicitly, same thing goes for arrays I suppose?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right Josh, when initializing a variable you can use the short-hand form:

In other words, you don't need to add new String[] explicitly. However, when assigning to an existing variable (not initializing a newly declared variable), you can't use the short-hand form, you'll have to include the new String[].
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!