When I try to print my array in the main...I'm getting NULL and 0.0.
Can anyone give me some advice as to why this may be happening?
Since I was getting all Nulls and 0.0's I pulled the for loop into my dataCalculations method and it printed the name and the sales amount perfectly.
Is my use of the .length feature incorrect?
Any advice/help would be greatly appreciated.
Thanks,
/*
* Anthony Vincenzo Laginess
* CIT 130 HMW 08 Arrays
* 10/19/16
* Time Spent:
*/
package cit130mhmw08_laginess;
import java.util.Scanner;
public class CIT130MHMW08_Laginess
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("------------------------------------------");
System.out.println("Please enter the total number of dealers: ");
System.out.println("------------------------------------------");
int numDealers = input.nextInt();
numDealers = numberOfDealers(numDealers);
String[] dealerNames = new String[numDealers];
double[] dealerSales = new double[numDealers];
double[] commissionRate = new double[dealerSales.length];
double[] dealershipSalesTotal = new double[dealerSales.length];
double[] dealerSalesAvgTotal = new double[dealerSales.length];
double[] totalCommission = new double[commissionRate.length];
System.out.println("--------------------------------------------------------");
System.out.printf("Please enter the required data for each of your dealers: %n");
System.out.println("--------------------------------------------------------");
dataCalculation(numDealers);
System.out.println("----------------");
System.out.println("Dealer Totals: ");
System.out.println("----------------");
displayTotals(numDealers, dealerNames, dealerSales, commissionRate);
}//main
//METHOD 1
public static int numberOfDealers(int dealers)
{
int results;
Scanner input = new Scanner(System.in);
while(dealers < 0 || dealers > 30)
{
System.out.printf("%nEnter a valid number of dealers: %n");
dealers = input.nextInt();
}
results = dealers;
return results;
}//number of dealers methods
//METHOD 2
public static void dataCalculation(int data)
{
String[] dealerNames = new String[data];
Scanner input = new Scanner(System.in);
System.out.printf("%nEnter the names of the dealers:%n ");
for(int i = 0; i < data; i++)
{
String names =input.nextLine();
dealerNames[i]= names;
}
double[] dealerSales = new double[data];
System.out.printf("%nEnter their sales totals: %n");
for(int i = 0; i < data; i++)
{
double sales = input.nextDouble();
dealerSales[i] = sales;
}
}//data calculations
//METHOD 3
public static double[] commission(double[] dealerSales)
{
//Create array
double[] commissionRate = new double[dealerSales.length];
for(int i = 0; i < dealerSales.length; i++)
{
commissionRate[i] = dealerSales[i];
if(commissionRate[i] > 0 && commissionRate[i] < 5000)
commissionRate[i] = commissionRate[i] * 0.08;
else if(commissionRate[i] > 5000 && commissionRate[i] < 15000)
commissionRate[i] = commissionRate[i] * 0.15;
else if(commissionRate[i] > 15000)
commissionRate[i] = commissionRate[i] * 0.20;
}
return commissionRate;
}//commission method
public static double[] dealershipSales(double[] dealerSales)
{
//Create array
double[] dealershipSalesTotal = new double[dealerSales.length];
for(int i = 0; i < dealerSales.length; i++)
{
dealershipSalesTotal[i] += dealerSales[i];
}
return dealershipSalesTotal;
}//dealership sales
public static double[] dealerSalesAvg(double[] dealerSales)
{
double[] dealerSalesAvgTotal = new double[dealerSales.length];
for(int i = 0; i < dealerSales.length; i++)
{
dealerSalesAvgTotal[i] += dealerSales[i] / dealerSales.length;
}
return dealerSalesAvgTotal;
}//dealership sales averages
public static double[] dealershipTotalCommission(double[] commissionRate)
{
double[] totalCommission = new double[commissionRate.length];
for(int i = 0; i < commissionRate.length; i++)
{
totalCommission[i] += commissionRate[i];
}
return totalCommission;
}//total commission for the dealership
public static void displayTotals(int numDealers, String[] dealerNames, double[] dealerSales, double[] commissionRate)
{
for(int i = 0; i < numDealers; i++)
{
System.out.println(" " + dealerNames[i]);
System.out.println(" " + dealerSales[i]);
System.out.println(" " + commissionRate[i]);
}
}//display totals
}//class
You are not returning anything from dataCalculation(), so the calculations are lost when the method call is done. Just as dealerNames, dealerSales and commissionRate are passed as arguments to displayTotals(), they should probably be passed to dataCalculation() first rather than being declared again, at least the first two. You should call commission() to get the last, the method doesn’t seem to be called ever.
Related
I am finding the sum of multiple arrays and have searched various codes and programs but it seems none has worked for me or should I say none has actually made my brain work to create a code based on what I saw. "What I am trying to do is to add the 3 inputed Grade Arrays and then divide it by 3 to get the Array for Average."
package Activity;
import java.util.*;
public class TestCode {
static Scanner sc = new Scanner(System.in);
static int indexMax = 3;
public static void main(String[] args) {
String InfoName[][] = new String[indexMax][2];
Double InfoGrade[][] = new Double[indexMax][3];
InputInfoName(InfoName);
InputGrade(InfoGrade);
PrintInfo(InfoName, ComputeAvg);
}
public static String[][] InputInfoName(String NameArr[][]) {
for (int i = 0; i < indexMax; i++) {
System.out.println();
System.out.println("Enter Student[" + (i + 1) + "]'s Full Name");
System.out.println();
System.out.print("First Name: ");
NameArr[i][0] = sc.nextLine();
System.out.print("Last Name: ");
NameArr[i][1] = sc.nextLine();
}
return NameArr;
}
public static Double[][] InputGrade(Double GradeArr[][]) {
for (int i = 0; i < indexMax; i++) {
System.out.println();
System.out.println("Enter Student[" + (i + 1) + "]'s Grades");
System.out.println();
System.out.print("1st Grade: ");
GradeArr[i][2] = sc.nextDouble();
System.out.print("2nd Grade: ");
GradeArr[i][3] = sc.nextDouble();
System.out.print("3rd Grade: ");
GradeArr[i][4] = sc.nextDouble();
}
return GradeArr;
}
// Main Problem
public static Double[][] ComputeAvg(Double ComputeArr[][]) {
Double AddAvg[][] = new Double[indexMax][];
Double getAvg[][] = new Double[indexMax][];
for (int i = 0; i < indexMax; i++) {
AddAvg[i][] = // I don't know what is next
}
return ComputeArr;
}
// End of Main Problem
public static void PrintInfo(String arr[][], Double ComputeAvg[]) {
System.out.println("");
System.out.println("Student Info");
for (int i = 0; i < arr.length; i++) {
System.out.println("Student[" + i + "]: " + arr[i][0] + "," + arr[i][1] + " Average: " + ComputeAvg[i]);
}
}
}
Side-note Personally, I would replace the use of the Double object by the primitive-type double, mainly because of (apart from performance) readability and because in your code is not really needed.
Create the array that will keep the average by array:
Double AddAvg[] = new Double[indexMax];
iterate through the arrays and get the average of those arrays using Java streams:
Arrays.stream(ComputeArr[i])
.mapToDouble(Double::doubleValue)
.average()
.orElse(0);
The full method:
public static Double[] ComputeAvg(Double[][] ComputeArr) {
Double[] AddAvg = new Double[indexMax];
for (int i = 0; i < indexMax; i++) {
AddAvg[i] = Arrays.stream(ComputeArr[i])
.mapToDouble(Double::doubleValue)
.average()
.orElse(0);
}
return AddAvg;
}
If you cannot use streams then do the sum manually:
public static Double[] ComputeAvg(Double[][] ComputeArr) {
Double[] AddAvg = new Double[3];
for (int i = 0; i < 3; i++) {
double sum = 0.0;
for(int j = 0; j < ComputeArr[i].length; j++) {
sum += ComputeArr[i][j];
}
AddAvg[i] = sum / ComputeArr[i].length;
}
return AddAvg;
}
In Java arrays indexed from 0 to the size of the array -1. So this method:
public static Double[][] InputGrade(Double GradeArr[][]) {
for (int i = 0; i < indexMax; i++) {
System.out.println();
System.out.println("Enter Student[" + (i + 1) + "]'s Grades");
System.out.println();
System.out.print("1st Grade: ");
GradeArr[i][2] = sc.nextDouble();
System.out.print("2nd Grade: ");
GradeArr[i][3] = sc.nextDouble();
System.out.print("3rd Grade: ");
GradeArr[i][4] = sc.nextDouble();
}
return GradeArr;
}
is accessing positions outside the boundaries of the array GradeArr (i.e., GradeArr[i][4]). Change to :
public static Double[][] InputGrade(Double GradeArr[][]) {
for (int i = 0; i < indexMax; i++) {
System.out.println();
System.out.println("Enter Student[" + (i + 1) + "]'s Grades");
System.out.println();
System.out.print("1st Grade: ");
GradeArr[i][0] = sc.nextDouble();
System.out.print("2nd Grade: ");
GradeArr[i][1] = sc.nextDouble();
System.out.print("3rd Grade: ");
GradeArr[i][2] = sc.nextDouble();
}
return GradeArr;
}
If it is possible to use Stream API, the averages of 2D Double array may be calculated using DoubleStream::average:
static Double[] calcAvg(Double[][] arr) {
return Arrays
.stream(arr) // Stream<Double[]>
.map(inner -> Arrays.stream(inner) // Stream<Double>
.mapToDouble(Double::doubleValue) // convert to primitive double
.average() // average calculated as OptionalDouble
.orElse(0.0) // in case of empty array
)
.toArray(Double[]::new);
}
Also, Collectors::averagingDouble may be used to make it even less verbose:
static Double[] calcAvg(Double[][] arr) {
return Arrays
.stream(arr) // Stream<Double[]>
.map(inner -> Arrays.stream(inner)
.collect(Collectors.averagingDouble(Double::doubleValue)) // or x -> x
)
.toArray(Double[]::new);
}
Tests
Double[][] arr = {
{},
{0.1},
{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0, 7.0, 8.0},
{5.0, 11.0, 17.0, 23.0, 29.0, 35.0},
};
System.out.println(Arrays.toString(calcAvg(new Double[0][])));
System.out.println(Arrays.toString(calcAvg(arr)));
Output:
[]
[0.0, 0.1, 2.0, 6.0, 20.0]
when I attempt to run this program it will accept the inputs but will only print out 0.0 0.0 0.0 for all prints. I need it to print the imputed numbers and then print the average to 2 decimal accuracy. Why is it only printing 0s and not the numbers put in
import java.util.Scanner;
import java.util.Arrays;
public class pcCalculate
{
double[] pcPrice = new double [10];
public static void main(String[] args)
{
Quiz input = new Quiz();
Quiz display = new Quiz();
Quiz avgCalc = new Quiz();
input.arrayInput();
display.displayPCPrices();
avgCalc.avgPCCalc();
}
public void displayPCPrices()
{
System.out.println(Arrays.toString(pcPrice));
}
public double avgPCCalc()
{
int sum = 0;
for (int i=0; i < pcPrice.length; i++)
{
sum = (int) (sum + pcPrice[i]);
}
double average = sum / (double)pcPrice.length;
return average;
}
public void arrayInput()
{
Scanner price = new Scanner(System.in);
System.out.println("Enter Prices");
pcPrice[0] = price.nextDouble();
pcPrice[1] = price.nextDouble();
pcPrice[2] = price.nextDouble();
pcPrice[3] = price.nextDouble();
pcPrice[4] = price.nextDouble();
pcPrice[5] = price.nextDouble();
pcPrice[6] = price.nextDouble();
pcPrice[7] = price.nextDouble();
pcPrice[8] = price.nextDouble();
pcPrice[9] = price.nextDouble();
}
}
Arrays are better in loops, in this case, I would to something like this:
import java.util.Scanner;
import java.util.Arrays;
public class pcCalculate {
double[] pcPrice = new double [10];
public static void main(String[] args){
Scanner price = new Scanner(System.in);
for(int i=0; i< pcPrice.length; i++){
System.out.print("Set Price " + (i + 1) + ":");
pcPrice[i] = price.nextDouble();
}
displayPCPrices();
System.out.println("The average is :" + avgPCCalc());
}
public void displayPCPrices()
{
System.out.println("The prices are: " + "\n");
for(int i = 0; i< pcPrice.length; i++){
System.out.print(pcPrice[i] + " ");
}
}
public double avgPCCalc()
{
double sum = 0;
for (int i=0; i < pcPrice.length; i++)
{
sum += pcPrice[i];
}
double average = sum / 10;
return average;
}
}
Call your displayPCPrices method from your main and pass in pcPrice as a parameter:
import java.util.Scanner;
import java.util.Arrays;
public class pcCalculate
{
public static void main(String[] args)
{
double[] pcPrice = new double [10];
Scanner price = new Scanner(System.in);
System.out.println("Enter Prices");
pcPrice[0] = price.nextDouble();
pcPrice[1] = price.nextDouble();
pcPrice[2] = price.nextDouble();
pcPrice[3] = price.nextDouble();
pcPrice[4] = price.nextDouble();
pcPrice[5] = price.nextDouble();
pcPrice[6] = price.nextDouble();
pcPrice[7] = price.nextDouble();
pcPrice[8] = price.nextDouble();
pcPrice[9] = price.nextDouble();
displayPCPrices(pcPrice);
}
public void displayPCPrices(Array pcPrice)
{
System.out.println(Arrays.toString(pcPrice));
}
public double avgPCCalc()
{
int sum = 0;
for (int i=0; i < pcPrice.length; i++)
{
sum = (int) (sum + pcPrice[i]);
}
double average = sum / (double)pcPrice.length;
return average;
}
}
EDIT: And just so you're aware, main(String[] args) is where your program will always start, from there you call other methods and do your work. Nothing else will be calling your main method.
EDIT2: If you'd like to have a conversation about your coding practices shoot me a message, I see you've changed your question and you're missing some basic concepts that would help you easily solve this issue.
I'm trying to teach myself Java and I'm having a hell of a time! :)
I just started the other day and I've hit a bit of a road block.
I'm trying to learn arrays, but my methods are also kinda jacked up.
I know when I invoke my method in my main that it isn't right. I thought I was supposed to give the method that I was invoking the arguments that it needs to carry out its processes?
I'm just trying to call my last method and display the data from that method.
package test_arraymethods;
import java.util.Scanner;
public class TEST_arraymethods
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("------------------------------------------");
System.out.println("Please enter the total number of dealers: ");
System.out.println("------------------------------------------");
int numDealers = input.nextInt();
numDealers = numberOfDealers(numDealers);
System.out.println("--------------------------------------------------------");
System.out.printf("Please enter the required data for each of your dealers: %n");
System.out.println("--------------------------------------------------------");
dataCalculation(numDealers);
//displayTotals(numberOfDealers, dealerNames, dealerSales, commissionRate);
}//main
//METHOD 1
public static int numberOfDealers(int dealers)
{
int results;
Scanner input = new Scanner(System.in);
while(dealers < 0 || dealers > 30)
{
System.out.printf("%nEnter a valid number of dealers: %n");
dealers = input.nextInt();
}
results = dealers;
return results;
}//number of dealers methods
//METHOD 2
public static void dataCalculation(int data)
{
String[] dealerNames = new String[data];
Scanner input = new Scanner(System.in);
System.out.printf("%nEnter the names of the dealers:%n ");
for(int i = 0; i < data; i++)
{
String names =input.nextLine();
dealerNames[i]= names;
}
double[] dealerSales = new double[data];
System.out.printf("%nEnter their sales totals: %n");
for(int i = 0; i < data; i++)
{
double sales = input.nextDouble();
dealerSales[i] = sales;
}
}//data calculations
//METHOD 3
public static double[] commission(double[] dealerSales)
{
//Create array
double[] commissionRate = new double[dealerSales.length];
for(int i = 0; i < dealerSales.length; i++)
{
commissionRate[i] = dealerSales[i];
if(commissionRate[i] > 0 && commissionRate[i] < 5000)
commissionRate[i] = commissionRate[i] * 0.08;
else if(commissionRate[i] > 5000 && commissionRate[i] < 15000)
commissionRate[i] = commissionRate[i] * 0.15;
else if(commissionRate[i] > 15000)
commissionRate[i] = commissionRate[i] * 0.20;
}
return commissionRate;
}//commission method
public static double[] dealershipSales(double[] dealerSales)
{
//Create array
double[] dealershipSalesTotal = new double[dealerSales.length];
for(int i = 0; i < dealerSales.length; i++)
{
dealershipSalesTotal[i] += dealerSales[i];
}
return dealershipSalesTotal;
}//dealership sales
public static double[] dealerSalesAvg(double[] dealerSales)
{
double[] dealerSalesAvgTotal = new double[dealerSales.length];
for(int i = 0; i < dealerSales.length; i++)
{
dealerSalesAvgTotal[i] += dealerSales[i] / dealerSales.length;
}
return dealerSalesAvgTotal;
}//dealership sales averages
public static double[] dealershipTotalCommission(double[] commissionRate)
{
double[] totalCommission = new double[commissionRate.length];
for(int i = 0; i < commissionRate.length; i++)
{
totalCommission[i] += commissionRate[i];
}
return totalCommission;
}//total commission for the dealership
public static void displayTotals(double[] numberOfDealers, double[] dealerNames, double[] dealerSales, double[] commissionRate)
{
for(int i = 0; i < numberOfDealers.length; i++)
{
System.out.println(" " + dealerNames[i]);
System.out.println(" " + dealerSales[i]);
System.out.println(" " + commissionRate[i]);
}
// pass all of your values to this method and then display them
//display the dealer name and amount of sales
//and the amount of commission for all dealers in a tabular format.
}//display totals */
}//class
The reason that you can't get this to work:
displayTotals(numberOfDealers, dealerNames, dealerSales, commissionRate);
is that it is referring to variables that are not in scope. Those variables are all local variables within methods whose calls have finished. If you want the variables to be accessible in another method, they must be declared as (static, in this case) fields.
I have an assignment, I was wondering how I could go about using 2D arrays with another class, I have a class called Die that looks like this:
public class Die
{
private final int MAX = 6; // maximum face value
private int faceValue; // current value showing on the die
public Die()
{
faceValue = 1;
}
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
public void setFaceValue(int value)
{
faceValue = value;
}
public int getFaceValue()
{
return faceValue;
}
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
}
Now in a main method i have to do the following
I have all the other parts done, I just cant seem to figure out this part.
My current code(Not started this part) is below
import java.util.Arrays;
import java.util.Scanner;
class ASgn8
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("How many players? ");
int playerCount = scan.nextInt();
scan.nextLine();
String[] playerNames = new String[playerCount];
int again = 1;
for(int i = 0; i < playerCount; i++)
{
System.out.print("What is your name: ");
playerNames[i] = scan.nextLine();
}
int randomNum = (int)(Math.random() * (30-10)) +10;
}
}
Do any of you java geniuses have any advice for me to begin?
Thanks!
Here is your main method, you just need to update your main method with this one,
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("How many players? ");
int playerCount = scan.nextInt();
scan.nextLine();
HashMap<String, ArrayList<Die>> hashMap = new HashMap<String, ArrayList<Die>>();
int again = 1;
for(int i = 0; i < playerCount; i++)
{
System.out.print("What is your name: ");
hashMap.put(scan.nextLine(),new ArrayList<Die>());
}
for(String key : hashMap.keySet()){
System.out.println(key + "'s turn....");
Die d = new Die();
System.out.println("Rolled : " + d.roll()) ;
hashMap.get(key).add(d);
System.out.println("Want More (Yes/No) ???");
String choice = scan.next();
while(choice != null && choice.equalsIgnoreCase("YES")){
if(hashMap.get(key).size()>4){System.out.println("Sorry, Maximum 5-Try you can...!!!");break;}
Die dd = new Die();
System.out.println("Rolled : " + dd.roll()) ;
hashMap.get(key).add(dd);
System.out.println("Want More (Yes/No) ???");
choice = scan.next();
}
}
for(String key : hashMap.keySet()){
System.out.println(key + " - " + hashMap.get(key));
}
}
EDITED
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("How many players? ");
int playerCount = scan.nextInt(); // get number of participant player...
scan.nextLine();
Die[] tempDie = new Die[5]; // temporary purpose
Die[][] finalDie = new Die[5][]; // final array in which all rolled dies stores...
String [] playerName = new String[playerCount]; // stores player name
int totalRollDie = 0; // keep track number of user hash rolled dies...
for(int i = 0; i < playerCount; i++) // get all player name from command prompt...
{
System.out.print("What is your name: ");
String plyrName = scan.nextLine();
playerName[i] = plyrName;
}
for(int i = 0; i < playerCount; i++){
System.out.println(playerName[i] + "'s turn....");
totalRollDie = 0;
Die d = new Die();
System.out.println("Rolled : " + d.roll()) ;
tempDie[totalRollDie] = d;
totalRollDie++;
System.out.println("Want More (Yes/No) ???");
String choice = scan.next();
while(choice != null && choice.equalsIgnoreCase("YES")){
if(totalRollDie < 5){ // if user want one more time to roll die then first check whether alread user has rolled 5-time or not.
Die dd = new Die();
System.out.println("Rolled : " + dd.roll()) ; // rolled and print whatever value get..
tempDie[totalRollDie] = dd;
totalRollDie++;
System.out.println("Want More (Yes/No) ???");
choice = scan.next();
}
}
finalDie[i] = new Die[totalRollDie];
for(int var = 0 ; var < totalRollDie ; var++){
finalDie[i][var] = tempDie[var]; // store Die object into finalDie array which can random number for all user..
}
}
for(int i = 0 ;i < playerCount ; i++){ // finally print whatever user's roll value with all try...
System.out.println(" --------- " + playerName[i] + " ------------ ");
for(Die de : finalDie[i]){
System.out.println(de);
}
}
tempDie = null;
}
import java.util.Scanner;
class DataInput {
String name[];
int korean[], math[], english[];
int sum[];
double average[];
int students;
int rank[];
public void save() {
Scanner sc = new Scanner(System.in);
System.out.println("Type in number of students");
students = sc.nextInt();
name = new String[students];
korean = new int[students];
math = new int[students];
english = new int[students];
sum = new int[students];
average = new double[students];
rank = new int[students];
for (int i = 0; i < students; i++) {
System.out.println("Type name");
name[i] = sc.next();
System.out.println("Type Korean score");
korean[i] = sc.nextInt();
System.out.println("Type math score");
math[i] = sc.nextInt();
System.out.println("Type English score");
english[i] = sc.nextInt();
sum[i] = korean[i] + math[i] + english[i];
average[i] = sum[i] / 3.0;
}
}
int stu() {
return students;
}
int[] sum() {
return sum;
}
}
class DataOutput {
DataInput data = new DataInput();
int sNum;
int[] rrank, sum;
DataOutput(int students, int[] sum) {
this.sNum = students;
this.rrank = new int[sNum];
this.sum = sum;
}
void ranker() {
int cnt = 1;
for (int i = 0; i < sNum; i++) {
for (int j = 0; j < sNum; j++) {
if (sum[i] < sum[j]) {
cnt++;
}
}
rrank[i] = cnt;
cnt = 1;
}
}
}
public class Score {
public static void main(String[] args) {
DataInput data = new DataInput();
int sNum = data.stu();
int[] sum = data.sum();
DataOutput out = new DataOutput(sNum, sum);
data.save();
out.ranker();
System.out.println();
System.out.println("Name\t\tKorean math English \t sum Average Rank");
System.out
.println("-------------------------------------------------------");
for (int i = 0; i < data.stu(); i++) {
System.out.println(data.name[i] + "\t\t" + data.korean[i] + " "
+ data.math[i] + " " + data.english[i] + "\t"
+ data.sum[i] + " " + data.average[i] + " "
+ out.rrank[i]); // this is where i get an Exception
}
}
}
So, this is my program for getting ranks of students. But somehow when I run the code, I keep getting "OutOfBoundaryException". I checked my code, and realized that when I instantiate a new instance of DataOutput, it creates all new data. So I tried to fix this by setting a constructor. However I still can't solve this matter. I know that I can put the ranker method into DataInput class, and problem will be easily solved however I really want to keep DataOutput class.
Thank you for your time.
PS: Exception is thrown on line 98, out.rrank[i]
Your students field isn't set until the save() method is called. The value of sNum in main is then 0.
Change the order in main to:
DataInput data = new DataInput();
data.save();// <--
int sNum = data.stu();
int[] sum = data.sum();
DataOutput out = new DataOutput(sNum, sum);
out.ranker();
the problem was that you initialize the rank[] before creating students, As soultion I suggest you to initialize after collection students/scores
public void init(int students, int[] sum){
this.sNum = students;
this.rrank = new int[sNum];
this.sum = sum;
}
And update main()
DataOutput out = new DataOutput();
data.save();
int sNum = data.stu();
int[] sum = data.sum();
out.init(sNum, sum);
out.ranker();