Chase Harris

Greenhorn

Posts: 19

posted 1 year ago

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!

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!

posted 1 year ago

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.

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.

Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH

Chase Harris

Greenhorn

Posts: 19

posted 1 year ago

, 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

I tried usingLes 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.

, 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

posted 1 year ago

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.

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:I tried usingLes 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.

, 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

Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH

Chase Harris

Greenhorn

Posts: 19

posted 1 year ago

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:

That really helped! Thank you. I still have a few small problems with my output though.Les Morgan wrote:Chase,

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

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:

posted 1 year ago

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.

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Chase Harris

Greenhorn

Posts: 19

posted 1 year ago

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 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.

posted 1 year ago

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

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: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 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.

Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH