Can't get my do loop to work

Page 1 of 1

14 Replies - 1009 Views - Last Post: 12 April 2008 - 05:50 PM Rate Topic: -----

#1 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Can't get my do loop to work

Post icon  Posted 11 April 2008 - 05:45 AM

So I posted a problem I was having with a for loop awhile ago. After much tinkering around, I managed to straighten it out and get it working. I decided that I wanted to add a loop to my program so that after the for loop had executed the program asks the user if they want their program to loop back to the beginning (depending on if they enter y/n).

My code so far is:
import java.util.Scanner;
public class StringShirt {
  
  public static void main(String[] args) 
  {
	final char LOOP = 'y';
	Scanner sc;
	String toEncode;
	String tempDelim;	  
	int offset;  
	char delim;   
	char currChar; 	
	boolean punctBool; 
	String outputStr = ""; 
	String temp;
	
	
	sc = new Scanner(System.in);
	
	System.out.print("Please enter a string to encode: ");
	toEncode = sc.nextLine().toUpperCase();
	System.out.print("Please enter the delimiter you wish to use: ");
	tempDelim = sc.nextLine();
	delim = tempDelim.charAt(0);
	System.out.print("Please enter the amount you wish to shift by: ");
	offset = sc.nextInt();	
	System.out.print("True or false -- Would you like to include punctuation: ");
	punctBool = sc.nextBoolean();
	
	System.out.println("\nEncoding: " + toEncode);
	System.out.println("Letters will be shifted by: " + offset);
	System.out.println("Words delimited by: " + delim);
	System.out.println("Including punctuation: " + punctBool);
	
	do
	{ 
	for (int i = 0; i < toEncode.length(); i++) 
	{
	currChar = toEncode.charAt(i);
	  
	  if (Character.isLetter(currChar)) 
	  {
		int character_number = ((int) currChar + offset - (int) 'A');	
		int new_character_number = character_number % 26;	
		outputStr += (char) (new_character_number + (int) 'A');
	  } 
	  else if (Character.isDigit(currChar)) 
	  {
		outputStr += currChar;
	  } 
	  else if (currChar == ' ')
	  {
		outputStr += delim;
	  }
	  else if (punctBool)
	  {
		outputStr += currChar;
	  }
	}
	
	System.out.println("\nThe encoded string is: " + outputStr);
	
	  System.out.print("\nAgain? (y/n): ");
	temp = sc.nextLine();
}
	while (LOOP == 'y');
  }
}


Instead of looping back the beginning of the program, it's just adding to the already encoded output that the for loops have produced. Can anyone point out why this is happening? PLeaaaaase, it's driving me crazy.

Is This A Good Question/Topic? 0
  • +

Replies To: Can't get my do loop to work

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Can't get my do loop to work

Posted 11 April 2008 - 10:28 AM

You have to reset outputStr to "" at each iteration of the loop.

	
   do
   {
	   outputStr = ""; 
	   for (int i = 0; i < toEncode.length(); i++)





and at the end it is not

while(LOOP == 'y') which will always be true

but

while(temp == LOOP)
Was This Post Helpful? 0
  • +
  • -

#3 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Can't get my do loop to work

Posted 11 April 2008 - 03:45 PM

Thanks for the reply. I changed my last bit of code to:
	temp = sc.nextLine();
	temp2 = temp.charAt(0);
}
	while (temp2 == LOOP);
  }
}


temp2 is a char, but it still doesn't work. It compiles, but once it reaches the loop part while it's actually running it says "Exception in thread "main" java.lang.StringIndexOutOfBoundsException on: String index out of range 0". I can't use the code you gave me because they're incompatible types. I'm soooo close.

This post has been edited by skin_: 11 April 2008 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Can't get my do loop to work

Posted 11 April 2008 - 03:48 PM

View Postskin_, on 11 Apr, 2008 - 03:45 PM, said:

Thanks for the reply. I changed my last bit of code to:
	temp = sc.nextLine();
	temp2 = temp.charAt(0);
}
	while (temp2 == LOOP);
  }
}


temp2 is a char, but it still doesn't work. I can't use the code you gave me because they're incompatible types. I'm soooo close.


Try

tmp = sc.next(); // instead of nextLine()

or

while(sc.next().charAt(0) == LOOP);
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Can't get my do loop to work

Posted 11 April 2008 - 03:52 PM

Or in addition to that since you have defined temp as a string...

while (temp.equals("y"))

Then you can just keep nextLine(). The problem here is that you are attempting to compare the incoming string of nextLine (and into temp) against the char. If you just take temp and compare it to a string of "y" then you should have no problems here. This will also allow you to get rid of LOOP as a variable if you like.

On a side note, keep track of case too. Never know when a user may type a capital.

:)

This post has been edited by Martyr2: 11 April 2008 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Can't get my do loop to work

Posted 11 April 2008 - 04:01 PM

Thank you so much for the replies guys. This is my last question I PROMISE! :D
I got it to compile, run and ask if the user wants to repeat, but now when they enter 'y' it repeats the output, not the beginning of the loop. :blink:
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Can't get my do loop to work

Posted 11 April 2008 - 04:05 PM

View Postskin_, on 11 Apr, 2008 - 04:01 PM, said:

Thank you so much for the replies guys. This is my last question I PROMISE! :D
I got it to compile, run and ask if the user wants to repeat, but now when they enter 'y' it repeats the output, not the beginning of the loop. :blink:

Never say "This is my last question I PROMISE!"

I guess you'll have to move your

do
{

just before

System.out.print("Please enter a string to encode: ");
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Can't get my do loop to work

Posted 11 April 2008 - 04:25 PM

pbl is exactly right. You have to prompt for the new string at the beginning of your do loop and remove the prompt before the do loop (otherwise you will be asking them twice) or you can put the prompt at the bottom of your do while loop.

Now I am not sure if you have solved it yet, but you probably ran into a problem with it skipping past your "Again (y/n)" line and that is because of how you are collecting info using the nextBoolean which leaves a carriage return in the stream, of course eaten up later by the nextLine() call and considered your choice in the Y/N question.... ultimately determining it is N and leaves the program.

So watch out for that. :)
Was This Post Helpful? 0
  • +
  • -

#9 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Can't get my do loop to work

Posted 11 April 2008 - 04:44 PM

Thank you, I have fixed ALMOST everything up now. I have one last thing and that is about when the program initially asks the user to input their string. The first loop, the program is fine and runs through perfectly [thankyou! :D] but once I say I would like to loop it returns back to the start okay, but after I have entered in a string it may skip a few lines. It is grouping two questions together into the one statement, so I can't actually answer the first question. Eg. "Please enter a string you with to encode: Please enter the delimiter you wish to use: " all on the one line. :crazy:
Was This Post Helpful? 0
  • +
  • -

#10 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Can't get my do loop to work

Posted 11 April 2008 - 04:50 PM

Please refer to my previous post about the possible problem you will come across. This is the reason why you are getting that issue. Did I predict the future? Perhaps I had. That is kinda scary. oooooohhhhh

B)
Was This Post Helpful? 0
  • +
  • -

#11 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Can't get my do loop to work

Posted 11 April 2008 - 05:00 PM

Give this a whirl...

import java.util.Scanner;

public class StringShirt {
  
  public static void main(String[] args)
  {
    Scanner sc;
    String toEncode;
    String tempDelim;      
    int offset;  
    char delim;  
    char currChar;     
    boolean punctBool;
    String temp;
    
    
    sc = new Scanner(System.in);
    

    do
    {
    	// Clear the string first
    	String outputStr = "";

    	// Setup our prompts
    	System.out.print("Please enter a string to encode: ");
    	toEncode = sc.nextLine().toUpperCase();
    	System.out.print("Please enter the delimiter you wish to use: ");
    	tempDelim = sc.nextLine();
    	delim = tempDelim.charAt(0);
    	System.out.print("Please enter the amount you wish to shift by: ");
    	offset = sc.nextInt();    
    	System.out.print("True or false -- Would you like to include punctuation: ");
    	punctBool = sc.nextBoolean();

    	// Eat up the carriage return left behind
    	sc.nextLine();

    	// Show our stats
    	System.out.println("\nEncoding: " + toEncode);
    	System.out.println("Letters will be shifted by: " + offset);
    	System.out.println("Words delimited by: " + delim);
    	System.out.println("Including punctuation: " + punctBool);
    
    	
    	// Do the encoding
    	for (int i = 0; i < toEncode.length(); i++)
    	{
    		currChar = toEncode.charAt(i);
      
      		if (Character.isLetter(currChar))
      		{
        		int character_number = ((int) currChar + offset - (int) 'A');    
        		int new_character_number = character_number % 26;    
        		outputStr += (char) (new_character_number + (int) 'A');
      		}
      		else if (Character.isDigit(currChar))
      		{
        		outputStr += currChar;
      		}
      		else if (currChar == ' ')
      		{
        		outputStr += delim;
      		}
      		else if (punctBool)
      		{
        		outputStr += currChar;
      		}
    	}
    
    	System.out.println("\nThe encoded string is: " + outputStr);
    
    	// Prompt to repeat
    	System.out.print("\nAgain? (y/n): ");
    	temp = sc.nextLine();

    	// If yes, go back to top of do while loop and re-prompt
    } while (temp.equals("y"));
  }
}



You should find that does most of what you wanted yes? Enjoy! :ph34r:
Was This Post Helpful? 0
  • +
  • -

#12 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Can't get my do loop to work

Posted 11 April 2008 - 05:20 PM

:D :D :D !!!!!
Thank you so much.
Was This Post Helpful? 0
  • +
  • -

#13 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Can't get my do loop to work

Posted 11 April 2008 - 07:39 PM

How would I go about setting up a counter so that when the user enters 'n' it displays how many times the loop has been performed.
Declare an int called loopCount, set initial value to 0, if loop is performed loopCount++. But where do I put this?
Was This Post Helpful? 0
  • +
  • -

#14 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Can't get my do loop to work

Posted 12 April 2008 - 12:09 AM

Pretty please? :D
Was This Post Helpful? 0
  • +
  • -

#15 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Can't get my do loop to work

Posted 12 April 2008 - 05:50 PM

I would have thought putting this if statement:
	if (loop.equalsIgnoreCase("y"))	
	{
		loopCount++;
	}
	else
	{
		System.out.println(loopCount);
	}


after:
	System.out.print("\nAgain? (y/n): ");  
loop = sc.nextLine(); 



Would have worked, but the int loopCount stays at it's initial value of 0.
:crazy:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1