Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jun Kakeno
iOS Development with Swift Techdegree Graduate 24,842 PointsHelp!!! Storing user input and reuse the entered value in printf() statement
I'm a begginer to programing and I'm working on my first project. I'm trying to user the user input value and reuse that value in a sensence. The requirement is that I need to use System.in to read user input and System.out to print.
Issue: Method jar.getItemName() and jar.getItemNumber() take-in NEW user input everytime I call them as printf() parameters.
Target: I need to STORE the original user input and reuse the stored value in a sentence with printf().
Attempts: 1) Assigned the methods above to a variable. 2) Pass a declared variable into nextInt() and nextLine(). 3) Pass the String itemName and int itemNumber as printf() parameters. All of the above resulted in compile errors.
I'm STOCK please help!!!
I've made a Jar class and game class.
Jar class
import java.util.Scanner;
class Jar { public String itemName; public int itemNumber;
Scanner Name = new Scanner(System.in);
Scanner Number = new Scanner(System.in);
public Jar (String itemName){
this.itemName = itemName;
}
public Jar (int itemNumber){
this.itemNumber = itemNumber;
}
public String getItemName() {
String itemName = Name.nextLine();
return itemName;
}
public int getItemNumber(){
int itemNumber = Number.nextInt();
return itemNumber;
}
}
Game class
public class Game {
public static void main(String[] args) { // Your amazing code goes here...
Jar jar = new Jar("");
System.out.printf("ADMINITRATOR SETUP %n**************************%n");
System.out.printf("Name of the item in the jar: ");
System.out.printf("The maximum number of %s in the jar: ", jar.getItemName());
jar.getItemNumber();
System.out.println("");
System.out.printf("PLAYER %n**************************%n");
System.out.printf("The maximum number of %s in the jar is %d. %n", jar.getItemName(),jar.getItemNumber());
System.out.printf("Guess: %n");
}
}
Jun Kakeno
iOS Development with Swift Techdegree Graduate 24,842 PointsRyan thank you so much for your response. I was able to resolve the issue by storing the method return value in a variable I've made in the Game class. This way I can reuse these values over and over. The key was the order in which the method return value are assigned to the variable as the program is executed from top to bottom one line at a time. However I still don't have a good understanding on the concept 'Setters' you've mentioned. Could you kindly elaborate?
1 Answer
Ryan Schron
1,084 PointsSure.
First its important to understand access restrictions in Java (I'm not familiar with the curriculum here as I learned it during university). This table from stack overflow is a good summary:
| Class | Package | Subclass | Subclass | World
| | |(same pkg)|(diff pkg)|
————————————+———————+—————————+——————————+——————————+————————
public | + | + | + | + | +
————————————+———————+—————————+——————————+——————————+————————
protected | + | + | + | + | o
————————————+———————+—————————+——————————+——————————+————————
no modifier | + | + | + | o | o
————————————+———————+—————————+——————————+——————————+————————
private | + | o | o | o | o
+ : accessible
o : not accessible
Right now you are using public/no modifier declarations when you declare variables and methods so their access isn't restricted. Here is an example of public/private use:
public class Foo{
public int num1 = 1;
private int num2 = 2;
}
public class Bar{
Foo foo = new Foo();
public int num1Test = foo.num1; //Here we directly access the var in Foo, and its OK
public int num2Test = foo.num2; //Here we try and access the private var in Foo, and this is NOT ok
}
what <strong>getters</strong> and <strong>setters</strong> allow for us to do is to bypass some of these restrictions as we choose, so we can limit the scope that other pieces of code can access our data. These are simple methods that control the flow and are typically in this form:
public int getNum2(){
return num2;
}
public void setNum2(int value){
num2 = value;
}
if we define those methods in the class Foo above, we can do the following:
public class Foo{
public int num1 = 1;
private int num2 = 2;
public int getNum2(){
return num2;
}
public void setNum2(int value){
num2 = value;
}
}
public class Bar{
public int num1Test = foo.num1; //Still OK;
public int num2Test = foo.getNum2(); //Now it works!
foo.setNum2(17); //We can also change the variable now using a setter!
}
Basically they are simple methods used for access control. Here's a final example of why that my be important:
public void setNum2(int value){
if(value == 42){
throw new IllegalArgumentException(); //Throw an error if someone tries to set our value to a specific number
} else{
num2 = value;
}
}
Maybe its not entirely relevant to your problem, but its a nice trick to be aware of.
Jun Kakeno
iOS Development with Swift Techdegree Graduate 24,842 PointsRyan, thank you for taking the time to help me. I was missing understanding this concept of accessing data. This helped thanks again.
Ryan Schron
1,084 PointsRyan Schron
1,084 PointsHi Jun,
From what I see you are moving in the right direction, but could use some clarity.
Firstly think of the scanner as some object that's going to get things from you. It is not bounded by type (though it contains methods that will act on certain types), so there is no reason to create multiple scanner objects. Typically you'll see something like
Scanner sc = new Scanner(System.in);
and the same scanner will be used repeatedly.
The second thing I see is that you are defining 2 constructors. This is actually OK if you know what you are doing because you can take advantage of overriding, but I don't think that is intended in this case. The way you have your code setup would only make sense if you wanted to construct 2 different Jar objects, and wanted to initialize them differently. What you want instead are called setters. These are methods designed for setting object data when the object is constructed in another class. Here is an example:
Then in another class you could do Jar.setNumber(37); to set that fields value;
If your fields and object are public, however, you do not need a method to do this and can access the field directly. Here is an example:
My third tip is you should never begin your variable names with a capitol letter. Its just bad code etiquette. Only class names should start with a capitol letter, otherwise use camel case. See my setNumber method as an example.
Finally to read in, youll want to do something like the following:
You should try and reference the scanner class documentation to see a comprehensive list of available methods. I hope this helps.