Forums Register Login

char array initialization dynamically

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

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
+Pie Number of slices to send: Send
k... sorry.. got my mistakes.. thanks a lot!!
so i think i have to initialize my array directly like
char strtsnh[] = {'hyundai','bmw'};
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
ya actually i am more into C. got it.. thanks!
used it the program, it ran without errors.!!!
thanks a lot.!!
1
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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[].
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2281 times.
Similar Threads
Read keyboard IO methods hangs.
IOException is never thrown in body of corresponding try statement
Java adding infinte strings
how come i can't call my search method from my main method?
String Input and Bubble Sort
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:09:30.