Member Avatar for krejar

This is a mess. What I want to do is make 1 program with the 3 loops in it do something different and produce an output of what each loop does.

Do loop: print out every other character
For loop: print out the string reversed
While loop: print out in reverse every other character

Example:

I enter something stupid like: now is the time
I get as a result: Do - nwi h ie
For - emit eht si won
While - ei h iwn

Now, I have no problem doing this in their own separate programs and using an array, but I am not allowed to do that this time. Also, I cannot use any reverse methods or arrays.

This is a mess, and as of posting this, I am still working with my partner trying to figure this out. I do realize I am missing a substantiate amount of code, but we are at a road block.

/* 
   January 19, 2012
	Lab 2 Part B
	Part B Program: Loops.java
*/

import java.util.Scanner;

public class Loops 
{
   public static void main(String[] args) 
	{
	   String input, output;
		int i = 0;
		Scanner scan = new Scanner(System.in);
      
		System.out.println("Enter something: ");
      input = scan.nextLine(); 
      
		
		output = "";
		
		do
		{
		   i = input.length() - 1;
		   output = output + input.charAt(i);   
			i--;
		}
		while (i <= 0);
		System.out.println("Every other character: " + output);
		
		for (i = input.length() - 1; i != -1; i--);
		{
		   output += input.charAt(i);
		} 
		
		
		while (i >= 0) 
		{
		   output = output + input.charAt(i);
			i--;
      }
	   System.out.println("Reversed every other character: " + output);
   }
}

This is a mess. What I want to do is make 1 program with the 3 loops in it do something different and produce an output of what each loop does.

Do loop: print out every other character
For loop: print out the string reversed
While loop: print out in reverse every other character

Example:

I enter something stupid like: now is the time
I get as a result: Do - nwi h ie
For - emit eht si won
While - ei h iwn

Now, I have no problem doing this in their own separate programs and using an array, but I am not allowed to do that this time. Also, I cannot use any reverse methods or arrays.

This is a mess, and as of posting this, I am still working with my partner trying to figure this out. I do realize I am missing a substantiate amount of code, but we are at a road block.

/* 
   January 19, 2012
	Lab 2 Part B
	Part B Program: Loops.java
*/

import java.util.Scanner;

public class Loops 
{
   public static void main(String[] args) 
	{
	   String input, output;
		int i = 0;
		Scanner scan = new Scanner(System.in);
      
		System.out.println("Enter something: ");
      input = scan.nextLine(); 
      
		
		output = "";
		
		do
		{
		   i = input.length() - 1;
		   output = output + input.charAt(i);   
			i--;
		}
		while (i <= 0);
		System.out.println("Every other character: " + output);
		
		for (i = input.length() - 1; i != -1; i--);
		{
		   output += input.charAt(i);
		} 
		
		
		while (i >= 0) 
		{
		   output = output + input.charAt(i);
			i--;
      }
	   System.out.println("Reversed every other character: " + output);
   }
}

well why not then write the 3 seperate programs and then try putting it to one? you could use substring() method to get each value or charAt() and then once you have the value alone check for doubles and eliminate them(you would check the string you are appending to for the doubles by using the contains() method), all those that are not doubles just get appended to a string and not added to an array-thats what i would do for the do loop, the while loop is easy just use substring() method starting at the last char of the string that you got from you do loop(which is each letter in a string in the correct order) and iterate until the start of the loop and append it to another string, this will then reverse its order, the same would apply for the for loop, except you will use the users input . hope that helps

the main issue is you use output and i for all 3 loops but never reset their values correctly before each different loop style (except for the "for" loop which does that by design), try setting output to an empty string after you print it out and this way they next loop will start working on its own string building task.

second mistake : you do-while goes backwards,and doesnt skip a letter (tip : play with "%" operator for skipping some iterations)

you for seems alright although i dislike the condition you set for it, i'd use "i>=0" but yours should still work.

the while is the big mistake :
first it doesnt skip letters but its creating an infinite loop...

after your for is completed, i equals -1 which is the condition for it to exit, then you start a while(i>=0) that does i--; for each iteration , going far far away from its condition to stop...

you're pretty close tho, think it through and it'll be a done project in no time!

Member Avatar for krejar

Well, I figured out that I need to declare 3 different string inputs for all three loops. I found out that with my current set up, with just the one string input, when I go to run the program, it mutates my string through all the loops and the final result is something funky.

Still plugging away at this.

like i said, your printing your output string between loops, just reset it to an empty string between loops after printing it , and you won't need to declare 3 strings...

String output = "";
//loop 1
System.out.println(output);

output = "";
//loop 2
System.out.println(output);

output = "";
//loop 3
System.out.println(output);
Member Avatar for krejar

Yep, I got it working last night. Resetting the loops was the key.

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.