Reputation: 33
Trying to use setVaraible()
to make the String "Hometown"
equal user input, so it can be called back later. Someone else previously helped me with something of this nature, but it was used in a loop, so I can't exactly see how it worked.
private static String InputName;
private static String Sex;
private static String Age;
private static String input;
private static String Hometown;
It looked like this:
while (sc.hasNext()) {
input = sc.next();
System.out.println("");
setVariable(a, input);
if(input.equalsIgnoreCase("no")){
sc.close();
break;
}
else if(a>questions.length -1)
{
a = 0;
}
else{
a++;
}
if(a>questions.length -1){
System.out.println("Fine, " + InputName
+ ", so, you are " + Age + " years old and " + Sex + "." );
System.out.println("");
sleep();
System.out.println("Do you need to change any information?\n");
}
if(!input.equalsIgnoreCase("no") && a<questions.length){
System.out.println(questions[a]);
}
And, I just need it here (I know this code is wrong):
static public void Intro() {
int a = 0;
setVariable(3, Hometown);
String[] questions = {"What do you want to name your hometown?"};
System.out.println(questions);
input = sc.next();
}
Here is the setVariable()
method:
static void setVariable(int a, String Field) {
switch (a) {
case 0:
InputName = Field;
return;
case 1:
Age = Field;
return;
case 2:
Sex = Field;
return;
case 3:
Hometown = Field;
return;
}
}
Upvotes: 0
Views: 1595
Reputation: 347244
Basically, you can call setVariable
by passing it two values, the first is a int
value which represents field to be set and the second is the value to apply, for example
setVariable(0, "This is the input name");
Would set the InputName
variable to "This is the input name"
Using 1
would allow you to change the Age
variable, 2
the Sex
variable and 3
the Hometown
variable, so basically, what you need to try is something like...
setVariable(3, "New home town value");
For example...
Currently your code looks something like...
static public void Intro() {
int a = 0;
setVariable(3, Hometown);
String[] questions = {"What do you want to name your hometown?"};
System.out.println(questions);
input = sc.next();
}
Which basically assigns the current value of Hometown
to itself...which is probably null
, then you try and read a value from the Scanner
Try it the other way round, for example...
static public void Intro() {
System.out.println("What do you want to name your hometown?");
input = sc.nextLine();
setVariable(3, input);
}
Making sure you pass the input
value to the method, so it can assign the value to the correct field...
Personally, I don't find this either useful or intuitive. Sure you could use an enum
or even define static final
variables that would make determine which field you want to change easier, but Java is an Object Oriented language, we might as well make use of it...
I would encourage you to define a Person
object (for argument sake) with which you can set/get the individual properties of the object, for example...
public class Person {
private String inputName;
private String sex;
private String age;
private String hometown;
public void setName(String name) {
this.inputName = name;
}
public String getName() {
return inputName;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setAge(String age) {
this.age = age;
}
public String getAge() {
return age;
}
public void setHometown(String hometown) {
this.hometown = hometown;
}
public String getHometown() {
return hometown;
}
}
Then you would simply create a new instance of Person
Person person = new Person();
And fill it out...
person.setName("Ruphet");
person.setSex("Male");
person.setAge("18");
person.setHometown("Murembugie");
And when you need to show or "get" some value...
System.out.println("Hello " + person.getName() + " from " + person.getHometown());
ps-
You might like to take a read through Code Conventions for the Java Programming Language, it will make it easier for others to read your code (and for you to read others)...
Upvotes: 1
Reputation: 201447
Try something like this.
// This method should not be static, but your Hometown is.
public static void setHometown(String input) {
// This should not be a static variable, also it shouldn't be capitalized.
if (input == null) {
Hometown = "";
} else {
Hometown = input;
}
}
Upvotes: 0