What is the method for converting strings in Java between upper and lower case?
String#toLowerCase and String#toUpperCase are the methods you need.
There are methods in the String class; toUppercase() and toLowerCase().
i.e.
String input = "Cricket!";
String upper = input.toUpperCase(); //stores "CRICKET!"
String lower = input.toLowerCase(); //stores "cricket!"
This will clarify your doubt
Yes. There are methods on the String itself for this.
Note that the result depends on the Locale the JVM is using. Beware, locales is an art in itself.
String#toLowerCase
Assuming that all characters are alphabetic, you can do this:
From lowercase to uppercase:
// Uppercase letters.
class UpperCase {
public static void main(String args[]) {
char ch;
for(int i=0; i < 10; i++) {
ch = (char) ('a' + i);
System.out.print(ch);
// This statement turns off the 6th bit.
ch = (char) ((int) ch & 65503); // ch is now uppercase
System.out.print(ch + " ");
}
}
}
From uppercase to lowercase:
// Lowercase letters.
class LowerCase {
public static void main(String args[]) {
char ch;
for(int i=0; i < 10; i++) {
ch = (char) ('A' + i);
System.out.print(ch);
ch = (char) ((int) ch | 32); // ch is now uppercase
System.out.print(ch + " ");
}
}
}
Coverting the first letter of word capital
input:
hello
world
String A = hello;
String B = world;
System.out.println(A.toUpperCase().charAt(0)+A.substring(1) + " " + B.toUpperCase().charAt(0)+B.substring(1));
Output:
Hello World
Related
The output is fine but the output is in Uppercase letter, I want the output to be lowercase character, what should I do?
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
int a[]=new int[26];
Scanner sc = new Scanner (System.in);
String str=sc.nextLine();
for(int i = 0; i<str.length();i++) {
if(str.charAt(i)>=65 && str.charAt(i)<=90) {
a[str.charAt(i)-65]++;
}
else if(str.charAt(i)>=97 && str.charAt(i)<=122) {
a[str.charAt(i)-97]++;
}
}
for(int i=0;i<26;i++) {
if(a[i]>0) {
System.out.print(" "+(char )(i+65)+ "(" + a[i]+")");
}
}
}
}
'A' is 65. 'a' is 97. Use 97 instead of 65 in your final loop.
Alternatively use 'a' directly instead of the number. This makes it more obvious what you're doing in that code.
It may be more straightforward to convert the entire string to lower case to avoid checking for upper case letters.
Also, as suggested earlier using character literals as a instead of 97 would make the code more readable.
Scanner sc = new Scanner(System.in);
int a[] = new int[26];
String str = sc.nextLine().toLowerCase();
for (char c : str.toCharArray()) {
if (c >= 'a' && c <= 'z') {
a[c - 'a']++;
}
}
for (char c = 'a'; c <= 'z'; c++) {
int i = c - 'a';
if (a[i] > 0) {
System.out.print(" "+ c + "(" + a[i] + ")");
}
}
Check out this post with a accepted answer How do I convert strings between uppercase and lowercase in Java?
You could probably do something like this
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
int a[]=new int[26];
Scanner sc = new Scanner (System.in);
String str=sc.nextLine();
String lowerCaseInput = str.toLowerCase();
System.out.print(lowerCaseInput);
}
}
This is what I have so far. I'm unsure how to print the individual value of each char in ascii without the values repeating on every line. Any help would be appreciated.
Create a for loop to print out all of the ASCII values associated with EACH
character of the town name or pet name (e.g., R = 125, a = 97 etc.)
import java.util.Arrays;
public class Lab1Part2{
public static void main (String[] args){
char B = 'B';
char l = 'l';
char a = 'a';
char n ='n';
char h = 'h';
int ascii = B;
//create array
char[] JavaCharArray = new char[6];
JavaCharArray[0] = 'B';
JavaCharArray[1] = 'l';
JavaCharArray[2] = 'a';
JavaCharArray[3] = 'n';
JavaCharArray[4] = 'c';
JavaCharArray[5] = 'h';
//print array
for(int i=0;i<JavaCharArray.length;i++){
System.out.println(JavaCharArray[i] + " = " + ascii);
}
}
}
The char type in Java is legacy, essentially broken. As a 16-bit value, it is incapable of representing most characters.
Code points
Instead, learn to use code point integer numbers to work with individual characters.
int[] codePoints = "Blanch😷".codePoints().toArray() ;
for( int codePoint : codePoints )
{
System.out.println( "character: " + Character.toString( codePoint ) + " = code point: " + codePoint ) ;
}
See this code run live at IdeOne.com.
character: B = code point: 66
character: l = code point: 108
character: a = code point: 97
character: n = code point: 110
character: c = code point: 99
character: h = code point: 104
character: 😷 = code point: 128567
You will find "codePoint" related methods around various classes such as String, Character, StringBuilder. See also: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
You need to calculate "ascii" every time you enter the loop (iteration). char can be casted to int automatically
//print array
for(int i=0; i<JavaCharArray.length; i++){
ascii = JavaCharArray[i];
System.out.println( JavaCharArray[i] + " = " + ascii);
}
btw: you declare a few char variables at the start but never use them. For example, you probably wanted to do JavaCharArray[0] = B; rather than JavaCharArray[0] = 'B';
You need to cast the characters to int:
public static void main(String[] args) {
//create array
char[] JavaCharArray = new char[6];
JavaCharArray[0] = 'B';
JavaCharArray[1] = 'l';
JavaCharArray[2] = 'a';
JavaCharArray[3] = 'n';
JavaCharArray[4] = 'c';
JavaCharArray[5] = 'h';
//print array
System.out.print("(");
for (int i = 0; i < JavaCharArray.length; i++) {
System.out.print(JavaCharArray[i] + " = " + (int) JavaCharArray[i]+ ", ");
}
System.out.print(")");
}
I'm trying to make a program where it asks for a letter from the alphabet. Lets say I choose the letter "b". The "b" should be shown in quotation marks in the program. I'm trying to learn Java, I know HTML and CSS, but Java is new to me, so go easy.
So in practice:
Choose a letter:
d
abc"d"efghijklmnopqrstuvwxyz
I've figured out how to print the alphabet
import java.util.Scanner;
public class Characters {
public static void main(String[] args) {
char c;
for(c = 'a'; c <= 'z'; ++c)
System.out.print(c + " ");
}
}
(I have added scanner, because I'll ask the user for the letter)
A very concise example:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
char c = s.next().charAt(0);
for(char i = 'a'; i < 'z'; i++){
System.out.print(i == c ? "\""+c+"\"" : i);
}
}
}
Use java.util.Scanner's .next() to read the next word, then get the first character of the word.
Sample Run:
a
"a"bcdefghijklmnopqrstuvwxy
Since Java recognises quotation marks for text, you need to add a \ before each caracter to indicate to java that you wish for it to be part of the text. For example, if you want to show "b" with quotation mark, your final string is going to be a\"b\"cdefghijklmnopqrstuvwxyz
First, we would have to read the input char:
Scanner scanner = new Scanner(System.in);
char c = scanner.next().charAt(0); // Get char
Then, I would make sure that our char is in the alphabet:
if(!('a' <= c && c <='z')) {
return; // NOT IN ALPHABET
}
If we got this far, we successfully read a character, which is in the alphabet.
Now, we can print out the modified alphabet.
for(char letter = 'a'; letter <= 'z'; letter++) {
if(letter == c) {
System.out.print("\"" + c + "\"");
}else {
System.out.print(letter);
}
}
/*
* Compares user input and checks whether they are anagrams
*
*/
import java.util.Scanner;
public class Anagram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first sentence: ");
String s1 = sc.nextLine();
System.out.print("Enter second sentence: ");
String s2 = sc.nextLine();
s1.toLowerCase();
String new1 = "";
for( char ch = 'a'; ch <= 'z'; ch++){
int i;
for(i = 0; i <s1.length(); i++){
if(ch == s1.charAt(i)){
System.out.print(ch + " are the letters of " + s1 + " in order ");
break;
}
}
}
s2.toLowerCase();
String new2 = new String();
for( char ch2 = 'a'; ch2 <= 'z'; ch2++){
int i2;
for(i2 = 0; i2 <s2.length(); i2++){
if(ch2 == s2.charAt(i2)){
System.out.print(ch2 + " are the letters of " + s2 + " in order ");
break;
}
}
}
}
}
In reference to my question before to which this is the proper way of doing the homework everything works fine except im having trouble creating a new string and passing all the chars to the new string. However this has to be done without using stringbuffer or append() is that possible?
You can use + operator that concatenates strings. But I think it is not the purpose of homework. It seems that you are expected to create char array and then create string using this array.
But that's it, man. If it is a homework you have now enough tips. Do it yourself and welcome to Stackoverflow.
I have been working on this for hours and now Im kinda stuck....please help me.
Im a complete programming handicap. All the methods work fine except the alphabet one.
It will receive two characters (either upper or lower case) and return a string composed of the range of char values given. Maintain the same case (upper or lower) that was passed in to the method. If an upper case and a lower case char (one of each) was passed to the method, convert the upper case char into lower case and use the lower case range. Note, the range will be inclusive of the starting char and exclusive of the ending char. Also, observe that if the starting (first) char given is greater than the ending (second) char, for example 'm' and 'h', then the method will return an empty string since there are no chars in this range.
Can you give me some help on how I can do the above on the alphabet method?
import java.util.*;
class CharacterOperations
{
public static void run()
{
int number=1;
Scanner scanner = new Scanner(System.in);
while(number > 0)
{
System.out.println("(1) Insert 1 to change a letter from its lower case value to its upper case value");
System.out.println("(2) Insert 2 to change a letter from its upper case value to its lower case value ");
System.out.println("(3) Insert 3 for the alphabet method (range of two letters) ");
System.out.println("Enter a number (or negative to quit): ");
number = scanner.nextInt();
if (number == 1)
{
System.out.print("Enter a lower case letter: ");
String a= scanner.next();
char letter = (char) a.charAt(0);
toUpper(letter);
}
else if (number == 2)
{
System.out.print("Enter an upper case letter: ");
String a= scanner.next();
char letter = (char) a.charAt(0);
toLower(letter);
}
else if (number == 3)
{
System.out.print("Enter an upper case or lower case letter: ");
System.out.print("Enter an upper case or lower case letter: ");
String a= scanner.next();
char letter1 = (char) a.charAt(0);
String b= scanner.next();
char letter2 = (char) b.charAt(0);
alphabet(letter1, letter2);
}
}
}
public static char toUpper(char letter)
{
int rep = ((int)letter - 32);
char ltr = (char)rep;
System.out.println("The letter "+ ltr + " integer representation is: " + rep);
return (char) ((int) letter -32);
}
public static char toLower(char letter)
{
int rep = (int)(letter + 32);
char ltr = (char)rep;
System.out.println("The letter " + ltr + " integer representation is: " + rep);
return (char) ((int) letter + 32);
}
public static String alphabet( char letter1, char letter2){
int rep1 = (int)letter1;
int rep2 = (int)letter2;
char ltr1 = (char)rep1;
char ltr2 = (char)rep2;
System.out.println("The letter " + ltr1 + " integer representation is: " + rep1);
System.out.println("The letter " + ltr2 + " integer representation is: " + rep2);
}
}
Thanks!
for (char c = 'a'; c <= 'z'; c++) {
System.out.println(c);
}
it's very simple ^_^
With a char you can just ++ it to get the next char and so on.
char a = 'a';
a++; // now you have b
a++; // now you have c
Just do a while loop to go from start to end char.
Use this to first to generate a random letter. You first have to generate a random number within a certain range. Then when you get the number back and store it in a char variable, it shows it as a letter.
char letter = 0;
letter = (char) (65 + (char)(Math.random() * (90 - 65) + 1));
This answaer assumes you are just talking about standard keyboard characters from the ASCII set.
Take the ascii codes for the 2 charaters and create a loop:
StringBuilder buf = new StringBuilder();
for (int i = rep1; i <= rep2; ++i)
buf.append((char)i);
return buf.toString();
This will work as they both need to be same case...
public static String alphabet(char letter1, char letter2) {
StringBuffer out = new StringBuffer();
for (char c = letter1; c < letter2; c++) {
out.append(c);
}
return out.toString();
}
Obviously you should add some error checking and handling
Simple solution:
String str = "A"
char cr = str.charAt(0);
System.out.println((cr += 1)); //got B
public static String alphabet( char letter1, char letter2){
expects to return a String because you said public static "String"
take out the String and this should work, replace it by void if you just want to print with System.out.print. or return the string that your making
beside that my advise for going to uppercase and lowercase would have been to convert the char to a String and just use the java built in method.