programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# Cycle through Chars?

Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
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!

Rancher
Posts: 1006
26
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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: 1006
26
• Number of slices to send:
Optional 'thank-you' note:
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: 1006
26
• Number of slices to send:
Optional 'thank-you' note:
Chase,

Here is a very basic loop structure and explanation of how it works.

Chase Harris
Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:

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:

lowercase baba
Posts: 13018
66
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 13018
66
• Number of slices to send:
Optional 'thank-you' note:

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

here's a big hint:

Les Morgan
Rancher
Posts: 1006
26
• Number of slices to send:
Optional 'thank-you' note:
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?

 You showed up just in time for the waffles! And this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton