Welcome to Dream.In.Code
Become a Java Expert!

Join 149,606 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,907 people online right now. Registration is fast and FREE... Join Now!




char comparison

 
Reply to this topicStart new topic

char comparison

capty99
16 Sep, 2007 - 08:49 PM
Post #1

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
just trying to see if the first character is a vowel or not,
don't remember java after the break and its kicking my ass....

so i was just doing an if statement,
seeing if the char was equal,
but i get the error that the || operator can't be used between java.lang.strings
CODE

char firstChar = userText.charAt(0);
if(firstChar = "a" || "A" || "e" || "E" || "i" || "I" || "o" || "O" || "u" || "U" ){
System.out.println("the first character is a vowel");}



User is online!Profile CardPM
+Quote Post

capty99
RE: Char Comparison
16 Sep, 2007 - 09:08 PM
Post #2

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
i have another question,
i have a string,
and need to analyze each word separately.

whats the best way to do that.
User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: Char Comparison
16 Sep, 2007 - 09:36 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Tyler,

I may have some syntax incorrect (if so Im sorry) but couldnt you create an array populated wiht the vowels, then loop through the array checking each index to your myString.charAt(0), something like this

CODE

char vowels[] = {'a','e','i','o','u'};
char chr = myString.charAt(0);
for(int i = 0;i < vowels.length;i++)
{
    if vowels[i] == chr
    {
        System.out.println("the first character is a vowel");
    }
}

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Char Comparison
16 Sep, 2007 - 09:56 PM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE(Tyler James Lee @ 17 Sep, 2007 - 06:49 AM) *

just trying to see if the first character is a vowel or not,
don't remember java after the break and its kicking my ass....

so i was just doing an if statement,
seeing if the char was equal,
but i get the error that the || operator can't be used between java.lang.strings
CODE

char firstChar = userText.charAt(0);
if(firstChar = "a" || "A" || "e" || "E" || "i" || "I" || "o" || "O" || "u" || "U" ){
System.out.println("the first character is a vowel");}



Three problems:
- "a" is a String not a char. 'a' is the char.
- the equality operator is == and not =
- you have to write the whole thing like this: (firstChar==a || firstChar == A || ... )

Naturally it would be easier to test it with either with a switch (probably it would be faster) or writing something like
this:
CODE

static private final String vowels = "aeiou";
...
if( vowels.indexOf( userText.substring(0,1).toLowerCase( )) >= 0){


QUOTE(Tyler James Lee @ 17 Sep, 2007 - 07:08 AM) *

i have another question,
i have a string,
and need to analyze each word separately.

whats the best way to do that.


Probably the StringTokenizer class is what you are looking for.
User is offlineProfile CardPM
+Quote Post

capty99
RE: Char Comparison
16 Sep, 2007 - 11:04 PM
Post #5

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
thank you both, i got that fixed up and working now. gracias.

now,
another one,

is there any way to find symbols somehow without doing one of these steps.
or do i just make a massive array with a bunch of symbols in it.

my project is to make a program that can translate english into pig latin

so right now, with yalls help ,

from the input hello world

i get :: ellohay orldway.

but the example i'm supposed to make work is Hello World!

and when i do that i get elloHay orld!way

and thats just a pain to do another big array.
User is online!Profile CardPM
+Quote Post

1lacca
RE: Char Comparison
17 Sep, 2007 - 01:09 AM
Post #6

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I would recommend having a look at the Character and String class, they have very useful functions that generally do right what you are looking for (telling you if a symbol is a letter or digit, and converting between upper and lower case)
User is offlineProfile CardPM
+Quote Post

capty99
RE: Char Comparison
17 Sep, 2007 - 09:29 AM
Post #7

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
this is for myself. restricted comp access right now.

note to self : ignore last section & Fix find first vowel

CODE

import java.util.*;

public class PigLatin{
public static void main(String[] args){
//Establish a scanner & prompt for input
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter A Sentence:");
String toTranslate = scan.nextLine();

//Establish an object and print out return from running it
PigLatin translateIt = new PigLatin();
System.out.println(translateIt.translate(toTranslate));
}

private String translatedString = ""; //Will become the final string
private String userText; //the input from the user
private String finalWord; //Will become each word after translated
private int startWord = 0;
private int endWord = 0;
private int firstVowel = 0; //marks the first vowel in each word
private char vowelCheckChar =0; //used as a marker of what letters have been checked to be vowels

// Translate an individual word
public String translateWord(String userWord){

//create an object and then pass word to findFirstVowel
PigLatin wordIt = new PigLatin();
firstVowel = wordIt.findFirstVowel(userWord);

//If the first letter in the word is a vowel, follow piglatin rules
//and create the final word
if(firstVowel == 0 ){
finalWord = (userWord + "yay");
}

//If the first letter is a consonant, split the word at the firstVowel,
//and build a finalWord
else{
String beforeVowel = userWord.substring(0,firstVowel);
String afterVowel = userWord.substring(firstVowel);
finalWord = (afterVowel + beforeVowel + "ay");
}

return finalWord;
}

//translate a whole string of words
private String translate(String userWord){
userWord = userWord;

//this will repeat as long as the endWord (which marks the last letter of the last word translated)
//is less than the total number of letters in the string
for(int i = 0; endWord < userWord.length(); i++){

//re-establish the end of our new word as the next space
endWord = (userWord.indexOf(" ", startWord));

//if the indexOf returns a false statement, then there is no final space
//so we extend the endWord only to the last character in the string
if(endWord == -1){
endWord = userWord.length();
}

//now we know both ends of where our word is, we pull the string out of there
String nextWord = userWord.substring(startWord,endWord);
//for the next loop we establish the new startpoint
startWord = (endWord + 1);
//create a piglatin object
PigLatin wordIt = new PigLatin();
//use the translateWord method to translate each individual word,
//pass it the string we just cut out of the user entry
translatedString += wordIt.translateWord(nextWord);
//since we culled out the spaces, put one back in
translatedString += " ";
}
return translatedString;
}

private int findFirstVowel(String userWord)
{
//Establish array of vowels
char vowels[] = {'a','e','i','o','u'};

//Runs through each letter of the word until a vowel is found
for(int p=0; p < userWord.length(); p++){
vowelCheckChar = userWord.charAt(p);

//Compares the letter currently on, to the array of vowels
//if a match is found, the loop will end and the firstVowel marked
for(int q=0; q < vowels.length; q++){
    if (vowels[q] == vowelCheckChar)
    {
        firstVowel = p;
        p=userWord.length();
    }
}
}
return firstVowel;
}
}


This post has been edited by Tyler James Lee: 17 Sep, 2007 - 09:52 AM
User is online!Profile CardPM
+Quote Post

capty99
RE: Char Comparison
17 Sep, 2007 - 11:04 AM
Post #8

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
alright i do have another question.

so i have all this stuff going on in my PigLatin class,
as is specified for the project,

but the class also is supposed to have a main method to run the little program
(normally we put the main in a little test file and our class somewhere else)

anyways,
you can see before i use one method inside another,
i initialize a new object, and then do it like that.

is that necessary? if not is there another way to use one method within another cause i didn't get it going.

CODE

PigLatin wordIt = new PigLatin();
firstVowel = wordIt.findFirstVowel(userWord);

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month