Question: I have a Java Programming question. I have the following code and at Scanner.nextLine(); it is telling me that I Cannot make a static reference

I have a Java Programming question. I have the following code and atScanner.nextLine();it is telling me that I \"Cannot make a static reference to the nonstatic method nextLine() from the type Scanner\" I can't figure out how to correct this.

importjava.io.PrintStream;

importjava.util.Scanner;

publicclassQuartsToGallonsWithExceptionHandling {

QuartsToGallonsWithExceptionHandling(){

}

publicstaticvoidmain(String args[]){

inti = 18;

booleanflag =false;

Scanner scanner =newScanner(System.in);

while(!flag)

try{

System.out.println(\"enter quarts needed >> \");;

i = scanner.nextInt();

flag =true;

}

catch(Exception exception){

Scanner.nextLine();

System.out.println(\"Invalid data entry\");

}

intj = i / 4;

intk = i / 4;

System.out.println((newStringBuilder()).append(\"A job that needs \")

.append(i).append(\" quarts requires \")

.append(j).append(\" gallons plus \")

.append(k).append(\" quarts.\").toString());

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!