Program still picks up letter, when I specifically tell it not to - java

In one of the lines, at least. Heres the whole code.
import java.util.Scanner;
public class TestChar {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String myChar ;
System.out.println(" Please input a character from your keyboard : ");
myChar = input.nextLine();
char c = myChar.charAt(0);
System.out.println(" You entered " + myChar);
System.out.println(" is it a letter? " + Character.isLetter(c));
System.out.println(" is it a number? " + Character.isDigit(c));
System.out.println(" is it in lowercase? " + Character.isLowerCase(c));
System.out.println(" is it in uppercase? " + Character.isUpperCase(c));
if (Character.isLetter(c))
System.out.println(" In Upper Case : " + Character.toUpperCase(c) + ". And in lower case : " + Character.toLowerCase(c)) ;
if
(Character.isDigit(c) && !myChar.equals(Character.isLetter(c)))
System.out.println( myChar + " is a number.") ;
if (!myChar.equals(Character.isLetter(c)) && !myChar.equals(Character.isDigit(c)))
System.out.println( myChar + " is neither a number nor letter.");
}
}
Another school assignment of mine. The last line of code picks up a letter, when (I think) I've specifically told it not too. I am an extreme novice at coding, so go easy on my code.

Character.isLetter() and Character.isDigit() return a boolean value, which you're subsequently comparing to myChar, which is (somewhat misleadingly) a String. Obviously, they will never be equal.
Here's a correct and simplified version:
if (Character.isLetter(c)) {
System.out.println(" In Upper Case : " + Character.toUpperCase(c) + ". And in lower case : " + Character.toLowerCase(c));
} else if (Character.isDigit(c)) {
System.out.println(c + " is a number.");
} else {
System.out.println(c + " is neither a number nor letter.");
}

Related

Using a loop to figure out the number of tries left

I have this celebrity guessing game where the user has to guess a celebrity’s name, given only a portion of the letters in the name.
I give the player the “clue” (e.g.rge oney) and read in their guess. The program should have a loop that allows them to keep guessing. If they guess incorrectly 3 times, give them a hint If they guess incorrectly a fourth time (after the hint), they lose the game (and you should tell them who the celebrity was).
I'm having trouble with the loop. this is what I have so far.
System.out.println("Celebrity Guessing Game");
String celeb = "John Lennon";
System.out.print("Choose your difficulty (easy/medium/hard): ");
String difficulty = input.nextLine();
int maxtry = 3;
if (difficulty.equals("easy"))
{
System.out.println("Here is your clue: " + celeb.substring(1, 4) + " " + celeb.substring(5,10));
}
else if (difficulty.equals("medium"))
{
System.out.println(("Here is your clue: " + celeb.substring(0, 3) + " " + celeb.substring(4,9)));
}
else if (difficulty.equals("hard"))
{
System.out.println(("Here is your clue: " + celeb.substring(2, 4) + " " + celeb.substring(5,7)));
}
System.out.print("What is your guess? ");
String guess1 = input.nextLine();
System.out.println("guess1 = " + guess1 + " celeb = " + celeb );
while (!guess1.equals(celeb) && maxtry == 3 ) {
if (!guess1.equals(celeb) && maxtry == 3) {
maxtry--;
System.out.println("Try Again." + " Number of guesses left : " + maxtry);
}
if (guess1.equals(celeb) || guess1.equals("john lennon")) {
System.out.println("Good Guess, you are correct!");
}
This is my output:
Celebrity Guessing Game
Choose your difficulty (easy/medium/hard): easy
Here is your clue: ohn Lenno
What is your guess? john lennon
guess1 = john lennon celeb = John Lennon
Try Again. Number of guesses left : 2
Good Guess, you are correct!
^ Why is it going through both if statements??
The problem is in checking condition. It have following problems:
It should be maxtry > 0 instead of maxtry == 3
Instead of equals() use equalsIgnoreCase()
Following is corrected code snippet:
while (!guess1.equalsIgnoreCase(celeb) && maxtry > 0 ) {
if (!guess1.equalsIgnoreCase(celeb) && maxtry > 0) {
maxtry--;
System.out.println("Try Again." + " Number of guesses left : " + maxtry);
}
Note: You are not reading input from user for other tries.

How can i make a loop for my code so it runs until user gives correct input?

I am wondering how I could make this code loop whenever the user gives a number outside of what the operator variable is asking. I am open to different suggestions. I have tried and failed many times using the do while loop. I want the code to say "Choose a number between 1-4" if the user gives the wrong number and then I want the loop back to the operator variable until the user gives a correct number and after the correct answer is given I want the program to go though the rest of the code and close.
import static java.lang.System.*;
import static javax.swing.JOptionPane.*;
import static java.lang.Integer.*;
public class SimpleCalc {
public static void main(String[] args) {
do {
String operator = showInputDialog("Choose operation: " + "\n" +
"[1] = Plus" + "\n" +
"[2] = Minus" + "\n" +
"[3] = Multiply" + "\n" +
"[4] = Divide" + "\n");
int c = parseInt(operator);
if (c>4 || c<1) {
showMessageDialog(null, "Choose a number between 1 - 4.");
}
else{
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
switch(c) {
case 1:
showMessageDialog(null, a + " + " + b + " = " + (a+b));
break;
case 2:
showMessageDialog(null, a + " - " + b + " = " + (a-b));
break;
case 3:
showMessageDialog(null, a + " * " + b + " = " + (a*b));
break;
case 4:
showMessageDialog(null, a + " / " + b + " = " + (a/b));
break;
}
}
} while (c>4 || c<1);
}
}
Move the declaration of c out of do block, otherwise it is not accessible in while
int c;
do {
...
c = parseInt(operator);
...
} while (c > 4 || c < 1);
You are on the right track by using do while loop. However the value c is not seen as its within the do while block. if you add int c above the do block and make int c = parseInt(operator); to c = parseInt(operator); it will work

Write a program that receives a character and displays its Unicode

import java.util.Scanner;
public class Ex4_9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter character");
String a = input.nextLine();
char ch = a.charAt(0);
if (a.length() == 1){
System.out.println("The character entered is " + ch);
System.out.println(" the Unicode for character " + ch + " " + ??);
}
else
System.out.println("complain about the number of characters.");
}
}
I want to be able to enter E and java display 69. what do i need to fill in for the ??
You want to use codePointAt...
System.out.println(" the Unicode for character " + a + " " + a.codePointAt(0));
Simply cast ch to int to get its Unicode value, as in
System.out.println(" the Unicode for character " + ch + " " + ((int) ch));
As for comments, this will work for any char, but not any Unicode code point. However, the question asks for a solution for a char, which mine works for. ch is initialised as a.charAt(0), which doesn't work for surrogates anyway, so I don't see any reason in the downvote.

Java calulator throws -bad operand types for binary operator '-'-

I get this error; bad operand types for binary operator '-'
All other operations work...when I leave out the subtraction via comments like // and /* */; can someone help?
This is the code by the way; the exception is on the subtraction line.
public class Calculator {
/*
*use 'javac Calculator.java' to compile;
*use 'jar cvf Calculator.jar Calculator.class' for jar;
*use 'java Calculator' to run;
*/
public static void main(String []args) {
String NewName
Scanner user_input = new Scanner( System.in );
System.out.println("Type your name please.");
NewName = user_input.next();
System.out.println("");
System.out.println("Hello " + NewName + ".");
System.out.println("I am Hunter's java calculator program.");
System.out.println("");
//mathematical input
String operator;
float cal1, cal2;
System.out.println("Type a Number...");
cal1 = user_input.nextFloat();
System.out.println("");
System.out.println("Type another Number...");
cal2 = user_input.nextFloat();
System.out.println("");
Scanner opt = new Scanner(System.in);
System.out.println("Enter an operator");
operator = opt.next();
//operation decisions
if (operator.equals("+")){
System.out.println("The answer is " + cal1+cal2 + ".");
}
if (operator.equals("-")){
System.out.println("The answer is " + cal1-cal2 + ".");
}
if (operator.equals("/")){
System.out.println("The answer is " + cal1/cal2 + ".");
}
if (operator.equals("*")){
System.out.println("The answer is " + cal1*cal2 + ".");
}
}
}
You need parenthesis:
System.out.println("The answer is " + (cal1-cal2) + ".");
Otherwise what you have is treated as
System.out.println(("The answer is " + cal1) - (cal2 + "."));
which is invalid since you can't subtract strings.
Why don't you have an error with the other operators? Well, * and / have higher precedences, so those are working as expected. +, on the other hand, is overloaded to concatenate strings:
System.out.println("The answer is " + cal1+cal2 + "."); // concatenates, doesn't add
For example, if call1 is 1 and call2 is 2, the result will be:
The answer is 12.
which isn't what you want. Again, this can be solved with parenthesis.

an array does not get a value

I have a problem with the following code
public static void SideBet(int numberDice,int bet,int money) {
System.out.println("You established a " + "\""+ "point" + "\"" + ". " + "Your " + "\""+ "point" + "\"" + " is " + numberDice + ". " + "You have to roll a(n) " + numberDice + " to win your bet, "+ bet +" chips." );
System.out.println();
System.out.println("You can put side bets for 4,5,6,8,9 or 10.");
SideBetChoice = Console.readLine("Would you like to make any side bets ? (Type " + "\""+ "Yes" + "\"" + " or "+ "\""+ "No" + "\"" + ", then hit Enter.)");
int s = 0;
int r = 0;
if (SideBetChoice.equals("Yes")) {
System.out.println("You can put as many side bets as you would like for the numbers 4,5,6,8,9 or 10.");
int SideBetNumber = Console.readInt("How many side bets would you like to make ? (Introduce a number, minimum 1, maximum 6.)");
int[] SBNArray = new int[SideBetNumber];
int[] sbArray = new int[SideBetNumber];
for (s = 0; s <= (SideBetNumber -1) ; s++) {
SBNArray[s] = Console.readInt("On which number would you like to put a side bet ?");
sbArray[s] = Console.readInt("Currently you have " + money + " chips, how much would you like to bet ?");
money = money - sbArray[s];
System.out.println("Thank you for your " +sbArray[s]+ " chip side bet on number " +SBNArray[s]+".");
System.out.println();
}
}
if (SideBetChoice.equals("No")) {
return;
}
sbArray and SBNArray does not get a value and it keeps crashing ...
Can anyone help me out and tell me what is wrong, why the 2 arrays do not get a value, therefor they are null ?
There is no readInt()-method in Console.
Also I'm not sure if you're using console correctly, it should look like this:
Console console = System.console();
console.readLine("Type something");
Just use readLine() and convert it to an int:
Console console = System.console();
String input = console.readLine("Type a number");
try
{
int myNumber = Integer.parseInt(input);
}
catch(NumberFormatException e)
{
System.out.println("This ain't a number!");
}
Also please never use Capital Letters for Variables' Names or method-names, it's very confusing because you could think it would be a Class or a Type.
So please change the name of SBNArray and SideBetNumber, SideBetChoice etc. etc.
Only Constants should be written with only Capital Letters and Classes and Types start with Capital Letters.
EDIT:
Sorry, it seems that you're using BreezyGUI.Console, therefore there is a readInt()-method.
Could you give more information?
I'd like to know if the text of the readInt() is even displayed.

Categories