Reputation: 29
I have been working with java just a little, but there is these small logic things that I seem to never understand and I'll give an example right away. I currently have this code:
class test {
public static void main(String[] args) {
ST<String, Integer> st;
st = new ST<String, Integer>();
for (int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
st.put("P", 5); // Put method
for (String s : st.keys()) {
StdOut.println(s + " " + st.get(s));
}
}
}
So what it does is that it creates a Symbol Table and inserts some data to it from a file provided. As you can see, I have a put method which inserts the value 5 on key "P". I want to create a method for this just for the sake of practice. So what I do is:
public static void addValue() {
st.put("P", 5);
}
and call that method instead of the "put" method in my code. However I can't compile this, since the method "addValue" doesn't know the variable st
.
I then thought to put these two lines:
ST<String, Integer> st;
st = new ST<String, Integer>();
into the class constructor, but that didn't make it. Can someone please explain some logic behind this because theres clearly something I'm missing. How can I split this code into methods in a nice way just for the sake of practice? + If anyone knows of a good place to just read about logic like this, I'll be enourmously appreciated.
Thanks in advance.
Upvotes: 2
Views: 317
Reputation: 109
You can either add it as a class member
class test {
private static ST<String, Integer> st;
public static void main(String[] args) {
st = new ST<String, Integer>();
for (int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
addValue();
for (String s : st.keys()) {
StdOut.println(s + " " + st.get(s));
}
}
public static void addValue() {
st.put("P", 5);
}
}
or pass it in as a variable
public static void addValue(ST<String, Integer> st) {
st.put("P", 5);
}
Then from your main method you can just call
addValue(st);
Upvotes: 2
Reputation: 363
There are a couple of issues here.
Test
(convention)ST
is not part of Java (in your comments you write its part of a library, this information should be part of the question), same for StdIn
and StdOut
.To actually solve your question, there are a couple of solutions. BlithE already showed you one, the other is mentioned by David Zhou in a comment:
// missing ST StdIn and StdOut, they probably need too be imported
public class Test {
public static void main(String[] args) {
ST<String, Integer> st;
st = new ST<String, Integer>();
for (int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
addValue(st);
for (String s : st.keys()) {
StdOut.println(s + " " + st.get(s));
}
}
public static void addValue(ST<String, Integer> st) {
st.put("P", 5);
}
}
Upvotes: 1
Reputation: 17577
The variable st
is only known to the main method because you declared it there. To allow access from other methods you should create a class member. Here is the class that should do what you want:
class test {
private static ST<String, Integer> st;
public static void main(String[] args) {
st = new ST<String, Integer>();
for (int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
addValue();
for (String s : st.keys()) {
StdOut.println(s + " " + st.get(s));
}
}
public static void addValue() {
st.put("P", 5);
}
}
Upvotes: 2