Method to check vowels in a string [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
public static void main(String[] args) {
Scanner Input= new Scanner(System.in);
System.out.print("Enter String: ");
String s =Input.nextLine();
int index = s.length();
boolean isVowel= true;
isVowel = vowels(s,index);
if(isVowel==true)
System.out.println("Its Vowel");
}
public static boolean vowels(String s,int index){
String small=s.toLowerCase();
String large = s.toUpperCase();
char z=s.charAt(s.length()-1);
if (s==small) {
large = s.toUpperCase();
if(s==large){
}
for (int i = 0; i < s.length(); i++) {
if (z=='A'||z=='E'||z=='I'||z=='O'||z=='U') {
System.out.println("Character at " + s.charAt(s.length()-1) + " is a vowel");
return true;
} else if(z!='A'||z!='E'||z!='I'||z!='O'||z!='U'){
System.out.println("The String contains no Vowels");
return false;
}
}
}
return true;
}
}
It keeps returning the last printing statement, "The String contains no Vowels"
Any suggestions?

import java.util.*;
public class Solution{
public static void main(String[] args) {
Scanner Input= new Scanner(System.in);
System.out.print("Enter String: ");
String s =Input.nextLine();
if(vowels(s)) System.out.println("It contains a vowel!");
else System.out.println("It does not!");
}
public static boolean vowels(String s){
String word = s.toUpperCase();
char[] words = word.toCharArray();
for(int i = 0; i<words.length; i++){
char z = words[i];
if (z=='A'||z=='E'||z=='I'||z=='O'||z=='U') return true;
}
return false;
}
}
If I understood your question right, I think the above code should do it.

Related

How can i make my Programm accept Palindrom with capital letter or space between? [duplicate]

This question already has answers here:
Java, Check if a String is a palindrome. Case insensitive
(5 answers)
Closed 1 year ago.
import java.util.Scanner;
public class palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word = sc.next();
String org_word = word;
word = word.replace(" ","");
String reverse = "";
for (int i = word.length()-1; i >=0; i--) {
reverse += word.charAt(i);
}
boolean palindrome = true;
for (int i= 0; i < word.length(); i++){
if(word.charAt(i) != reverse.charAt(i)){
palindrome = false;
}
}
if (palindrome) {
System.out.println("Your word is a palindrome!");
}
else System.out.println("Your word is not a palindrome!");
}
}
If I put a Palindrome in my Program like "racecar" it does it correctly, but if I type "race car" with a space, it doesn't work. Neither does it when I start a word with a capital letter.
You are using scanner.next() to read in your arguments. In the case of a race car, this means it will read in the first word: race. Which is indeed not a palindrome. To solve this, you need to use scanner.nextLine() to read everything until the next line.
For ignoring case sensitivity, you could change all input to lower case. The string method has a very usefull out of the box method: toLowerCase()
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word = sc.nextLine();
word = word.replace(" ", "");
word = word.toLowerCase();
String reverse = "";
for (int i = word.length() - 1; i >= 0; i--) {
reverse += word.charAt(i);
}
boolean palindrome = true;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) != reverse.charAt(i)) {
palindrome = false;
}
}
if (palindrome) {
System.out.println("Your word is a palindrome!");
} else {
System.out.println("Your word is not a palindrome!");
}
}

to check whether a string is palindrome or not using java

import java.util.Scanner;
public class palindrome{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String rev;
for(int i=str.length()-1,k=0;i>=0 && k<str.length();i--,k++)
{
rev.charAt(k) = str.charAt(i);
}
if(rev==str)
System.out.println("string is palidrome");
else
System.out.println("string is not palindrome");
}
}
what is wrong with this code?
note: error is showing at the following line of code
rev.charAt(k)=str.charAt(i);
.charAt(k) doesn't return a location, it only tells you what character is there.
Sample code for palindrome program
import java.util.*;
class PalindromeExample2
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner(System.in);
System.out.println("Enter a string/number to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string/number is a palindrome.");
else
System.out.println("Entered string/number isn't a palindrome.");
}
}

Why character in StringBuild does not change?

The character must be entered from the console to change to lowercase letters on this line. But it displays the same word and the symbol does not change.
public class Task {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(requestString());
char symbol = requestSymbol().charAt(0);
int count = 0;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == symbol) {
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
count++;
}
}
System.out.println("Number of entries: " + count);
System.out.println("Converted string: " + sb);
}
static String requestString() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter string:");
return scanner.nextLine();
}
static String requestSymbol() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the symbol:");
return scanner.next();
}
}
It seems to be a problem with the line:
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
It should be:
sb.setCharAt(i, Character.toUpperCase(symbol));

Is there an easier way to write this? [duplicate]

This question already has answers here:
Reverse a string in Java
(36 answers)
Closed 4 years ago.
There are some assignments in Walter Savitch's Java book, where it asks you to write some code to reverse the order of a word that is entered. I came up with the following and am wondering if I could be able to optimize it as it seems a little heavy:
public static void main(String[] args) {
String statement;
System.out.print("Enter a statement to reverse: ");
statement = keyboard.nextLine();
int n;
String finalWord = "";
String letter;
for (n = statement.length(); n > 0; n--)
{
letter = statement.substring(0, 1);
finalWord = letter + finalWord;
statement = statement.substring(1);
System.out.println(finalWord);
}
System.out.println("Final work: " + finalWord);
Any insight would be appreciated.
}
import java.lang.*;
import java.io.*;
import java.util.*;
class ReverseString
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter String To Reverse:- ");
String input = sc.next();
// convert String to character array
char[] arr = input.toCharArray();
for (int i = arr.length-1; i>=0; i--)
System.out.print(arr[i]);
}
}
You can use below given code to reverse string
public class ReversString
{
public static void main(String args[])
{
String name = "Vinayak Dwivedi";
String reverseStrinf = "";
for(int i = name.length() - 1;i >= 0 ;i--)
{
reverseStrinf = reverseStrinf + name.charAt(i);
}
System.out.println("reverseStrinf:-"+reverseStrinf);
}
}

Java palindrome until EOF

I have a program that is supposed to ask the user for a number and it will determine whether it is a palindrome or not. It's supposed to keep asking for numbers until EOF is input - So far it asks for the number twice and doesn't seem to be doing the while loop correctly.
Any insight is appreciated
import java.util.Scanner;
public class PalindromeEOF
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to check if it is a palindrome:");
String num = scanner.nextLine();
String reverse = "";
while (scanner.hasNextLine())
{
for ( int i = 0; i<num.length(); i++ )
{
reverse = num.charAt(i) + reverse;
}
if (num.equals(reverse))
{
System.out.println("\nEntered number IS a palindrome.");
}
else
{
System.out.println("\nEntered number is NOT a palindrome.");
}
System.out.println("\nEnter a number to check if it is a palindrome:");
num = scanner.nextLine();
reverse = "";
}
System.out.println("\nProgram ended on request");
}
}
This worked for me; unless you need num or reverse outside the while loop it should work.
import java.util.Scanner;
public class PalindromeEOF
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to check if it is a palindrome:");
while (scanner.hasNextLine())
{
String num = scanner.nextLine();
String reverse = "";
for ( int i = 0; i<num.length(); i++ )
{
reverse = num.charAt(i) + reverse;
}
if (num.equals(reverse))
{
System.out.println("\nEntered number IS a palindrome.");
}
else
{
System.out.println("\nEntered number is NOT a palindrome.");
}
System.out.println("\nEnter a number to check if it is a palindrome:");
}
System.out.println("\nProgram ended on request");
}
}
I would separate the palindrome test into its' own method. You could do that in a one line method like
public static boolean isPalindrome(String str) {
return new StringBuilder(str).reverse().toString().equals(str);
}
but I would prefer to iterate the first half of the characters and compare them to the second half in reverse like
public static boolean isPalindrome(String str) {
if (str == null) {
return false;
}
char[] chars = str.toCharArray();
for (int i = 0; i * 2 <= chars.length; i++) {
if (chars[i] != chars[chars.length - i - 1]) {
return false;
}
}
return true;
}
Then your main can invoke that in an infinite loop (terminating on the lack of input) like
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter a number to check if it is a palindrome:");
if (!scanner.hasNextLine()) {
break;
}
String num = scanner.nextLine();
if (isPalindrome(num)) {
System.out.printf("%s is a palindrome%n", num);
} else {
System.out.printf("%s is NOT a palindrome%n", num);
}
}
System.out.println("Program ended on request");
}

Categories

Resources