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?
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.
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?
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?
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[].
Post by:autobot
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