Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cycle through Chars?  RSS feed

 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, guys. I've been stuck on this coding for days now. I needed to create a hail stone sequence, with my name embedded, like:

J 10 is an even number, so I take half: 5
O 5 is an odd number, so I make 3n + 1: 16
H 16 is an even number, so I take half: 8
N 8 is an even number, so I take half: 4
(TAB) 4 is an even number, so I take half: 2
D 2 is an even number, so I take half: 1
O The process took 6 steps to reach 1

However, with my code, all I get is:
J10 is an even number, so I take half: 5
J5 is an odd number, so I make 3n + 1: 16
J16 is an even number, so I take half: 8
J8 is an even number, so I take half: 4
J4 is an even number, so I take half: 2
J2 is an even number, so I take half: 1
The process took 6 steps to reach 1

Here's my code so far:


Thanks!
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line:

you will notice only references the char at the 0 position, if you want it to print along the entire string, you need to make it a variable index and increment it ever time through the loop.
 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:This line:

you will notice only references the char at the 0 position, if you want it to print along the entire string, you need to make it a variable index and increment it ever time through the loop.
I tried using
, but that didn't work.

This was my output:
Please enter a number to start hail stone sequence: 42
John Dirt42 is an even number, so I take half: 21
John Dirt21 is an odd number, so I make 3n + 1: 64
John Dirt64 is an even number, so I take half: 32
John Dirt32 is an even number, so I take half: 16
John Dirt16 is an even number, so I take half: 8
John Dirt8 is an even number, so I take half: 4
John Dirt4 is an even number, so I take half: 2
John Dirt2 is an even number, so I take half: 1
The process took 8 steps to reach 1
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct. Your code you just listed you added another loop in the way of the "for" structure and you have it print along the entire string length, then proceed to the input part of your solution. Step through the code by hand--mental exercise there. If you cannot, then learn to use the debugger--it will be your strongest friend until you can visualize what is happening yourself.

You have the basic loop in your OP, you don't need another. What you do need is to change the chatAt(0) to a charAt(myVariable) and increment myVariable each time you go though your input loop.

Chase Harris wrote:
Les Morgan wrote:This line:

you will notice only references the char at the 0 position, if you want it to print along the entire string, you need to make it a variable index and increment it ever time through the loop.
I tried using
, but that didn't work.

This was my output:
Please enter a number to start hail stone sequence: 42
John Dirt42 is an even number, so I take half: 21
John Dirt21 is an odd number, so I make 3n + 1: 64
John Dirt64 is an even number, so I take half: 32
John Dirt32 is an even number, so I take half: 16
John Dirt16 is an even number, so I take half: 8
John Dirt8 is an even number, so I take half: 4
John Dirt4 is an even number, so I take half: 2
John Dirt2 is an even number, so I take half: 1
The process took 8 steps to reach 1
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chase,

Here is a very basic loop structure and explanation of how it works.
 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Chase,

Here is a very basic loop structure and explanation of how it works.
That really helped! Thank you. I still have a few small problems with my output though.

Please enter a number to start hail stone sequence: 54
J 54 is an even number, so I take half: 27
O 27 is an odd number, so I make 3n + 1: 82
E 82 is an even number, so I take half: 41
41 is an odd number, so I make 3n + 1: 124
D 124 is an even number, so I take half: 62
I 62 is an even number, so I take half: 31
R 31 is an odd number, so I make 3n + 1: 94
T 94 is an even number, so I take half: 47
The input entered is not a number. Please run again.

It doesn't go through the whole sequence and at the bottom it says, "The input entered is not a number. Please run again."

Here's my coding:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the string "JOE DIRT", with eight characters.

Each time you loop, you increment i, and use it to find a character in the string.

Your loop had run eight times, and is still not done.

what happens on this line when i = 8?

System.out.print(s.charAt(i)+ " ");

and remember, the characters are in positions 0-7.
 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:You have the string "JOE DIRT", with eight characters.

Each time you loop, you increment i, and use it to find a character in the string.

Your loop had run eight times, and is still not done.

what happens on this line when i = 8?

System.out.print(s.charAt(i)+ " ");

and remember, the characters are in positions 0-7.
I just noticed that for "if(i==s.length()) bLoop = false;" it has a yellow underline, which means it isn't being used. I'm guessing that's the problem?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'll ask again...

What character will this return (or what will happen), if s contains "JOE DIRT"?



here's a big hint:

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chase,

In your algorithm you cannot guarantee that you have a halting condition at the end of your String length, one thing you may do to elevate the unwanted consequences is to make your index circular--you can mod the index by the string length value, that will always guarantee that your index with be within the range of 0 to K-1, but your string will continuously cycle the output until you exit.

Les

Chase Harris wrote:
fred rosenberger wrote:You have the string "JOE DIRT", with eight characters.

Each time you loop, you increment i, and use it to find a character in the string.

Your loop had run eight times, and is still not done.

what happens on this line when i = 8?

System.out.print(s.charAt(i)+ " ");

and remember, the characters are in positions 0-7.
I just noticed that for "if(i==s.length()) bLoop = false;" it has a yellow underline, which means it isn't being used. I'm guessing that's the problem?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!