Just started learning Java a week or so ago. I have made a little Java program that calculates the amount of reviews, and the average review rating that something has. For instance, on the app store, you would enter in 1 star, 2 star, 3 star, 4 star, and 5 star reviews and the program would calculate the average review score. Anyway, maybe it would just be easier to show you. What I want to do is have the keyboard input section in a different class. But when I tried to do this, the compiler said "cannot find symbol" I know there is no global variables in Java, but surely there is a way to pass the user-input variable into a different class(?). I have tried the "extends" keyword to make my keyboard class a subclass, but that didn't work either. I essentially need to access the firStar, secStar, thrSstar, fouStar, and fivStar variables in the reviewHandler class, when those variables are originally defined in the keyboardInput class.
reviewHandler class:
public class reviewHandler {
public void reviews() {
keyboardInput keyboard = new keyboardInput();
keyboard.fiveStar();
int oneStar = firStar;
int twoStar = secStar;
int threeStar = thrStar;
int fourStar = fouStar;
int fiveStar = fivStar;
int starOne = oneStar * 1;
int starTwo = twoStar * 2;
int starThree = threeStar * 3;
int starFour = fourStar * 4;
int starFive = fiveStar * 5;
int reviewCount = oneStar + twoStar + threeStar + fourStar + fiveStar;
double reviewStarCount = starOne + starTwo + starThree + starFour + starFive;
double reviewAverage = reviewStarCount / reviewCount;
reviewAverage = reviewAverage * 100;
reviewAverage = Math.round(reviewAverage);
reviewAverage = reviewAverage / 100;
System.out.println("Total reviews: " + reviewCount);
System.out.println("Average review score: " + reviewAverage);
}
}
keyboardInput class:
import java.util.Scanner;
public class keyboardInput extends reviewHandler {
public void fiveStar() {
Scanner stars = new Scanner(System.in);
int firStar, secStar, thrStar, fouStar, fivStar;
System.out.println("Number of 1 star reviews: ");
firstar = stars.nextInt();
System.out.println("Number of 2 star reviews: ");
secstar = stars.nextInt();
System.out.println("Number of 3 star reviews: ");
thrstar = stars.nextInt();
System.out.println("Number of 4 star reviews: ");
foustar = stars.nextInt();
System.out.println("Number of 5 star reviews: ");
fivstar = stars.nextInt();
}
}
reviewLauncher class:
public class reviewLauncher {
public static void main (String[] args) {
reviewHandler start = new reviewHandler();
start.reviews();
}
}
EDIT: If I move the firStar, secStar, thrSstar, fouStar, and fivStar variables out of the method, transforming them into instance variables, the program compiles and runs, but the program will return a 0 when I try to use it.