• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with a loop based code  RSS feed

 
Tommy Johnson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Thanks in advance.




 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,

if I need to use double values instead of int that shouldn't make a difference right?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea why the code thing didn't work

Follow this link for how to UseCodeTags.
 
Tommy Johnson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


there we go lol
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!