changing scope of variable in java

July 17, 2012 at 14:42:20
Specs: Windows 7
Here's my code:

import java.util.*;

public class Start
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter your login code: ");
int loginCode = scan.nextInt();
if (loginCode == 524)
{
String name = "Name1";
}
if (loginCode == 781)
{
String name = "Name2";
}
System.out.println("Hello, " + name);
}
}

My IDE gives me an error on the third to last line, and I read somewhere that I need to change the scope of the String 'name', but I don't know how to do this and I couldn't find anything online. Can anyone help me?


See More: changing scope of variable in java

Report •

#1
July 18, 2012 at 02:48:43
This is very basic stuff, and it's not the first such question that you have asked. I suggest that you get yourself a good book on Java programming, or follow an online tutorial.

In the meantime, read this link: http://www.java2s.com/Tutorial/Java... to learn about variable scope. And you may find the rest of the tutorial valuable too.


Report •

#2
July 18, 2012 at 14:38:20
yeah, that link didn't really help. And I'm sorry, I am just starting to learn java.

Report •

#3
July 18, 2012 at 17:09:28
import java.util.*;

public class Start
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter your login code: ");
        int loginCode = scan.nextInt();
        String name = "";
        if (loginCode == 524)
        {
            name = "Name1";
        }
        if (loginCode == 781)
        {
            name = "Name2";
        }
        System.out.println("Hello, " + name);
    }
}

That's it. btw, it's easier to read indented code (which is why I formatted it.) And ijack is right, a good programming book should help answer basic questions like these.


Report •

Related Solutions

#4
July 18, 2012 at 23:04:46
The fact that you are just learning Java is the reason that I suggest that you work through a good tutorial such as the one I linked to.

Report •

#5
July 21, 2012 at 18:58:09
technogeek: well, that code gives me an error and says that 'strings are immutable and cannot be changed'.

ijack: So basically you are saying that I shouldn't ask for help or post anything on computing.net until I am super-advanced, but by then I probably won't need help. OK. Goodbye forever, I guess... BTW I never posted a question about the scope of a variable, one of my previous questions solution was that the System.out class was not static for some reason, and had nothing to do with the scope of the variable I was displaying.
"The only bad question is the one that goes unasked."


Report •

#6
July 21, 2012 at 19:50:42
I think ijack's point is that you keep repeating the same mistakes, and these mistakes are caused by a misunderstanding of some very fundamental concepts.

You're trying to write an essay in (let's go with) German without first learning the language. While we can answer your question, I suspect you'll either fail to grasp the reasoning behind the answer, or worse you'll grasp the wrong reason. It would not be your fault, but your potential to learn Java would be harmed just the same. I suggest taking a beginners programming class, read a beginners programming book, or at the very least find someone you can talk to face-to-face with before you try to tackle Java on your own. First you crawl, then you walk, then you run.

How To Ask Questions The Smart Way


Report •

#7
July 22, 2012 at 03:25:35
"ijack: So basically you are saying that I shouldn't ask for help or post anything on computing.net until I am super-advanced, but by then I probably won't need help."

No. I am saying that forums are not a replacement for tutorials. To ask people for help with something that you don't understand and can't easily find the answer to yourself is one thing. To expect people to hand-hold you through the basic steps of learning a programming language is something else entirely. There are books and tutorials intended for exactly this purpose. I took the time to point you to a good Java tutorial; I don't expect thanks, but if you don't like my advice just ignore it. But don't act like a spoilt brat because you don't like my advice.

If you are not prepared to put in the effort to do a little learning on your own then you will not find computer programming a satisfying hobby or job to pursue. There is nothing natural about computer programming languages; you have to learn the basic syntax.


Report •

Ask Question