How to solve the problem of program displaying an error when inputting additional data? [closed]

Program to calculate the area of a graphic.

The program can calculate the area of circles, rectangles, and squares. When running, first input the type of shape (1 represents circle, 2 represents rectangle, 3 represents square), and then determine the type of shape input by the user. Enter the radius for circles, length and width for rectangles, and side length for squares,The data types of the above parameters are all integers.

After calculating the area, display it with two decimal places and set PI to 3.14.
The input is divided into two parts, the first number is 1 representing a circle, 2 representing a rectangle, 3 representing a square, and then the corresponding parameters required for calculating the area.

Request to output the area of the graph on a separate line for each test instance. If the graphic type entered by the user is invalid, an error message will be given:“Invalid Data!”

My solving code:

import java.util.Scanner;
public class Main {


public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    int a = in.nextInt();
    if(a==1){
        int b= in.nextInt();
        System.out.printf("%.2f",3.14*b*b);
    }
    else if(a==2) {
        double b= in.nextDouble();
        double c = in.nextDouble();
        System.out.printf("%.2f", b*c);
    }
    else if(a==3) {
        Double b= in.nextDouble();
        System.out.printf("%.2f", b * b);
    }
    else{
        System.out.print("Invalid Data!");}

    }
}

Although I have completed this task. But if I input some data but cannot get the correct results, it makes me feel a lack of skills:

If input 3 2 2, the program outputs 4.00.

This is not right. I tried in. close() under the guidance of my teacher, but it was unsuccessful. How should it output Invalid Data when inputting 3 2 2! What about it? I can’t solve it, I have to publish the problem.

New contributor

hongsen zhang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

The Scanner class reads an entire line (everything the user has entered before pressing ENTER) and then scans this line for “tokens” (numbers, strings, etc). In other words, your code won’t get any number until the user has entered the entire line and pressed ENTER.

Let’s take your 3 2 2 case as an example. The scanner will read an entire line into a buffer (as a side effect of your a = in.getInt() call), and then it will scan this line, from left to right, looking for the actual integer (the 3). After that, your call to b = in.getDouble() will scan the rest of the same line looking for one double (the first 2).

Any “extra” values at the end of the line will just be ignored. (In this case, 3 2 2 will be a valid input, and the last 2 will just be ignored.) This practice is quite common (and usually acceptable) when using a Scanner.

An actual problem may appear if you want to loop, doing several calculations (one square, one circle, another square, etc), as the extra values at the end of one line will be wrongly interpreted as part of the next calculation. (In your 3 2 2 example, the 3 2 would be interpreted as an order to calculate a square of size 2, and the final 2 would be interpreted as an order to calculate a rectangle, whose input values will be looked for in the next line.)

To avoid this potential problem (or if you’re just uncomfortable ignoring extra values), do what you’re doing but, after reading all required values (for the square, just an integer and a double), advance the scanner to the next line using nextline(). You can also check the return of this function, which will tell you if there was something “extra” at the end of the line (before advancing the scanner to the next one).

Try adding this code at the end of your function:

    String extra = in.nextLine();
    if (! extra.equals(""))
        System.out.printf("nExtra input ignored: %sn", extra);

Note: after calling close(), the scanner won’t allow you to read any more lines, but extra input at the end of already read lines won’t be “rejected”. It’s not what you’re looking for.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật