Boolean value not working, need a fix? - java

What I want is to output A or Z.
Everything else between A and Z works except A and Z, I tried to add an "else if"
with equals and && but it's not working.
The "System.out.println(ConvertLetterToDigit(num2));"
takes the input and transfers it into a switch that'll give back an "int" value.
If you need more infos, please comment below ._.
char num1;
char num2;
String num3;
String equality;
System.out.println("\nThis game consists of inputting a digit and we will output the character linked to that digit.");
do
{
System.out.println("Please input a letter.");
equality= "^[a-zA-Z](1)";
num3 = console.next();
if (num3.length() < 2);
{
if (num3.matches(equality))
{
num2 = (char)num3.charAt(0);
System.out.println(ConvertLetterToDigit(num2));
}
else if (num3.length() > 1)
{
System.out.println("We said 1 letter.");
}
else
{
System.out.println("What do you think you're doing.");
}
}
} while (!num3.matches(equality));
break;

If I understand correctly, you want to check that your input is indeed a letter between a and z.
In that case, your if should be:
if( num2 >= 'a' && num2 <= 'z') {
// okay, do something
}
else {
// show error message
}

The condition you've written is always false: you require num2 to be equal to 'a' AND to 'z'. You should probably rewrite it as num2 >= 'a' && num2 <= 'z' (note the less/greater signs change).
Excluding 'a' and 'z' is as simple as changing >= to >, and <= to <.
&& means AND, || means OR. Use them appropriately. (A condition that matches both 'a' and 'z' would be num2 == 'a' || num2 == 'z'.)

Related

Count Vowels and Consonants

I was exploring this code which gives a count of vowels and consonants, but didn't understand this else if (ch >= 'a' && ch <= 'z') line of code. Please tell me what's the logic behind it.
import java.util.Scanner;
public class Vowels {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter string");
String str = sc.nextLine();
int vowl = 0;
int conso = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowl++;
} else if (ch >= 'a' && ch <= 'z') {
conso++;
}
}
System.out.println(vowl);
System.out.println(conso);
}
}
A benefit of chars is that you can operate with them like if they were integers.
For example, you can do you this as well 'a' + 3 = 'd'
Meaning that 'a' < 'd' = true.
notice the if statement catches all vowels
whats ever is not a vowel will either be a capital letter, a number, a special character or consonants
else if (ch >= 'a' && ch <= 'z')
this checks if its not a vowel does it atleast fall in the range of small letter 'a'-'z' and is not a special charecter or a number.( we knonw its not a vowel but is it in the ascii range 26=a -51=z)
refer to the ASCII table to understand the range comparison
The comparison of characters the way it is done can create confusion, as you can see from Java: Character comparison.
Basically #TDG is correct by saying that ch is checked to be between 'a' and 'z', and thus the check might be translated as "is ch a lower case character?"
The tricky part is that depending on the language people use the expectation can be different, especially since language specific characters are not taken into account. In German language, 'รถ' would definitely qualify as lower case character but is not in the range of the check. The complexity may get evident by studying the Unicode code charts.
The best check is to use Character.isLowerCase().
char is a character that represented by a number which is the index of the character in the ASCII/unicode table, since the the alphabet characters are arranged in order in the ASCII table, the following code checks if the ch is in the range of the lowercase alphabet characters representation which is 97 to 122 in the table.
using (int) ch you can see the decimal value of the character and can compare it with the index in the ASCII table.
you can see the ASCII table here:https://www.asciitable.com/

How to define 00-90 when I have charAt each of it

What I need to do is write a program that makes the first character (which is charAt(0) )and the second character (which is charAt(1) ) to become a value that not exceeding 90 which is (0 ~ 90) , but I also have to define them as an independent digit , because my program will make it to invalid if it is other than a digit.
So for an example it will become invalid if I type in 91
and it will valid if I type in number between 0~90
but I have no idea how to do this...
if(Character.isDigit(loop1.charAt(0))&&
Character.isDigit(loop1.charAt(1)))
I have tried this ,but not working
if(Character.isDigit(loop1.charAt(0)) &&
Character.isDigit(loop1.charAt(1)) &&
((loop1 >= 0)&&(loop1 <= 90)))
also this one but this is not working( I have no idea what I'm doing)
if(Character.isDigit(loop1.charAt(0)) &&
(((int)loop1.charAt(0)) >= 0) && <=9
Character.isDigit(loop1.charAt(1)) &&
((int)loop1.charAt(1)) <= 9)
Please help me... thanks a million !
Assuming I understand your question, parse loop1 and test the values using a simple if check, like
int t = Integer.parseInt(loop1);
if (t < 0 || t > 90) {
System.out.println("Value outside accepted range.");
} else {
System.out.println("Value valid.");
}
If I am getting this right you want to convert the first two characters of a string into a number and check is that number bigger than 90. Also you want the digits to be stored in different variables(?). If so this code should do it:
int digit1 = loop1.charAt(0) - '0';
int digit2 = loop1.charAt(1) - '0';
int number = digit1 * 10 + digit2;
if ( number <= 90 && number >= 0 )
System.out.println("Input is good");
else
System.out.println("Input is bad");

How to properly use substring

public static String updatePartialword(String partial, String secretWord, char guess){
char achar = secretWord.charAt(0);
char bchar = secretWord.charAt(1);
char cchar = secretWord.charAt(2);
char dchar = secretWord.charAt(3);
char echar = secretWord.charAt(4);
if (achar == guess);{
partial = guess + partial.substring(1,4);
}if (bchar == guess);{
partial = partial.substring(0)+ guess + partial.substring(2,4);
}if (cchar == guess);{
partial = partial.substring(0,1)+ guess + partial.substring(3,4);
}if (dchar == guess);{
partial = partial.substring(0,2)+ guess + partial.substring(3);
}if (echar == guess);{
partial = partial.substring(0,3)+ guess;
}
This is returning values like "aaaa", a being the value that was input. The initial value for partial is "-----". This is kind of like wheel of fortune. So when a user enters "a", the result should be something like "-a---" Thanks.
Enter your guess:
a
Character a appears 1 time(s)
You now have 150 dollars
a----
You have two options:
a) guess a character
b) buy a character
Type a or b
a
You chose to guess a character
Rolling Dice
Outcome is 0
Enter your guess:
n
Character n appears 1 time(s)
You now have 150 dollars
a----n---
You have two options:
a) guess a character
b) buy a character
Type a or b
Others have answered the question, but have in mind also that you should do
partial = guess + partial.substring(1,5);
because the ending position you specify is not included in the substring, so with your code it'd get the positions 1-3.

Cannot get program to recognize capital letters when input?

Java beginner here - hoping someone can help me solve this problem. I am trying to write a simple program that will display a particular message depending on the character entered by the user. The problem I'm having is that it won't recognize the validity of capitalized characters in determining which message to print.
The code compiles OK but if I enter a capital letter, it prints out the message telling me it is not a valid character to start an identifier.
Here is a snippet of the source code:
choice2 = (char) System.in.read();
if(choice2 == 'q')
break;
else
if(choice2 == '_' || choice2 >= 'a' && choice2 <= 'z' && choice2 >= 'A' && choice2 <= 'Z' && choice2 > '0' && choice2 <= '9')
System.out.println("That is a valid character to start an identifier.");
else
if(choice2 == '$')
System.out.println("That is a valid character to start an identifier but should only be used by mechanically generated source code");
else
System.out.println("Sorry, that is not a valid character to start an identifier");
break;
Is there something I'm doing wrong or is it something inherent to the char data type?
Thanks
The problem is with your boolean groupings and a couple && should be ||
if (choice2 == '_' ||
((choice2 >= 'a' && choice2 <= 'z') ||
(choice2 >= 'A' && choice2 <= 'Z') ||
(choice2 > '0' && choice2 <= '9')))
This will evaluate to true if choice2 == '_' OR if choice2 is either between a and z inclusive, between A and Z inclusive, or between 0 and 9 inclusive.

Need to create a java code that will sort numbers

Need a java code that will sort integers in order.
This is a portion of my code but when I try to compile it, it says that it is missing a return statement. I am confused about how to fix this.
public double getSmallest()
{
if (num1 <= num2 && num1 <= num3)
return num1;
if (num2 <= num1 && num2 <= num1)
return num2;
if (num3 <= num1 && num3 <= num2)
return num3;
}
You have to return a value in the case that none of these conditions are true.
Either return a meaningless value like Double.MIN_VALUE (at least, meaningless in 99.9% of the cases) or throw an exception (a lot better!): throw new IllegalArgumentException().
Or, probably better: just refactor your conditions to make them automatically default to one value since that is really how this method should work. Like a waterfall.
Since you are not using an else if chain but three separated ifs according to the compiler what can happen is that if c1 and c2 and c3 are all false
if (c1)
return ..; << not executed
if (c2)
return ..; << not executed
if (c3)
return ..; << not executed
<< code flow arrives here but method ends: no return value
First of all, since these are mutually exclusive situations, you could use an else if:
if (c1)
return ..;
else if (c2)
return ..;
else if (c3)
return ..;
else
return 0; // will never be called
But since last case will never be chosen, because at least one condition will be true you can simply go:
if (c1)
return ..;
else if (c2)
return ..;
else
return ..;
Regarding message it is missing a return statement, it is because in the following situation:
what it none of the 3 if condition is satisfied.
Have a try with the following code:
public double getSmallest() {
return num1 < num2 ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2
: num3);
}
or
public double getSmallest() {
if (num1 < num2) {
return num1 < num3 ? num1 : num3;
} else {
return num2 < num3 ? num2 : num3;
}
}
The problem is the compiler only sees return statements within if statements. It thinks there's no guaranteed path through the method that has a return.
Something like this would work:
if(num1 <= num2 && num1 <= num3) {
return num1;
} else if(num2 <= num1 && num2 <= num1) {
return num2;
} else { //by matter of deduction
return num3;
}
Another possible solution:
public double getSmallest() {
double min = num1;
if(min > num2) {
min = num2;
}
if(min > num3) {
min = num3;
}
return min;
}
As a note, this particular solution is easily the most scalable. If instead of a handful of numbers, you had an array of numbers, you could assign min to the value of the first index, and then iterate through the entire collection comparing the value at each index to min, and if it's smaller, reassign min.
Above are probably the two best and most logical solutions. However, simply adding return 0 (or ANY return statement) after all the if statements will make the compiler happy. This will work... but it's sloppy in my opinion (at least in this case).

Categories

Resources