I tried to convert it to char but I'm still getting a problem that it won't build properly, newbie here. But any help would do please
import java.util.Scanner;
public class PizzaChoice {
public static void main(String[] args){
//char sizes;
Scanner input = new Scanner(System.in);
final int NUMBER_OF_CHOICES = 4;
char[] pizzaSize = {'S', 'M', 'L', 'X'};
double[] prices = {6.99, 8.99, 12.50, 15.00};
char sizeChosen;
double pizzaPrice = 0.0;
boolean validChoice = false;
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter pizza size- S, M, L, or X: ");
sizeChosen = inputDevice.nextLine().charAt(0);
for(int s = 0; s < NUMBER_OF_CHOICES; ++s)
{
if(sizeChosen.equals(pizzaSize[s]))
{
validChoice = true;
pizzaPrice = prices[s];
}
}
if(validChoice)
System.out.println("The price for chosen size " +
sizeChosen + " is $" + pizzaPrice);
else
System.out.println("Sorry - Invalid Choice Entered");
}
}
Replace char for pizzaSize and sizeChosen by Character and it will work. See "Char cannot be dereferenced" error
Related
I'm working on a project for school but I can't figure out why my code isn't exiting when I type "zzz". it's probably simple and I'll likely feel dumb when I know what the problem is. here's my code:
import java.util.*;
public class StringSort2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int counter = 0;
String[] arr = new String[15];
System.out.println("Enter item or type 'zzz' to quit");
arr[0] = input.nextLine();
counter++;
do{
arr[counter] = input.nextLine();
if (input.equals("zzz")){
System.out.println("bleh");
}
counter++;
} while (!input.equals("zzz") && counter <= 14);
Arrays.sort(arr);
System.out.println("Array is " + Arrays.toString(arr));
}
Replace
if (arr[counter].equals("zzz")) {
System.out.println("bleh");
}
counter++;
with
if (arr[counter].equals("zzz")) {
System.out.println("bleh");
} else {
counter++;
}
and
while (!input.equals("zzz") && counter <= 14)
with
while (!arr[counter].equals("zzz") && counter <= 14)
Explanation: zzz is a string which you have to compare with the input string stored in arr[counter].
Also, in order to avoid NullPointerException, you should perform sort operation on the copy of the array without any null element. Given below is the complete program:
import java.util.Arrays;
import java.util.Scanner;
public class StringSort2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int counter = 0;
String[] arr = new String[15];
do {
System.out.print("Enter item or type 'zzz' to quit: ");
arr[counter] = input.nextLine();
if (arr[counter].equals("zzz")) {
System.out.println("bleh");
} else {
counter++;
}
} while (!"zzz".equals(arr[counter]) && counter <= 14);
arr = Arrays.copyOf(arr, counter);
Arrays.sort(arr);
System.out.println("Array is " + Arrays.toString(arr));
}
}
A sample run:
Enter item or type 'zzz' to quit: a
Enter item or type 'zzz' to quit: b
Enter item or type 'zzz' to quit: c
Enter item or type 'zzz' to quit: zzz
bleh
Array is [a, b, c]
import java.util.*;
public class StringSort2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int counter = 0;
String[] arr = new String[15];
System.out.println("Enter item or type 'zzz' to quit");
arr[counter] = input.nextLine();
counter++;
do{
arr[counter] = input.nextLine();
if (arr[counter].equals("zzz")){System.out.println("bleh");}
counter++;
}while (!arr[counter].equals("zzz") && counter <= 14);
Arrays.sort(arr);
System.out.println("Array is " + Arrays.toString(arr));
}
}
You meant to compare the input String, not the Scanner object. I also removed your magic number "0" index that you had , since you already set your counter to 0.
The reason your code isn't working is that input is the scanner object, and the equals method on the scanner object doesn't refer to the data being read by it. A way of doing this so that it works would be:
import java.util.*;
public class Help {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int counter = 0;
String[] arr = new String[15];
String inputString = null;
System.out.println("Enter item or type 'zzz' to quit");
do{
inputString = input.nextLine();
arr[counter] = inputString;
if (inputString.equals("zzz")){System.out.println("bleh");}
counter++;
}while (!inputString.equals("zzz") && counter <= 14);
Arrays.sort(arr);
System.out.println("Array is " + Arrays.toString(arr));
}
}
input is a Scanner. It will never equal a String.
Try this
Scanner sc = new Scanner(System.in);
String input; // this is now a proper string to compare
String[] arr = new String[15];
System.out.println("Enter item or type 'zzz' to quit");
input = sc.nextLine();
int counter = 0;
while (counter < arr.length) {
if (input.equals("zzz")) return; // check most recently entered input
arr[counter++] = input; // if not returned, store in the list and increase counter
input = sc.nextLine(); // prompt next line
}
Arrays.sort(arr);
System.out.println("Array is " + Arrays.toString(arr));
Ok, I'm trying to make a simple sink a boat game in java, but am running into a java.lang.numberformatexception and don't know what I'm doing wrong... Here's the actual code I'm using. Sorry very noob still.
It's a simple 7 tiles battlefield with a ship on 3 tiles.
import java.util.Scanner;
public class New4
{
int randnum;
static char[] field;
int numofhits;
String userinput;
private static Scanner userguess;
public static void main(String[] args)
{
new New4();
}
public New4()
{
String input = "";
int numofhits = 0;
int randnum = (int) (Math.random() * 5);
char[] field = new char[7];
for(int i=0;i<7;i++)
{
field[i] = '*';
}
for(int j=0;j<3;j++)
{
field[randnum] = 'O';
randnum++;
}
for(char c : field)
{
System.out.print("|" + c);
}
System.out.print("|");
while(numofhits < 3)
{
AskUser();
input = userinput;
if(field[Integer.parseInt(input)] == 'O'){System.out.println("A terrifc hit");numofhits++;}
else{System.out.println("A blasted miss");}
System.out.println("Great game");}
}
public String AskUser()
{
String userinput;
System.out.println("Guess a number: ");
userguess = new Scanner(System.in);
userinput = userguess.next();
return userinput;
}
}
input = askUser(); //instead of userinput = input;
import java.util.Scanner;
class Digitsdisplay {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a value: ");
int value = input.nextInt();
int total = 0;
String digit = "" + value;
System.out.print("");
for (int i = 0; i < digit.length(); i++) {
int myInt = Integer.parseInt(digit.substring(i, i + 1));
System.out.println(myInt);
total += myInt;
}
}
}
output:
Please enter a value:
789
7
8
9
How do I reverse the output? For example, when I enter the number 123, it would display 321 with each digit on a new line.
If the user is inputting values in base 10, you could instead use the modulo operator along with integer division to grab the rightmost values successively in a while loop as so:
import java.util.Scanner;
public class Digitsdisplay {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a value: ");
int value = input.nextInt();
int quotient = value;
int remainder = 0;
while(quotient != 0){
remainder = quotient%10;
quotient = quotient/10;
System.out.print(remainder);
}
}
}
This might be a better method that attempting to convert the int to a string and then looping through the string character by character.
Loop you printing for loop in the reverse direction:
import java.util.Scanner;
class Digitsdisplay {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a value: ");
int value = input.nextInt();
int total = 0;
String digit = "" + value;
System.out.print("");
for (int i = digit.length()-1; i >= 0; i--) {
int myInt = Integer.parseInt(digit.substring(i, i + 1));
System.out.println(myInt);
total += myInt;
}
}
}
Edit: No reason to reverse the string itself, ie. using a Stringbuilder like the other answers say. This just adds to the runtime.
import java.util.*;
public class Francis {
public static void main(String[] args) {
Scanner input= new Scanner (System.in);
System.out.println("Please enter a value: ");
int value = input.nextInt();
int total = 0;
String digit = "" + value;
System.out.print("");
for (int i = 0; i < digit.length(); i++) {
int myInt = Integer.parseInt(digit.substring(i, i + 1));
System.out.println(myInt);
total += myInt;
}
}
}
Simply reverse the string before looping through it
digit = new StringBuilder(digit).reverse().toString();
I do not know how to display entries entered by user, and also to match and sort them.
import java.util.Scanner;
public class Case1
{
public static void main(String[] args) {
Scanner in=new Scanner (System.in);
System.out.println("Input a size of an array: ");
int size = in.nextInt();
int num[]=new int[size];
int i=0;
for (i=0;i<num.length;i++) {
System.out.println("Input a number: ");
num[i]=in.nextInt();
}
for (int c=0;i<num.length;c++){
for (int a=0; a<num.length;a++){
if(num[c]>num[a]){
int temp = num[c];
num[c]= num[a];
num[a]=temp;
}
}
}
for (int d=0;i<num.length;d++){
int value = 0;
if(value==num[i]) {
System.out.println("Match Found!");
}
}
}
}
help please.
It should look like this ->
import java.util.Scanner;
public class Case1
{
public static void main(String[] args) {
Scanner in=new Scanner (System.in);
System.out.println("Input a size of an array: ");
int size = in.nextInt();
int num[]=new int[size];
int i=0;
for (i=0;i<num.length;i++) {
System.out.println("Input a number: ");
num[i]=in.nextInt();
}
// DISPLAY ENTRIES
System.out.println("You entered the following entries.");
for (int index=0; index<num.length; index++) {
System.out.print(index + ": " + num[index] + " ");
}
// END DISPLAY ENTRIES
for (int c=0;c<num.length;c++){ // Changed i to c
for (int a=0; a<num.length;a++){
if(num[c]>num[a]){
int temp = num[c];
num[c]= num[a];
num[a]=temp;
}
}
}
for (int d=0;d<num.length;d++){ // Changed i to d
int value = 0;
if(value==num[d]) { // Changed i to d
System.out.println("Match Found!");
}
}
}
}
This should work, but when you asked in your question about matching, did you want to match each entry with 0 (which makes no sense) or did you want to compare each entry to 0?
Here is my code:
public class Exercixe09_04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string: ");
String s1 = input.next();
System.out.println("Enter a character: ");
String s2 = input.next();
char c = s2.charAt(0);
int num = count(s1, c);
System.out.print(num);
}
public static int count(String str, char a)
{
int count = 0;
for(int i =0; i < str.length(); i++)
if(str.charAt(i) == 'a'){
count++;
}
return count;
}
}
When I compile and run it, nothing prints. Can someone please help me figure out what is wrong?
import java.util.Scanner;
public class Exercixe09_04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String s1 = input.next();
System.out.print("Enter a character: ");
String s2 = input.next();
char c = s2.charAt(0);
int num = count(s1, c);
System.out.println(num);
}
public static int count(String str, char a) {
int count = 0;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == a) {
count++;
}
return count;
}
}
Sample run:
falsetru#jmlee12:/tmp$ java Exercixe09_04
Enter a string: Hello
Enter a character: l
2