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:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Help with a loop based code

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
So I'm learning Java, and it has been hard, mostly because the stupid text book and teacher are not exactly helpful. However I am doing very well in it,

but there are certain specific questions I have had for a while that the book never addresses. I would be so appreciative for some help. I don't want anyone to solve my problems for me, but certain java mechanics are unclear to me.

So firstly what I am trying to do is, as the exercise prompts, "write a program that displays these two tables side by side."

It looks like this (spacing and alignment is different but I can handle that, this is just to give you an idea of what I'm ultimately trying to do).

Kilograms Pounds | Pounds Kilograms
1 2.2 | 20 9.09
3 6.6 | 25 11.36
...
197 433.4 | 510 231.82
199 437.8 | 515 234.09

So of course I understand the math involved, but the book gives no examples that really address certain issues I'm having.

it only mentions how to do increments of +1 like for example in this for statement:

"for (int i = 1; i <= 9; i++) {"

So the first issue is that yeah i++ is only an increment of 1, how do I change them to 2 for the kilograms and 5 for the pounds??

ALSO, creating the right loop to do this line by line, in which each line includes not just the first table but the second table, how do I set up the for statement to set more than one value for each part of it, or is this doable with nesting for statements?

I'm not even entirely sure I'f I'm asking the right questions, but I would greatly appreciate some direction as the book gives absolutely none...

Sheriff
Posts: 7125
184
• Number of slices to send:
Optional 'thank-you' note:
A for loop has a lot of flexibility. You can increment/decrement your variable by any amount:

or shorter

You can initialize and increment more than one variable:

Tommy Johnson
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:

Knute Snortum wrote:A for loop has a lot of flexibility. You can increment/decrement your variable by any amount:

or shorter

You can initialize and increment more than one variable:

Ohhhhhh I just had an epiphany, with the first thing you said, the last part of the for statement is what action to do after each iteration of the loop, so yeah of course just put that variable + whatever I want the increment to be. Thanks so much!!

And so for the last code you put... basically I just separate things by a comma? In eclipse when I tried that it's like when it recognizes the variable as such, it changes color and it didn't seem to do that for me. I dunno I'll try again here and post my code.

Tommy Johnson
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
Also,

if I need to use double values instead of int that shouldn't make a difference right?

Knute Snortum
Sheriff
Posts: 7125
184
• Number of slices to send:
Optional 'thank-you' note:
Your welcome, and Welcome to the Ranch, BTW.

The for loop with multiple variables should work in Eclipse. Yes, just separate the variables with commas.

Knute Snortum
Sheriff
Posts: 7125
184
• 1
• Number of slices to send:
Optional 'thank-you' note:

Tommy Johnson wrote:if I need to use double values instead of int that shouldn't make a difference right?

No, doubles work fine.

Tommy Johnson
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
Okay something still wrong with this and I don't know why.

I have no idea why the code thing didn't work at the top before you post.. the spacings are different but the line that has an issue is the "for ( double k = 1, double p = 20; k <= 199, p <= 515; k = k + 2, p = p + 5) {"

Knute Snortum
Sheriff
Posts: 7125
184
• Number of slices to send:
Optional 'thank-you' note:
Two thing before the response:

* You're still not using code tags correctly. The code goes between the starting tag and the end tag.
* Try not to say, "Something's wrong." Say what's wrong, like an error message.

Now, the two things I can see that are a problem with the for loop are that you can't have a comma in the "while" part of the for loop (the middle). Pick one variable. Second, literal doubles need a decimal point, so write 1.0, not 1. See how that works for you.

Knute Snortum
Sheriff
Posts: 7125
184
• Number of slices to send:
Optional 'thank-you' note:

I have no idea why the code thing didn't work

Tommy Johnson
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:

there we go lol

Saloon Keeper
Posts: 10649
83
• Number of slices to send:
Optional 'thank-you' note:

Knute Snortum wrote:Now, the two things I can see that are a problem with the for loop are that you can't have a comma in the "while" part of the for loop (the middle). Pick one variable. Second, literal doubles need a decimal point, so write 1.0, not 1. See how that works for you.

In the while part you can replace the comma with &&.
Assigning an integer to a double is not an issue, though, me personally, I would put 1.0 just to be a visual reminder that we're dealing with doubles.

Marshal
Posts: 79045
375
• Number of slices to send:
Optional 'thank-you' note:

Knute Snortum wrote: . . .
* You're still not using code tags correctly. The code goes between the starting tag and the end tag.
. . .

I have corrected the code tags, and also removed some unnecessary tab characters to improve the indentation.

And welcome to the Ranch again.

Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
And use \t instead of :

Like this:

\t is a escape sequence
Here is the link for more escape sequences = https://docs.oracle.com/javase/tutorial/java/data/characters.html

Campbell Ritchie
Marshal
Posts: 79045
375
• Number of slices to send:
Optional 'thank-you' note:

RahulRise Das wrote:And use \t instead of . . .

That assumes OP wants a tab character rather than those spaces.

Ray Anderson
Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
Yes, \t is looks better than spaces,

please i'm just helping you all and please correct me if i'm wrong, also i don't like to disappoint people.

Ray Anderson
Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
It's ok, Tommy to test yourself but i suggest you use arrays for this program which is much more accurate than assigning instance variable range, Here is my code hope it helps :

One more advice : Try to pickup the logic of the program, I was just like you trying every possible way to not to look at answers, but then things were going slow for me. Then I started reading the logic of all programs in the book and I started applying those concepts to some the coding sites like CodingBat, Project Euler (good for coding and math) and then there is Codechef which is pretty tough you have to mind all things like code memory limit, line limit those kinda stuff.

Hope My Message Helped,
Thanks

Bartender
Posts: 3323
86
• Number of slices to send:
Optional 'thank-you' note:

RahulRise Das wrote:It's ok, Tommy to test yourself but i suggest you use arrays for this program which is much more accurate than assigning instance variable range

Can you explain why you think using arrays is "much more accurate".

The following code with no arrays gives identical results to your code:

Ray Anderson
Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
Tommy's code needed refinement and the code has to be written properly.

And your code proves it compared to his and mine.

Tony Docherty
Bartender
Posts: 3323
86
• Number of slices to send:
Optional 'thank-you' note:

RahulRise Das wrote:Tommy's code needed refinement and the code has to be written properly.

And your code proves it compared to his and mine.

Unfortunately your code does not add refinement nor is it written properly and neither your code nor my rewrite or your code helps the OP as it doesn't provide the correct answer to his problem.
Please be careful when giving advice as we don't want to unintentionally confuse the OP.

 Do Re Mi Fa So La Tiny Ad a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com