14 Replies - 1308 Views - Last Post: 06 October 2012 - 01:31 PM Rate Topic: -----

#1 breakingaway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 04:56 PM

Hi! This is my first post here and I'm sorry if this is long. I'm really confused.

Okay, so we were told to make a program that takes user inputted word and then formats it so it repeats vertically and horizantally but also adjusts one character each time.
Kind of like this.

Word: MARY

MARY
ARYM
RYMA
YMAR

The word can be as long as the user desires.

The instructions don't say to use a specific type of loop, just as long as nested loops are used.

I did a for loop but my professor said that he believed that a charAt method would be easier where each letter was assigned a number and then the number was adjusted each time. I didn't really understand his approach so it's hard for me to repeat it. My professor said that the problem was simple and I was over-thinking it. I was leaning towards using two for loops, one for the inside and one for the outside. I had a for loop that will do the following output:

MARY
ARY
RY
R

So i'm trying to figure out how to add the remaining letters.
I've tried a couple of different things so i've commented out what hasn't worked.
//Assign Variables and Accept input from the keyboard
		
String sentence;
		int min=0; int max;int k = 0; int differenceinlength;
		Scanner keyboard= new Scanner (System.in);
		System.out.println("Enter a string: ");
		sentence = keyboard.next();
		max= sentence.length();
		
		
		int i=sentence.charAt(0);
		for (i=0; i<=max; i++){  
			System.out.println(sentence.substring(i));
		 //if (sentence2.length()<sentence.length())
		 	//k=sentence2.length();
		 	//differenceinlength=i-k;
		 //int characteratdiff=sentence2.charAt(differenceinlength);
		 //int j=sentence2.charAt(0);
		 //for (j=0; j<=max; j++)
		 //{ System.out.println(sentence.substring(i)+sentence2.substring(characteratdiff));}

		}}}




This gives me
MARY
ARY
RY
R
as a output.

If you could please tell me what direction to go in I would really appreciate it!

Sincerely,
Overwhelmed

Is This A Good Question/Topic? 0
  • +

Replies To: Help with creating a program that repeats a word but moves one letter

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 05:04 PM

If you have an array of all the chars in the word, you can simply change what indexes hold what.
Was This Post Helpful? 1
  • +
  • -

#3 breakingaway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 05:19 PM

Thanks for the reply. We haven't learned about Array's yet... we're in chapter 4 and that's in chapter 7. I'm going to go ahead and read the chapter to see if that simplifies it for me. Do you know any way to do it without an array?
Was This Post Helpful? 0
  • +
  • -

#4 DoinWork  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 69
  • Joined: 26-October 11

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 06:00 PM

For something like:
MARY
ARYM
RYMA
YMAR

You could just get the length of the one they entered, then with the for loop as you increment mod 4 your index and it should work as you desire in a circular shift.
Was This Post Helpful? 1
  • +
  • -

#5 breakingaway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 06:07 PM

What does "mod 4 your index" mean? Like, use the modulas operator?
Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 06:33 PM

Yes, modulus operator. Quick question though, why use it? Couldn't he use the == operator instead? I got curious and made your program with a while and for loop, with two counters, not counting the one in the for loop.
Was This Post Helpful? 1
  • +
  • -

#7 DoinWork  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 69
  • Joined: 26-October 11

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 06:51 PM

From my understanding the mod would be essential, here's a quick test program(ugly as it may be) demonstrating on the word mary.

public class MaryTest
{
  public static void main(String[] args)
  {
		MaryTest test = new MaryTest();
  }
public MaryTest()
{
	String testStr= "MARY";
	System.out.println(testStr);
	System.out.println("=====");

	for(int i=0;i<testStr.length();i++)
	{
		String newStr="";
		newStr+=testStr.charAt(i % 4);
		newStr+=testStr.charAt((i+1) % 4);
		newStr+=testStr.charAt((i+2) % 4);
		newStr+=testStr.charAt((i+3) % 4);
		System.out.println(newStr);
	}
}
}



Output:
MARY
=====
MARY
ARYM
RYMA
YMAR


This post has been edited by DoinWork: 03 October 2012 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 06:59 PM

I would never have thought of doing it that way. Why not use a for loop inside the newStr and modulus operations? Also, the 4 isn't going to be a literal right?

I did it the old fashioned way, checking for the position with ifs. If the position is greater than the current counter, print away. If position reaches the length of the word, reset the position to zero, then print away again.
Was This Post Helpful? 2
  • +
  • -

#9 DoinWork  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 69
  • Joined: 26-October 11

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 07:05 PM

Yeah, a inner for loop(for the i+1..etc, instead of the 1 would be the variable) would make it a lot cleaner I just was trying to verify my thinking to myself. The 4 would be equal to the string length as well as the outer loop.
Was This Post Helpful? 0
  • +
  • -

#10 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 07:21 PM

You can use the iterator of the for loop for the variable inside i + n too, where n is the iterator. ^^
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 08:09 PM

*
POPULAR

View PostDoinWork, on 03 October 2012 - 09:51 PM, said:

From my understanding the mod would be essential, here's a quick test program(ugly as it may be) demonstrating on the word mary.

Your % 4 is hardcoded based on the String length
you also use it 4 times based on the String length
Unless you write a different version of your program for every possible String length, don't really how your % can be useful :)

If you can't use array
loop String length() time
extract String.substring(1) and append to it String.substring(0,1)
replace the original String

that should do it

		String str = "mary";
		for(int i = 0; i < str.length(); ++i) {
			System.out.println(str);
			str = str.substring(1) + str.substring(0,1);
		}


This post has been edited by pbl: 03 October 2012 - 08:11 PM

Was This Post Helpful? 6
  • +
  • -

#12 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 08:19 PM

Holy smokes, that is really simple! I would give you one but I'm maxed. Tomorrow it is. ^^
Was This Post Helpful? 1
  • +
  • -

#13 breakingaway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 09:12 PM

Thank you so much, especially pbl. The code worked and gave the needed output. I'm kind of confused though on what exactly this line did.

 str = str.substring(1) + str.substring(0,1); 


Could you please explain this to me?

Thanks again for all the help, I really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#14 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Help with creating a program that repeats a word but moves one letter

Posted 03 October 2012 - 10:10 PM

The method of String substring takes an argument of an index and returns everything on that index up until the end of the string or two arguments, a start and an end index and returns the resulting String.

So:
str = "mary"
str = str.substring(1) + str.substring(0,1);
str = "ary" + "m" // "arym" carries over

str = "arym"
str = str.substring(1) + str.substring(0,1);
str = "rym" + "a" // "ryma" carries over again, repeat until loop is done


Was This Post Helpful? 2
  • +
  • -

#15 breakingaway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Re: Help with creating a program that repeats a word but moves one letter

Posted 06 October 2012 - 01:31 PM

Thanks, fromTheSprawl! That makes more sense!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1