Results 1 to 4 of 4
  1. #1
    javastudent is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default for loop problem. Help needed.

    Hi all,

    This is an assignment I am currently doing for school and I have been stuck on this for hours. Can't seem to figure out what is wrong. Anyways here is the code...
    Java Code:
    import java.util.Scanner;
    
    public class Assignment2Ex3 {
    
    	public static void main(String[] args) {
    	
    		int i = 0;
    	
    		Scanner keyboard = new Scanner(System.in);
    		String input = keyboard.nextLine(), firstLetter = "", useLetter, restOfTheWord = "", word, characters = "", reversed = "";
    		
    		useLetter = firstLetter.valueOf(input.charAt(0));
    		restOfTheWord = input.substring(1,input.length());
    		word = restOfTheWord + useLetter;
    		
    		//System.out.println(reversed);
    		
    		while(!input.equalsIgnoreCase("quit")) {
    		
    			characters = input.valueOf(input.charAt(input.length() - 1 -i));
    			reversed = input.valueOf(input.charAt(i));
    		
    			for(i = 0; i < input.length(); i++) {
    			
    				characters = input.valueOf(input.charAt(input.length() - 1 -i));
    				reversed = input.valueOf(input.charAt(i));
    	
    				System.out.print(reversed);
    			}
    			
    			System.out.println(reversed);
    			
    			if(input.equalsIgnoreCase(reversed)) {
    					System.out.println("Yes, the word has this property");
    				}else if(!input.equalsIgnoreCase(reversed) && !input.equalsIgnoreCase("quit")) {
    					System.out.println("No, the word does not have this property");
    				}else {
    					System.out.println("This is not a proper source of input. Please enter a word!");
    				}
    		
    			input = keyboard.nextLine();
    		}
    	}
    }
    So I think the problem is within the for loop. I couldn't be wrong, but I just can't seem to figure it out. I have tried print the reversed variable in different parts of the code, but the only place where it does what it is suppose to is within the loop. So i think it is a scope problem as I can compare it to the input afterwards in the if else statement. Any help would be greatly appreciated.

    Thanks in advance.
    Last edited by JosAH; 06-08-2013 at 11:11 AM. Reason: added [code] ... [/code] tags

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: for loop problem. Help needed.

    What problem? All we know is that there is a problem. However, I am assuming your program wants to determine if the input word is a palindrome.

    First, you have a lot of extraneous code that doesn't do anything. So get rid of lines 12 thru 16.
    Only keep the keyboard input part of line 10, and the reversed declaration.
    Remove all code that uses the "characters" variable.

    Now for some less detail. For each iteration of the while loop you need to reset your values to their default state.
    Also, you are not creating a reversed string. You need to concatenate it backwards.

    And when concatenating a String you can use the input.charAt() directly.
    Example of forward concatenation:
    Java Code:
    String str = "";
    str = str + input.charAt(i);
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    javastudent is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default Re: for loop problem. Help needed.

    Hi,

    Thanks for the quick reply. I may have been a bit exhausted yesterday when I wrote the post so there were some mistakes in it. Sorry about, I guess what I wanted to really know is why the for loop in the code only gives me the very first letter for reversed and not the whole word when I print outside of the loop? I thought that it completely iterates through and gives you the final result at the end, instead however, I the print statement inside and at the end of the for loop gives me the whole word for reversed. What is with this cause all I really need to know is why the reversed variable only has the first letter stored in it because I need the whole word to compare it to the input in my if else statement. Also, I guess I may have used the word palindromic which was wrong to use. In the end the code just needs to tell me if take the letter of the first word and put it at the end and spell it backwards if it is still the same word. That is why I compare the input and reversed variable in the if else statement to check if this property exists.

    Again, thanks for any help you can provide still stumped on why it is doing this.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: for loop problem. Help needed.

    Ok, to answer you specific question, you assign a value to reversed like this:
    Java Code:
     reversed = input.valueOf(input.charAt(i));
    So it only gets the last character. The previous one gets replaced with the next each time thru the loop.

    What you want I believe is to concatenate the characters

    Java Code:
    reversed += input.valueOf(input.charAt(i)); 
    //                or
    reversed = reversed + input.valueOf(input.charAt(i));
    However, that still puts them in normal order. So you need to prepend the characters like this to reverse the string.
    Java Code:
    reversed = input.valueOf(input.charAt(i)) + reversed;
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Help needed with constructing a while loop
    By Bentino in forum New To Java
    Replies: 5
    Last Post: 03-18-2012, 08:38 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. help needed plz with some loop problem I guess
    By Tommeke in forum New To Java
    Replies: 6
    Last Post: 07-21-2011, 07:42 PM
  4. Jdbc problem.Help Needed
    By illusion1912 in forum New To Java
    Replies: 4
    Last Post: 01-09-2009, 06:35 AM
  5. assignment problem help needed
    By tiggz1980 in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 11:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •