last day (14 days later) » 

5:26 PM
Hello
 
Hello
 
Im still having a problem, with regards to a new array I made..
For some reason im never able to print them out
The previous one worked
Say I made an array
int[] ns = new int[scores.size()];
public void fillNS(){
for (int x = 0; x < ns.length; x++){
ns[x] = scores.get(x) * (x+1);
}
}

public void printNS(){
for (int x = 0; x < ns.length; x++)
System.out.print(ns[x] + ", ");
}
whenever i call printNS() in main, nothing prints out
 
Did you print scores list before executing this function?
 
in main, yes
tihis is under main:
ScoringNames abc = new ScoringNames();
abc.readFile();
abc.sortNames();
abc.printNames();
abc.addScores();
abc.printScores();
abc.fillNS();
abc.printNS();
 
Did you get any results in Scores list?
 
5:30 PM
yes it works fine
 
Okay then I think the problem is that you are defining array in either start of the program or in start of main function. So until the abc.addScores() method gets called , you still have scores list as an empty list so if you declare the array before calling abc.addScores() method, You have just defined an array with size zero so none of the loops will get execute and just won't print anything.
 
oh
 
SO you need to declare the array after calling abc.addScores() method which had changed the scores list already so That list won't be the empty list now if it has some valid data in it.
 
How do I change it to work tho
Hmm
Can I send you my file so you can see how ive structured
the program
 
So you can modify the main function like this.


ScoringNames abc = new ScoringNames();
abc.readFile();
abc.sortNames();
abc.printNames();
abc.addScores();
abc.printScores();
abc.fillNS();
abc.printNS();
 
5:33 PM
because all the arrays are in the class file, not main
 
ScoringNames abc = new ScoringNames();
abc.readFile();
abc.sortNames();
abc.printNames();
abc.addScores();
abc.printScores();
int ns[]=new int[scores.size()];
abc.fillNS();
abc.printNS();
Modify main method with this
 
should i delete ns[] from class
 
yes
declare it in main method
 
hmm but scores.size() wont work
in the declaration
 
or just declare the array name there.
int ns[];

and in main method assing it memory like:
ns=new int[scores.size()];
It will work in main method.
 
5:36 PM
hold on
public class ScoringNames {
BufferedReader x = null;
String location = "C:\\Users\\Rohan Vidyarthi\\Dropbox\\"
+ "College\\COSC 1430\\Scoring Names\\names.txt";

ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> scores = new ArrayList<Integer>();
int[] ns;

~random methods~


public static void main(String[] args) {
ScoringNames abc = new ScoringNames();
abc.readFile();
abc.sortNames();
abc.printNames();
abc.addScores();
abc.printScores();
ns[]] = new int[scores.size()];
abc.fillNS();
like this?
 
yes
you did some typo there I think.
ns[]]
here it is ns.
so ns=new int[scores.size()];
 
ns cannot be resolved to a variable
Cannot make a static reference to the non-static field scores
 
define it as static int[] ns
not int[] ns;
 
Cannot make a static reference to the non-static field scores

at ScoringNames.main(ScoringNames.java:176)
scores.size() is where the problem is
in main
 
define scores as static
 
5:44 PM
it WORKED!
 
omg thank you Sanket!!
 
You're welcome!
:)
 
good bye!
 

last day (14 days later) »