Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have used this scanner method before but I dont understand why I am getting a

ID: 3737985 • Letter: I

Question

I have used this scanner method before but I dont understand why I am getting  a error for the Scanner.in= new Scanner(System.in); and also the other .in methods

import java.util.Scanner;

public class LoopDemo {

public static void main(String[] args) {

int score,n=0, total=0, na=0, nb=0,nc=0,nd=0, nf=0;

double avg;

Scanner.in= new Scanner(System.in);

System.out.print("Enter Score: ");

score=in.nextInt();

while(score!=-999) {

if(score>=0 && score <=100) {

total+=score;

++n;

if(score>=90)

++na;

else if(score>=80)

++nb;

else if(score>=70)

++nc;

else if(score>=60)

++nd;

else

++nf;

}

if(n==0)

System.out.println("There were no valid scores were entered.");

else {

avg=(double)total/n;

System.out.printf("The class average is %.2f.%n",avg);

System.out.println("Counts;");

System.out.println("A: "+na);

System.out.println("B: "+nb);

System.out.println("C: "+nc);

System.out.println("D: "+nd);

System.out.println("F: "+nf);

}

in.close();

}

}

}

Explanation / Answer

Hi

I have fixed the issue and highlighted the code changes below

LoopDemo.java

import java.util.Scanner;

public class LoopDemo {

public static void main(String[] args) {

int score,n=0, total=0, na=0, nb=0,nc=0,nd=0, nf=0;

double avg;

Scanner in= new Scanner(System.in);

System.out.print("Enter Score: ");

score=in.nextInt();

while(score!=-999) {

if(score>=0 && score <=100) {

total+=score;

++n;

if(score>=90)

++na;

else if(score>=80)

++nb;

else if(score>=70)

++nc;

else if(score>=60)

++nd;

else

++nf;

System.out.print("Enter Score: ");

score=in.nextInt();

}
}
if(n==0)

System.out.println("There were no valid scores were entered.");

else {

avg=(double)total/n;

System.out.printf("The class average is %.2f.%n",avg);

System.out.println("Counts;");

System.out.println("A: "+na);

System.out.println("B: "+nb);

System.out.println("C: "+nc);

System.out.println("D: "+nd);

System.out.println("F: "+nf);

}

in.close();

}

}

Output:

Enter Score: 11
Enter Score: 22
Enter Score: 33
Enter Score: 44
Enter Score: -999
The class average is 27.50.
Counts;
A: 0
B: 0
C: 0
D: 0
F: 4