I have a Java Programming question. I have the following code and at Scanner.nex
ID: 3768789 • Letter: I
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 to the nonstatic method nextLine() from the type Scanner" I can't figure out how to correct this.
import java.io.PrintStream;
import java.util.Scanner;
public class QuartsToGallonsWithExceptionHandling {
QuartsToGallonsWithExceptionHandling(){
}
public static void main(String args[]){
int i = 18;
boolean flag = false;
Scanner scanner = new Scanner(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");
}
int j = i / 4;
int k = i / 4;
System.out.println((new StringBuilder()).append("A job that needs ")
.append(i).append(" quarts requires ")
.append(j).append(" gallons plus ")
.append(k).append(" quarts.").toString());
}
}
Explanation / Answer
Correct This : scanner.nextLine();
After CatchException
In Coding Find Scanner.nextLine(); and used scanner in small Letter
import java.io.PrintStream;
import java.util.Scanner;
public class QuartsToGallonsWithExceptionHandling {
QuartsToGallonsWithExceptionHandling(){
}
public static void main(String args[]){
int i = 18;
boolean flag = false;
Scanner scanner = new Scanner(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");
}
int j = i / 4;
int k = i / 4;
System.out.println((new StringBuilder()).append("A job that needs ")
.append(i).append(" quarts requires ")
.append(j).append(" gallons plus ")
.append(k).append(" quarts.").toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.