If statement help (noob) [duplicate] - java

This question already has answers here:
What's the best way to check if a character is a vowel in Java?
(5 answers)
Closed 6 years ago.
I was trying to grab the first character from user entry, and determine it to be a vowel or not. I am very new, and have been struggling with this for a while. I am trying to add all vowels to the variable 'vowel', and not only will that obviously not work, but I feel like I am going the long way. Any help at all is vastly appreciated as I am very new to this.
entry = scanner.nextLine();
letters = entry.substring(0,1);
holder = entry.substring(1);
vowels = "A";
if (entry.substring(0,1).equals(vowels)) {
pigLatinVowel = entry + "way";
System.out.println(pigLatinVowel);
}

First, since you only want one character, don't use string methods, i.e. use entry.charAt(0) == 'A' instead of entry.substring(0,1).equals("A").
With that, you can then turn it around:
"AEIOU".indexOf(entry.charAt(0))
If the character at position 0 in the entry variable can be found, indexOf() returns the index position (zero-based), otherwise it returns -1.
So, to see if it is a vowel, do this:
if ("AEIOU".indexOf(entry.charAt(0)) != -1) {
pigLatinVowel = entry + "way";
System.out.println(pigLatinVowel);
}

Related

How to add whichever character between every character of a premade String? [duplicate]

This question already has answers here:
Concatenating null strings in Java [duplicate]
(5 answers)
Closed 7 years ago.
How to add whichever character between every character of a premade String? (JAVA)
For example, I have the String "Hello world" and I have to add '_' between every character of the String.
Any function or useful code I can use to do it?
I have to do an algorithm that make me output "H_e_l_l_o_ _w_o_r_l_d"
This is what I have:
public String example(String s) {
String s2 = null;
for(int i = 0; i < s.length(); i++){
s2 += s.charAt(i) + (((i+1) == 0) ? " " : "-");
}
return s2;
}
My output in the main class is being:
nullH-e-l-l-o- -w-o-r-l-d-
Don't know why
This looks like a homework assignment. So, I won't directly write out all the code.
String = "hello world";
Say, there is a variable len = str.length() - 1. Instead of doing it from index 0, we will start our for loop from len - 1. The character 'd' is at index len, and the '_' will have to be inserted right before that. This can be done by setting the string to str = str.substring(0,i) + "_" + str.substring(i+1);
You will have to use a for loop that starts from len - 1 and goes on till the index reached is 0.
Now, on every single iteration, when you are inserting a character assigning str to str.substring(0,i) + "_" + str.substring(i+1); causes you to make a new string object, which is absolutely horrible style. This can be solved by using a StringBuilder.
Does that make it clear?
In the future, refrain from posting questions without having done any work. This community is there to help you with solving issues that you may have in your solutions, not write your solutions for you.

How to calculate tab length till the end of the String? [duplicate]

This question already has answers here:
Java String split removed empty values
(5 answers)
Closed 8 years ago.
I am spliting the String by tab like
String s = "1"+"\t"+2+"\t"+3+"\t"+"4";
System.out.println("length : "+ s.split("\\t").length);
In this case i get the length 4. But if i remove the last element 4 & give only blank, like
String s = "1"+"\t"+2+"\t"+3+"\t"+"";
System.out.println("Length : "+ s.split("\\t").length);
In this case i got the output 3. it means this is not calculating last tab.
In below case also, i need the length 4. This scenario i am using in my project & getting undesired result.
So please suggest me, How to calculate the entire length of tab delimited string, whether the last element is also blank.
Such as, if the case is,
String s = "1"+"\t"+2+"\t"+3+"\t"+"";
System.out.println("Length : "+ s.split("\\t").length);
then the answer should be 4 & if the case is,
String s = "1"+"\t"+2+"\t"+3+"\t"+"" + "\t"+ "";
System.out.println("Length : "+ s.split("\\t").length);
then the answer should be 5.
Please provide me the appropriate answer.
To prevent split from removing trailing empty spaces you need to use split(String regex, int limit) with negative limit value like
split("\t", -1)

If statement wont recognize string [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have an if statement that takes a string, and if another string has the same value as that string do 1 thing, and if the variable doesnt equal that string do another thring
here is my code
if(Pos != "D"){
System.out.println("doesnt = D");
}
if (Pos == "D" ){//WHY ISNT THIS WORKING
System.out.println("it does = D");
}
It recognizes when the variable doesnt = D and prints "doesnt = d" but when the variable = D it does nothing. I dont know why.
thanks
Never compare Strings with == or != since these check to see if two String variables refer to the same object reference, and this is not what you're interested in. Instead use the equals(...) or equalsIgnoreCase(...) method to see if the two Strings have the same chars in the same order as that's what really matters here. i.e.,
Use equals to compare strings :
if ("D".equals(Pos))

Java: Comparing Array of Characters case sensitive? [duplicate]

This question already has answers here:
In Java, how do I check if a string contains a substring (ignoring case)? [duplicate]
(6 answers)
Closed 9 years ago.
I am writing a program to compare a few characters with a Char Array and return the index of the array. Is there any possible way to compare ignore case?
For example below:
String in = "I AM A HAPPY BOY";
char[] cha = new char[] {a,c,e,g,i,k,h,m,o,q,s,u,w,y};
char testChar = in.substring(4,5).charAt(0);
for(int a = 0; a<char.length; a++){
if(cha[a] == testChar)
return a+1;
}
I am unable to get the index as it will always point to 0. Is there anyway to ignore case here? Appreciate some advise.
Use Character.toLowerCase on both characters:
if (Character.toLowerCase(cha[a]) == Character.toLowerCase(testChar)) {
// logic here
}
As a side note, you could get away with the first toLowerCase if all the characters in your array are already lower case, or even use toLowerCase on the initial string and avoid both.
You can use Character.toLowerCase(char):
if (Character.toLowerCase(cha[a]) == Character.toLowerCase(testChar)) {
return a+1;
}
use Character.ToLowerCase(char c) before testing for equality.
in=in.toLowerCase();
However the most efficient way to convert chars between cases is to flip the 6th bit (ASCII values differ by 32).

Reversing a string without using any built in methods [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Printing reverse of any String without using any predefined function?
Please advise how to reverse a string without using built in methods. I want to use only string class, please advise let say there is a string "john is a boy" and print "yob a si nhoj".
This method will return the string backwards. all you have to do is iterate through the string backwards and add it to another string.
you do this using a for loop, but first check if the string has a greater lenght than 0.
Java Strings have a method "charAt(index)" which return a single character on the position of the string, where position 0 is the first character. so if you would like to reverse "Boy" you would start on letter 2, then 1, and then 0, and add them all together into a new String, resulting in "yoB".
public static String reverseString(String inString) {
String resultString = "";//This is the resulting string, it is empty but we will add things in the next for loop
if(inString.length()>0) {//Check the string for a lenght greater than 0
//here we set a number to the strings lenght-1 because we start counting at 0
//and go down to 0 and add the character at that position in the original string to the resulting one
for(int stringCharIndex=inString.length()-1;stringCharIndex>=0;stringCharIndex--) {
resultString+=inString.charAt(stringCharIndex);
}
}
//finaly return the resulting string.
return resultString;
}
You could iterate through all the characters in your string and prepend them to a StringBuffer using the insert(0, char) method. Then at the end of the iteration, your StringBuffer will be the reversed string.

Categories