无法找到char的符号

时间:2016-02-08 16:54:46

标签: 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);

    }

}

我刚刚开始编码,所以如果答案显而易见,我就不会猜到它。

4 个答案:

答案 0 :(得分:1)

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();

答案 1 :(得分:0)

此处的问题是,班级Scanner不包含nextChar()方法。

解决这个问题的方法是从扫描程序中获取一个字符串并检查长度是否等于1(这意味着它只包含一个字符)然后获取此字符。如果您不想在输入包含多个字符时显示错误,则可以避免长度检查。

示例:

Scanner s = new Scanner(System.in);
        String inputString = s.next();
        if(inputString.length() > 1){
            throw new IllegalArgumentException("Only one character can be inputed!");
            //Handle however you want. The exeption is thron as an example.
        }
        char inputChar = inputString.charAt(0);
        //Continue your code :- ) 
祝你好运。

答案 2 :(得分:0)

使用input.next()代替input.nextChar()。 扫描程序不存在nextChar()方法。 input.next()将返回一个字符串。

您必须将char item替换为String item,将item = Character.toUpperCase(item)替换为item = item.toUpperCase()

您还可以在数量和项目之间输入+ " " +以将值与项目分开。

答案 3 :(得分:0)

import  java.io.*;

public class Citaj {

  private InputStream ul;    // File that you are reading from
  private char c;            // Last char that you read
  private boolean eof;       // end of file

  public Citaj (String ime) throws FileNotFoundException  // Open
    { ul = new FileInputStream (ime); }                   //   file.

  public boolean eofF () { return eof; }      // is it end of file?

  public char getChF () {    // get a next char.
    try { int i = ul.read (); return c = (eof = i == -1) ? ' ' : (char)i; }
      catch (Exception g) { eof = true; return c = ' '; }
  }

  public char CharF () {     // reading one non-white char.
    while (Character.isWhitespace (c = getChF ()));
    return !eof ? c : ' ';
  }

  public String StringF () { // read one string.
    String s = "";
    while ( Character.isWhitespace (c = getChF ()) && !eof);
    if (eof) return "";
    s += c;
    while (!Character.isWhitespace (c = getChF ()) && !eof) s += c;
    eof = false;
    return s;
  }

  public String LineF () {    // read one line
    String s="";
    while ((c = getChF ()) != '\n' && !eof) if (c != '\r') s += c;
    if (s.length () != 0) eof = false;
    return s;
  }

  public void getNLF ()      
    { while (c!='\n' && !eof) c = getChF (); c = '\0'; }

  public byte   ByteF    ()  // read one byte
    { String s = StringF (); return !eof ? Byte.parseByte (s) : 0; }

  public short  ShortF   ()  // read one short
    { String s = StringF (); return !eof ? Short.parseShort (s) : 0; }

  public int    IntF     ()  // read one int
    { String s = StringF (); return !eof ? Integer.parseInt (s) : 0; }

  public long   LongF    ()  // read one long
    { String s = StringF (); return !eof ? Long.parseLong (s) : 0; }

  public float  FloatF   ()  // read one float
    { String s = StringF (); return !eof ? Float.parseFloat (s) : 0; }

  public double DoubleF  ()  // read one double
    { String s = StringF (); return !eof ? Double.parseDouble (s) : 0; }

//  public boolean BooleanF()  // read one boolean
//    { String s = StringF (); return !eof ? Boolean.parseBoolean (s) : false; }

  // Support for reading from console:

  private Citaj () { ul = System.in; }      // private constructor

  private static Citaj gl = new Citaj ();   

  public static boolean eof    () { return gl.eofF    (); } // Variations:
  public static char    getCh  () { return gl.getChF  (); } //   
  public static char    Char   () { return gl.CharF   (); } //   
  public static String  String () { return gl.StringF (); } //   
  public static String  Line   () { return gl.LineF   (); } //   
  public static void    getNL  () {        gl.getNLF  (); } //   
  public static byte    Byte   () { return gl.ByteF   (); }
  public static short   Short  () { return gl.ShortF  (); }
  public static int     Int    () { return gl.IntF    (); }
  public static long    Long   () { return gl.LongF   (); }
  public static float   Float  () { return gl.FloatF  (); }
  public static double  Double () { return gl.DoubleF (); }
//  public static boolean Boolean() { return gl.BooleanF(); }
}

所以一般来说,只需导入给定的类,并使用它:Citaj.Char()。 您也可以根据自己的喜好重命名Citaj课程。)