0

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.

cheb1k4
  • 2,316
  • 7
  • 26
  • 39
Beatz
  • 99
  • 1
  • 2
  • 11

4 Answers4

2

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();
Idos
  • 15,053
  • 14
  • 60
  • 75
  • But this only capitalizes the first letter of the "item". The "item" is going to be a whole word, not just a letter. – Beatz Feb 08 '16 at 17:14
  • Capitalize? What exactly are you trying to achieve??? You said you are trying to read a character from the `Scanner`, well this does that... – Idos Feb 08 '16 at 17:15
  • Well Id like a good grade, for one. Im trying to get the code to print out how much an item would be, how many items there are, and to capitalize the item in the final println. Its part of the assignment. – Beatz Feb 08 '16 at 17:17
  • Well then you don't need `nextChar` you just need `next`, since you want a `String` not a `Character`. See my edit – Idos Feb 08 '16 at 17:18
  • Sorry, I didnt make it clear. The whole word has to be in uppercase, not just the first letter. – Beatz Feb 08 '16 at 17:28
  • Wow okay then it's extremely simple, i will edit again.. – Idos Feb 08 '16 at 17:29
  • Alright, that works. Thanks a bunch! – Beatz Feb 08 '16 at 17:33
  • No problem good luck in your class – Idos Feb 08 '16 at 17:35
0

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.

fill͡pant͡
  • 1,147
  • 2
  • 12
  • 24
0

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.

S.Klumpers
  • 410
  • 3
  • 14
  • When I tried item = String.toUpperCase(item) , it gave me this : LetsGoShop.java:21: error: incompatible types: String cannot be converted to Locale item = String.toUpperCase(item); – Beatz Feb 08 '16 at 17:19
  • Oops, try this then. I edited it. toUpperCase() should indeed be called from item withouth any arguments. – S.Klumpers Feb 08 '16 at 17:38
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(); }
}

So in general, just import given class, and use it like this: Citaj.Char(). Also you can rename class Citaj by your taste:)

Mleach
  • 7
  • 4