I keep getting an "Scanner cannot be resolved to a type" error. I am using Java and Visual Studio and this is for a beginner Java class

  Uncategorised

This is for a beginner java class. I keep getting an "Scanner cannot be resolved to a type" error. I am using Visual Studio and JDK 16.0.2 I also just installed Visual Studio and installed Java on this laptop. I am writing this Mortgage Calculator program for my class and it worked before I added the while loops for catching user input error. Now it gives me the "scanner" error when it didn’t before. I added and moved around a few code lines and all the sudden it gives me this "scanner" error. Below is the whole code:

  package com.phillip;
  import java.text.NumberFormat;
  import java.util.Scanner;

  public class Main{

        public static void main(String[] args) {
              final byte MONTHS_IN_YEAR = 12;
              final byte PERCENT = 100;

              int principal = 0;
              float monthlyInterest = 0;
              int numberOfPayments = 0;

              Scanner scanner = new Scannner(System.in);

              while (true) {
                    System.out.println("Principal: ");
                    principal = scanner.nextInt();
                    if (principal >= 1000 && principal <= 1_000_000) {
                          break;
                    }
                    System.out.println("Enter a value between 1000 and 1000000");
              }

              while (true) {
                    System.out.println("Annual Interest Rate: ");
                    float annualInterest = scanner.nextFloat();
                    if (annualInterest >= 1 && annualInterest <= 30) {
                          monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
                          break;
                    }
                    System.out.println("Enter a value between 1 and 30");
              }

              while (true) {
                    System.out.println("Period (Years): ");
                    byte years = scanner.nextByte();
                    if (years >= 1 && years <= 30) {
                          numberOfPayments = years * MONTHS_IN_YEAR;
                          break;
                    }
                    System.out.println("Enter a value between 1 & 30");
              }

              double mortgage = principal 
                    * (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments)) 
                    / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);

              String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
              System.out.println("Mortgage: " + mortgageFormatted);
        }
  }

Source: Visual Studio Questions

LEAVE A COMMENT