Forums Register Login
need help with string variables
New to java working through murach beginning java with eclipse book and im stuck.

The value of the local variable name is not used.... the "name" is underlined in last line of text and thats the error msg i get

Any idea how to fix this?
That is not an error, but rather a warning. This feature is provided by your IDE (Eclipse in your case).
Basically what it says, that you created something, but never used it, hence suggests you to consider, either use it or remove it.
Better explanation of your Question appreciated!!!
(1 like)
Welcome to the Ranch

What colour is the text underlined? You only need to worry if it is red; yellow marks are a warning. The code shou‍ld still run with warnings. I presume you got the message from hovering your mouse over the yellow triangle.
What you are doing is creating a local variable name and never using it. If you follow that by
then Eclipse will perceive that you have indeed used the variable and the warning shou‍ld disappear.
Unfortunate, that your IDE didn't help you much with indentation.
Thanks for the help guys im only in the second chapter of this book and its the first programming language im trying to lean.

thanks campbell... the underline was orange but it still wouldn't run... i did get the message from hovering over the triangle... i added in that line you said and it now works so thanks again.
Santosh i was trying to figure out why i was getting a warning message under the word "name" in the last line of my code.
Liutauras thanks for the help and whats wrong with my indentation? I know it matters but i dont really understand what to indent and where to please explain
(1 like)
Yours code below:

1. As you see your line 4 is shifted by four spaces to the right, which is correct. Now look at lines 5-7. They are shifted only by two spaces - inconsistent with line 4. I'd suggest to follow line 4 style.

2. Line 8 is empty for no reason. Either it needs to be fulfilled with some code or simply removed. Campbell suggested to add print statement, so it shouldn't be empty by now.

3. Line 9 supposed to be aligned with line 4.

4. There supposed to be a space between lines 3 and 4, so the method declaration would be separated from class declaration. Usually with no empty line after the class declaration are instance variables declared.

5. As for a better practice, when you declare main() method parameter, write '[]' right after the data type, in this case String. Always when you declare array type, write those angle brackets after the data type. There is similar thing to array as variable arguments varargs (which you'll learn some time later), and in case you'd write its ellipsis same as array identifier after the variable name - you'd get a compile time error. It isn't the most important argument about that. Most important is, that most developers write array identifiers after the data type when they declare array.

After the fixing what I have mentioned, your code supposed to look as:

Note: points 1 and 3 are about indentation; points 2 and 4 are about the formatting; line 5 is about style.
(1 like)

scout myers wrote:Thanks . . .

That's a pleasure

the underline was orange but it still wouldn't run... . . .

But it did run. It started, created the name, produced no output, and stopped. Since there was nothing to see, you thought it didn't run. Eclipse would have complained bitterly had the code proven impossible to run.

Liutauras thanks for the help and whats wrong with my indentation? I know it matters but i dont really understand what to indent and where to please explain

There are several different indentation conventions; be consistent with whichever you are using. We have some suggestions here. It is more important that you use a convention consistently than which convention you choose. You will suffer if you indent code inconsistently because you won't be able to follow the flow of execution. If we complain about indentation, it is for your benefit nor ours; we have had lots of practice reading badly indented code.

I won't tell you that Eclipse will correct indentation if you use ctrl‑I.
(1 like)
Fixed my code Liutauras thanks for that and can you explain what you mean by array?
Campbell is there anyway for me to know it ran? and thanks for that link
You get a little red square above the console window; that won't stay there for long. It is more a case of Eclipse complaining if the program can't be run.
Ok i see where you mean Campbell i normally dont have that window open though i just have the package explorer and the actual coding window open should i leave the console window up?
It's useful to have the console window open when you are running your program as any output from the program via System.out.println() will appear in that window as will any uncaught or dumped exceptions.

scout myers wrote:. . . explain what you mean by array? . . .

The only array I can see is args. You can read more about it here, or you can forget about it for a few weeks.
What Liutauras was saying is that the [] is part of the type, not part of the name, so shou‍ld go on the left. Not String args[], but String[] args, even though I saw the former format in older books.
Tony the console window pulls up in a small window when i run the code.

Campbell it had [] before args in the book but when i ran it one time i got a red error message and it had it after so i though maybe it was a typo and switched it

scout myers wrote:. . . it had [] before args in the book but when i ran it one time i got a red error message . . .

The compiler will accept either in Java®. It won't accept both in some other languages.
theres a option when i create a class to put it in automatically so ill use that for now till i find out more about it later in this book
While we're talking some guy the other day mentioned something about a programming language called python and said it was gaining popularity any thoughts on that language and should i learn that one instead of java before i get to far into this book on java?
No, learn Java. It is better language to start in my opinion. To move from Java to Python is way easier than other way round I would say. But you can create other topic and we can discuss that in more details and hear more opinions.
Ok Liutauras thanks again

This thread has been viewed 503 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 12, 2018 02:16:37.