Write a program that receives a character and displays its Unicode - java

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.

Related

There is an error of exception in thread "main" and i need to have a message entered

I don't see the issue or how to input the message. The full error that I keep getting is below. I can't see any problems with the code and I can't find a way to a message into the code.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of ran
ge: 30
at java.base/java.lang.StringLatin1.charAt (String.java:47)
at java.base/java.lang.String.charAt (String.java:693
at Main.main (Main.java:15)
import java.util.Scanner;
public class Main
{
public static void main( String[] args )
{
Scanner kb = new Scanner(System.in);
System.out.println("What is your message? ");
String message ="" + "\n";
System.out.println("\nYour message is " + " characters long.");
System.out.println("The first character is at position 0 and is '" + "'.");
int lastpos = 30;
System.out.println("The last character is at position " + lastpos + " and is '" + message.charAt(lastpos) + "'.");
System.out.println("\nHere are all the characters, one at a time:\n");
for ( int i=0; i<message.length(); i++ )
{
System.out.println("\t" + i + " - '" + message.charAt(i) + "'");
}
int a_count = 0;
for ( int i=0; i<message.length(); i++ )
{
char letter = message.charAt(i);
if ( letter == 'a' || letter == 'A' );
{
a_count++;
}
}
System.out.println("\nYour message contains the letter 'a' " + a_count + " times. Isn't that interesting?");
}
}
You never actually capture a message from the scanner - something like this:
message = kb.nextLine();
In addition, if you're trying to get the last index/position of the message, you'd want to do something like this:
int lastpos = message.length();
OR
int lastpos = message.length()-1;
The difference between the two being that the first gives you the size of the string, and the second gives you the last index of the string (for example, "abcdefg", lastpos 1 would be 7, and lastpos 2 would be 6).
Finally, keep in mind, you can do something like
message.charAt(0);
to get the first index character of the string.
I have made 3 changes from the code that you have written. They are the following:
Read the message using kb.nextLine(); instead of "" + "\n";
The first character of the message is message.charAt(0) instead of "'."
To count the number of characters, you have given semicolon after the if condition. Remove that. The semicolon after if is not needed.
Check the snippet below for better understanding.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("What is your message? ");
String message = kb.nextLine();
System.out.println("\nYour message is " + " characters long.");
System.out.println("The first character is at position 0 and is '" + message.charAt(0));
int lastpos = 30;
System.out.println("The last character is at position " + lastpos + " and is '" + message.charAt(lastpos) + "'.");
System.out.println("\nHere are all the characters, one at a time:\n");
for (int i = 0; i < message.length(); i++) {
System.out.println("\t" + i + " - '" + message.charAt(i) + "'");
}
int a_count = 0;
for (int i = 0; i < message.length(); i++) {
char letter = message.charAt(i);
if (letter == 'a' || letter == 'A') {
a_count++;
}
}
System.out.println("\nYour message contains the letter 'a' " + a_count + " times. Isn't that interesting?");
}
}

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

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.");
}

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.

How to only add something to a string if it doesn't contain it?

I am making a Lipogram program where any words with the banned letter are printed, however, the words are sometimes printed twice. How do I get it to not repeat the words?
Here is my code:
public String allWordsWith(char letter) {
String str = "";
String word = "";
s = s.replace(".", " ");
s = s.replace(",", " ");
s = s.replace("?", " ");
s = s.replace("!", " ");
s = " " + s + " ";
for (int i = 0; i <= s.lastIndexOf(letter); i++) {
if (s.charAt(i) == letter) {
if (str.contains(s.substring(s.lastIndexOf(" ", i), s.lastIndexOf(" ", i) + 1) + '\n') == true) {
} else {
word += s.substring(s.lastIndexOf(" ", i), s.indexOf(" ", i)) + '\n';
str += word;
}
}
}
return str;
}
Important clarification: Is the function run with the letter chosen as "o" on the string "hello hi hello howdy" meant to return "hello hello howdy" or "hello howdy". I.e., if the word appears twice, do you want to print it twice, or do you only want to print it once regardless of repetition?
If only once regardless of repetition, then you should be using a Set to store your data.
However, I think there's a chance you're instead dealing with an issue that when running the function with the letter chosen as "l" on that same string, "hello hi hello howdy", you are getting an output of "hello hello hello hello". Correct?
The issue here is that you are checking every letter and not testing each word. To fix this, I would use:
String[] words = s.split(" ");
to create an array of your words. Test each value in that array to see if it contains the given letter using:
if(words[index].contains(letter)){
str += " " + words[index];
}

I do not know why I keep on getting strange feedback when I am trying to format phone numbers from a user in java.

Whenever I type in a phone number, this program below that I wrote to format phone numbers from the user gives me back weird numbers that I did not even enter at all. Can someone please explain to me why I am getting such weird errors?
I want it so when someone enters just 12345678978 it will format to 1-234-567-8978
If they enter 2345678978 it will format to 234-567-8978
And if they enter 5678978 it will change to 567-8978.
I always get weird numbers that sometimes aren't even what I entered like
12345678978 I get 144-34--567-
2345678978 I get 153-567-8978
5678978 I get 162-8978
I would really appreciate some help. Thanks.
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner y = new Scanner(System.in);
String phoneNumber;
int phoneNumberLength;
System.out.print
("Please enter your phone number WITHOUT spaces or dashes: ");
phoneNumber = y.nextLine();
phoneNumberLength = phoneNumber.length();
if (phoneNumberLength == 11) {
phoneNumber = phoneNumber.charAt(0) + "-" + phoneNumber.charAt(1)
+ phoneNumber.charAt(2)
+ phoneNumber.charAt(3)
+ "-" + phoneNumber.charAt(4) + phoneNumber.charAt(5)
+ phoneNumber.charAt(6)
+ "-" + phoneNumber.charAt(7) + phoneNumber.charAt(8)
+ phoneNumber.charAt(9)
+ phoneNumber.charAt(10);
}
if (phoneNumberLength == 7) {
phoneNumber = phoneNumber.charAt(0) + phoneNumber.charAt(1)
+ phoneNumber.charAt(2)
+ "-" + phoneNumber.charAt(3) + phoneNumber.charAt(4)
+ phoneNumber.charAt(5) + phoneNumber.charAt(6);
}
else {
phoneNumber = phoneNumber.charAt(0) + phoneNumber.charAt(1)
+ phoneNumber.charAt(2)
+ "-" + phoneNumber.charAt(3) + phoneNumber.charAt(4)
+ phoneNumber.charAt(5)
+ "-" + phoneNumber.charAt(6) + phoneNumber.charAt(7)
+ phoneNumber.charAt(8)
+ phoneNumber.charAt(9);
}
System.out.println("So your phone number is " + phoneNumber + "?");
}
By the way. I know it is not formatted correctly but I am very confused with how stackoverflow allows you to add code.
Java is converting the characters from your charAt() calls to numerical values. Use substring methods instead, e.g.
phoneNumber = phoneNumber.substring(0, 3) + "-" + phoneNumber.substring(3);
Any string that starts like this:
number = number.charAt(0) + number.charAt(1) + ...
will cause the problem, because you are adding two char types together. This is treated as integer arithmetic, not string concatenation. It would be a lot better to add substrings together, so that the operator is string concatenation, instead of integer addition.
number = number.substring(0, 3) + '-' + number.substring(3, 6) + ...

Categories