java - 找不到 char 的符号

标签 java char cannot-find-symbol

学校作业,所以这段代码没有意义。每当我尝试使用 char 时,我似乎总是收到此错误

LetsGoShop.java:14: error: cannot find symbol
                       item = input.nextChar();
                                   ^
  symbol:   method nextChar()
  location: variable input of type Scanner
  1 error

下面是实际代码:

import java.util.Scanner;

public class LetsGoShop {

    public static void main(String[] args) {

        java.util.Scanner input = new java.util.Scanner(System.in);

        char item ;
        int price;
        int quantity;

        System.out.println(" Enter the name of the item : ");
        item = input.nextChar();
        System.out.println(" Enter the price of said item : ");
        price = input.nextInt();
        System.out.println(" Enter how much of said item you want to buy : ");
        quantity = input.nextInt();

        double total = price * quantity ;
        item = Character.toUpperCase(item);

        System.out.println(" You owe " +total+ " for " +quantity + item);

    }

}

我刚刚开始编码,所以如果答案很明显,我就不会猜到。

最佳答案

nextChar does not exist ,我建议您考虑尝试以下操作:

char item;
item = input.next().charAt(0);

编辑:据我了解,您想要这个:

String item = input.next();
String newItem = input.substring(0, 1).toUpperCase() + input.substring(1);

这将从用户处获取一个String(项目名称),并将第一个字母设为大写。

如果您想确保所有其他字母均为小写,请使用:

String newItem = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();

编辑#2:整个单词大写:

String item = input.next().toUpperCase();

关于java - 找不到 char 的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35274977/

相关文章:

java - JFace 向导的默认大小是多少?怎么知道呢?

c++ - 随机打印出每个字符

c++ - 未找到来自共享库的符号,但已定义

java - 编译Java代码时找不到符号

java - 产品风格和来源层次结构

java - Tomcat 9 中的编码问题 "The valid characters are defined in RFC 7230 and RFC 3986"

java - jackson 的自定义命名策略和 @JsonProperty

javascript - charCodeAt 和 fromCharCode 不返回相同的字符

c++ - 为什么将 char 指针流式传输到 cout 不打印地址?

java - 在另一个类中编译时无法识别一个类的对象