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