Beatz February 2016

Cannot find Symbol for char

School assignment, so this code is meaningless. Whenever I try to use a char, I always seem to get this error

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

Heres the actual code :

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

    }

}

I am just beginning to code, so if the answers obvious, I wouldn't have guessed it.

Answers


Idos February 2016

Since nextChar does not exist, I will offer you to consider trying the following:

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

Edit: from what I understand, you want this:

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

This will take a String (item name) from the user, and make the first letter uppercase.

If you want to make sure that all the other letters are lower case, then use:

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

Edit #2: To capitalize the entire word:

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


fillpant February 2016

The problem here is that the class Scanner does not contain a nextChar() method.

What you can do to resolve this is to get a String from the Scanner and check if the length is equal to 1 (that means it only contains one character) and then get this character. The length check can be avoided if you dont want to show an error in case the input has more than one characters.

Example:

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 :- ) 

Good luck.


S.Klumpers February 2016

Use input.next() instead of input.nextChar(). The nextChar() method does not exist for a Scanner. input.next() will return a String.

You'll have to replace char item with String item and item = Character.toUpperCase(item) with item = item.toUpperCase().

Also you could enter + " " + between quantity and item to separate the value from the item.


Mleach February 2016

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

Post Status

Asked in February 2016
Viewed 2,107 times
Voted 8
Answered 4 times

Search




Leave an answer