Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Assign a loop to variable to compare strings

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Assign a loop to variable to compare strings

    Hey everyone, so i am trying to write a program that takes the first letter of a string, concats it to the end of the string and can spell the original string backwards.

    For example,
    I have a string called
    "grammar"
    take the first letter "g" and add it to the end
    "grammar"
    g ----->

    This equals "rammarg" which is "grammar" spelled backwards. Other examples are "revive, potato, uneven"

    The end result is i have to compare the string backwards with the one that is concatenated

    The issue that i am running in to is i can concat the first letter of the string to the end just fine, but when i use a loop to spell the word backwards i have no way of using that loop and applying it to a variable so i can compare the two strings.


    I CAN'T use the "StringBuffer.reverse" method and have to use a loop to compare them


    public static void main(String[]args)
    	{
     
     
    	//creates string called word
    	String word;
     
    	Scanner keyboard = new Scanner(System.in); // initalizes scanner
    	System.out.println("Enter a word (type exit to end the program) :"); //prompts the user to enter in the word
    	word = keyboard.next(); //allows the word to be taken in
     
     
     
     
     
     
    		//Initialize first character of the string word
    		char wordfc = word.charAt(0);
     
     
    		//convert from character to string
    		String firstchar = Character.toString(wordfc);
     
    		//word with first char gone
    		String last = word.substring(1);
     
    		//letter at end // adds the first character of wordfc to the end of the substring last
    		String lae = last.concat(firstchar);
    		//System.out.println(lae);
     
     
     
     
                  // This is the loop(below) that prints the word backwards, 
                  //how would i assign this to a variable to compare to "lae" which is the "word" concatenated. 
     
     
                    int i = word.length()-1;
     
             	for( ; i>=0; )
    	        {
     
    		         System.out.print( word.charAt(i--));
     
    	         }
     
    		/*I am basically wanting this loop above to compare to lae as in
     
                    if(lae.equals(stringbw))
                    {
                     Syso("yes') 
                    }
                     else
                     Syso("no")
    		*/

    Thank you all in advanced for your help!


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    989
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: Assign a loop to variable to compare strings

    You could declare the String stringbw as an empty string and use your loop to append the characters to it instead of (or as well as) printing them.

    Alternatively if you don't want to actually print stringbw then you could use a single loop to compare each character of lae with the corresponding character from the other end of word. "grammar" has length 7, so your loop would compare lae[0] with word[6-0], lae[1] with word[6-1], ..., lae[6] with word[6-6].

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    norske_lab (March 6th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Assign a loop to variable to compare strings

    Thank you so much for the help! i was able to get my program 99% done. I have only one print statement that is not printing when i want it to.

    So after it tests the loop, it will say "yes" just fine if indeed the parameters above are true. The only thing i cant figure out is where to put the statement "no" if it does not work.

     
     
    import java.util.*;
     
    public class Words
    {
     
     
     
    	public static void main(String[]args)
    	{
    		String word;//creates string called word
    		Scanner keyboard = new Scanner(System.in); // Initializes scanner
    		System.out.println("Enter a word (type exit to end the program) :"); //prompts the user to enter in the word
    		word = keyboard.next(); //allows the word to be taken in
     
     
     
     
    		while(!word.equalsIgnoreCase("exit"))
    		{
     
     
     
     
    		//Initialize first character
    		char wordfc = word.charAt(0);
     
     
    		//convert from character to string
    		String firstchar = Character.toString(wordfc);
     
    		//word with first char gone
    		String last = word.substring(1);
     
    		//letter at end // adds the first character of wordfc to the end of the substring last
    		String lae = last.concat(firstchar);
    		//System.out.println(lae);
     
     
     
     
    			char wordd , laeword;
     
     
    			int numlae = 0;
    			int numword = 0;
    			int length = word.length()-1;
     
    			wordd = word.charAt(length - numword);
     
    			laeword = lae.charAt(numlae);	 
     
     
    			while(wordd == laeword)
    			{
     
    				System.out.println("yes");
    				numword++;
    				laeword++;
     
     
    			}
     
     
    			System.out.println("Enter a word (type exit to end the program):");
    			word = keyboard.next();
     
    		}
     
    		System.out.println("Bye");
     
     
     
     
     
     
    	}
    }



    So basically at the end of each occurrence when it is true it will print yes, but i don't know where to put the statement for it to say "no" if it equals false.

    Thank you so much for your help! you have no idea how much you helped me out!!!

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    989
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: Assign a loop to variable to compare strings

    First there seems to be something wrong with the logic

    wordd = word.charAt(length - numword);
    laeword = lae.charAt(numlae);    
     
    while(wordd == laeword)
    {
        System.out.println("yes");
        numword++;
        laeword++;
    }

    You should be checking each pair of characters. Since you already know how many comparison need to be made, a for loop might be easiest:

    for(int numword = 0; numword < ???: numword++)
    {
        char wordd = ???;
        char laeword= ???;
        // now compare them
    }

    As for reporting your answer, think about *when* you know the answer. The chances are you will know that a word fails the test (if it does) well before you will know that it passes.

Similar Threads

  1. Cannot assign requested address
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2012, 01:32 PM
  2. Variable updates after loop
    By tecno40 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 9th, 2011, 05:34 AM
  3. how to assign values to (args) from Jcreator
    By amr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 03:41 PM
  4. Replies: 1
    Last Post: October 16th, 2010, 04:32 PM
  5. How to assign values from a file to an arraylist of objects?
    By nadman123 in forum Collections and Generics
    Replies: 3
    Last Post: September 15th, 2008, 02:07 PM