how can i differentiate between big and small letter in java - java

i need to set a new array with 10 characters in which contains 2 capital letters. I need to find the capital letters and replace them with the same SMALL letters. how can i do it? how can i differentiate between small/capital letter?

To convert some letter to small/capital letter
public class Convert
{
public static void main (String args[]){
char array[]={'a','f','G','K','e','R','t','M','h','j' };
System.out.print(Character.toUpperCase(array[8]));//to convert h to H...for example
System.out.print(Character.toLowerCase(array[2]));//to convert G to g...for example
} }
If you want to know weather the letter is small/capital letter... Its outputs in boolean
public class Differentiate
{
public static void main (String args[]){
char array[]={'a','f','G','K','e','R','t','M','h','j' };
System.out.print(Character.isUpperCase(array[8]));//false because h is small letter ...for example
System.out.print(Character.isLowerCase(array[0]));//true because a is small letter ...for example
} }
but i dont know you want converting or differentiate !!

Related

Validating Scanner to only allow characters

Here's my first question. I'm taking an online coding class, and it definitely has some holes in the instruction. We have been tasked with taking 2 different inputs that are one character only, and converting them to ASCII values. I have that down pat, I'm struggling with how I'm supposed to validate the inputs to only allow one character, and not something such as a number or symbol, and then exit the program if such an error arises. Here's my code.
import java.io.*;
import java.util.*;
public class AndrewBrutonMod4TopSecret
{
public static void main(String args[])
{
Scanner kaReader = new Scanner(System.in);
System.out.println("Enter the first initial of your first name:");
String i1 = kaReader.next();
System.out.println("Enter the first initial of your last name:");
String i2 = kaReader.next();
char a = i1.charAt(0);
char b = i2.charAt(0);
char a1 = Character.toUpperCase(a);
char b1 = Character.toUpperCase(b);
int c = (int)a;
int d = (int)b;
System.out.println("Initials: "+a1+" "+b1);
System.out.println("Encrypted Initials: "+c+" "+d);
}
}
Any thoughts?
I'm struggling with how I'm supposed to validate the inputs to only
allow one character, and not something such as a number or symbol
You can use isAlphabetic() method
Javadoc:
Determines if the specified character (Unicode code point) is an
alphabet. A character is considered to be alphabetic if its general
category type, provided by getType(codePoint), is any of the
following:
UPPERCASE_LETTER
LOWERCASE_LETTER
TITLECASE_LETTER
MODIFIER_LETTER
OTHER_LETTER
LETTER_NUMBER
or it has contributory
property Other_Alphabetic as defined by the Unicode Standard.
private static boolean isValidChar(char ch) {
return Character.isAlphabetic(ch);
}
and then exit the program if such an error arises
For example
if (!isValidChar(a))
System.exit(-1);

How to Return "yes" if the first letter or last letter of the string is a vowel [ aeiouAEIOU ]

This code focuses on strings, decision making [ if statements ], and parameter passing.
So far I have a runner class and the return method to use for the vowel:
public class FirstLastVowel {
public static String isVowel(String letter) {
String fl = letter.substring(0, 1);
String LL = letter.substring(letter.length() - 1);
String vowel = "AEIOUaeiou";
vowel.indexOf(letter);
if(
}
}
import java.util.*;
import java.io.*;
public class StringRunner_Cavazos {
public static void main(String[] args) throws IOException {
Scanner fileIn = new Scanner(new File("firstLastVowel.dat"));
while(fileIn.hasNext()){
System.out.println(FirstLastVowel.isVowel(fileIn.nextLine()));
}
}
}
In the FirstLastVowel.java I have to put the substrings into variables and compare them to the indexOf(vowel) which is AEIOUaeiou and I'm stumped on what to type.
Edit #1: So I have changed some of the code, I removed the for loop and added 'vowel.indexOf(letter)' now how do I check the word if it's empty and return 'no' compared to if I can pull the first letter and the last letter and return 'yes'?
First, you do not need a loop: checking the initial letter is a one-time deal, you do not need to check letters beyond the initial one to determine if the word starts with a vowel or not.
You are on the right path with letter.indexOf(vowel) expression. However, you are ignoring the return value, which is why your code is not behaving the way you want. It should also be the other way around, i.e. vowel.indexOf(letter)
You need to check the result of the call to see if it is negative or non-negative. If the first letter is a vowel, the result is going to be non-negative; otherwise, the result is going to be negative.
The code should proceed as follows:
See if the word is empty, and return "no" if it is
Take the first letter of the word, and check if its index in the list of vowels is non-negative.
If the index is non-negative, return "yes"; otherwise, return "no".

Substring a string based on presence of a character

I have a string: LOAN,NEFT,TRAN. I want to substring the string based on getting a , during traversing the string. So I tried to first get a count for how many , are there. but not sure what function to user to get what I want. Also this should be dynamic, meaning I should be able to create as many substrings as required based on number of ,s. I tried the following code:
package try1;
public class StringTest {
public static void main(String[] args) {
String str="LOAN,NEFT,TRAN";
int strlen=str.length();
int count=0;
for(int i=0;i<strlen;i++)
{
if(str.contains("'"))
count++;
}
System.out.println(""+count);
for (int j=0;j<count;j++)
{
//code to create multiple substrings out of str
}
}
}
But I do not think contains() is the function I am looking for because value of count here is coming 0. What should I use?
Your code doesn't actually count the , characters because 1) contains doesn't take into account your loop variable 2) it's searching for ', not ,
Assuming you want to work at a low level rather than using high level functions like .split(), then I'd recommend the following.
for(char c : str.toCharArray()) {
if (c == ',') {
count++;
}
}
You can use split to get substrings directly:
String[] substrings = str.split(",");
Is this what you want as an output: (shown below)?
["LOAN", "NEFT", "TRAN"] // Array as an output
Or to just get the count of the splitting char, you can use the same line as above with this:
int count = substrings.length - 1;

How to find the word with the most uppercase letters in an ArrayList

I have to enter cities in an array list and then analyze the inputted citites to find the city with the most upercase letters. But i cannot figure out the code to anylyze the enteries in the arraylist and find the word with the most uppercase letters.
package cirties2;
import java.util.Scanner;
import java.util.*;
public class Cirties2 {
public static void main(String[] args)
{
Scanner city = new Scanner(System.in);
System.out.println("Enter the cities<enter stop to exit>");
List<String> cities = new ArrayList<String>( );
boolean thing = true;
while(thing)
{
String s = city.nextLine( );
if(s.equalsIgnoreCase("stop"))
{
System.out.println(cities);
break;
}
else
{
System.out.println("Enter the cities<enter stop to exit>");
cities.add(s);
}
}
}
}
It is too easy, I will give you a few hints though .
Looping through an Array List use the first way
To check whether a letter is Uppercase or not see this
You only care about the word with most Uppercase letters, so keep updating a variable (let's call it track)containing the index of the word with the highest number of uppercase letter whenever you find a word with more uppercase letter than the one track is pointing to.
You shouldn't ask such simple questions here, at least not without trying something first and showing us what you did so far. You will get a lot of down votes. I'm a fairly good programmer now but I'm talking from experience, feels good to be on the patronizing side lol.
Assuming that your strings contain only alphabets, upper case alphabets ASCII values are less than their lower case alternatives. So if u're using String.hashCode(), the string which is having more Upper case alphabets will have the smallest hashcode among all the strings.
Scan through your list, get the hashcode of each string in every iteration, update the minimum if its less than the previous value.
Hope this helps :)

How can i use substring or other function to divide a string into words?

So I have this function:
public void actionPerformed(ActionEvent e)
{
String input = jt.toString();
}
And I want to use substring or any other function to divide the words in this sentence.
Like using substring from 0 until it finds a "space" and then it should stop and then start again until the end of the sentence.
What you need is to use the Split method for the String class.
String[] input = jt.toString().split("\\s+");
This method will give you an array where each cell of the array will contain a word.
jt stands for JText?
If yes, you should get the text from the component instead of converting the object to string using the following instruction: jt.getText()
string[] words = input.Split(' ')
You can also do it with stringbuilder if you care about saving memory.
public class splitword {
public static void main(String[] args) {
String input = "Hi! Can you please split me into pieces :0";
String[] toSplit = new StringBuilder(input).toString().split("[\\s\\p{P}&& [^']]+");
for (String x : toSplit) {
System.out.println(x);
}
}
}

Categories

Resources