Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
public void userInput(String cityName, int cityPopulation, int cityInfrastructureLevel) {
    Scanner userInput = new Scanner (System.in);
    System.out.println("Enter a city name:");
    cityName = userInput.nextLine();
    System.out.println("Your city's name is " + cityName);
}
public static void main(String[] args) { 
    NewClass myClass = new NewClass(cityName, cityPopulation, cityInfrastructureLevel);
    myClass.userInput(name, population, infrastructureLevel);
    System.out.println(myClass.getName());
}


What I have tried:

I've tried calling a method from the main method that took user input.

Then I tried accessing the Object Attributes with getter methods and that still
the result was null & 0.

It was working when I had the Scanner instances in main.
Any suggestions I would be really grateful for!
Posted
Updated 13-Sep-22 20:52pm
v2
Comments
Richard MacCutchan 13-Sep-22 15:59pm    
The question is not clear, and the existing code is confusing.

That code is ... confusing: We have no idea what NewClass is, or what it contains.

cityName, cityPopulation, and cityInfrastructureLevel Are not declared in the Main method, nor are they obviously anywhere in the containing class. The same for name, population, and infrastructureLevel

If NewClass contains Main, then it's possible that cityName, cityPopulation, cityInfrastructureLevel are declared as static variables within that class, but then they aren't part of the instance you create at the start of the method, so there is only one set for the whole application rather than it being different for each instance of the class. If there weren't static, then Main couldn't access them because it is!

Then, the use of them as parameters to the userInput method means that inside the method all references to then refer to the parameters, not any instance version.
And because in Java all method parameters are passed by value, not by reference any changes you make to them aren't "linked" to the external versions - which is why you get null values when you try to use them in the

Go back to the working version, and think about what you are trying to do: I suspect that you need to create a parameterless userInput function that fills in the class instance variables of the same name - but from that fragment it's difficult to tell exactly what you intended to do.

Sorry if that sounds negative and unhelpful, but I don't think you have quite grasped what instances of classes are and how to use them yet - which is why your code is so confusing to everybody else!
 
Share this answer
 
Hello Sir, thank you so much for replying so quickly and I'm really sorry about the mess of code that I had for the question.

It's a lot clearer now about how method parameters passed by value and not by instance. I had them
add static global variables but I think my mistake was to pass them into the user input function.

I will try your idea about creating a parameter less userInput method.

Thank you so much for your help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900