1. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

Highlight every third line.

For my assignment, I have to prompt the user for a positive integer, then display a number of lines equal to the integer given. That part is easy. The part that's driving me crazy is where I have to highlight every third line, creating something like this:

I like the fall
I like the fall
I like the fall***
I like the fall
I like the fall
I like the fall***
I like the fall
I like the fall
I like the fall***

Here is what I've got so far:
Java Code:
```import java.util.Scanner;

public class YayFall {

public static void main(String[] args) {
int number, i;
String fall = "I like the Fall.";
Scanner scan = new Scanner( System.in );

System.out.print (" Enter an integer greater than 0 --> ");
number = scan.nextInt();

for (i=1; i<number; i++)
System.out.println(fall);
}
}```
Any help would be greatly appreciated, as I have to leave for work soon and this assignment is due in a couple days.

2. Re: Highlight every third line.

Are you looking for code that counts to three repeatedly?

3. Member
Join Date
Oct 2012
Posts
36
Rep Power
0

Re: Highlight every third line.

3 % 3 = 0, 6 % 3 = 0, 9 % 3 = 0, ... (% = modulo)

4. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

Re: Highlight every third line.

Norm: yes, something that automatically picks out every 3rd line. Jbelg's suggestion--which I appreciate--wouldn't work because there's no set number of lines.

5. Member
Join Date
Oct 2012
Posts
36
Rep Power
0

Re: Highlight every third line.

How do you mean no set numbers of lines?

Java Code:
```for (i=1; i<number; i++) {
System.out.println(fall + (i%3==0)?"HIGHLIGHT":"DON'T HIGHLIGHT");
// Every third line the result will be 0 so it will add HIGHLIGHT, otherwise it adds DON'T HIGHLIGHT
}```
It's just an example and the 3 can be replaced to be any xTH line.
Last edited by JBelg; 11-03-2013 at 07:42 PM.

6. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

Re: Highlight every third line.

Originally Posted by JBelg
How do you mean no set numbers of lines?

Java Code:
```for (i=1; i<number; i++) {
System.out.println(fall + (i%3==0)?"HIGHLIGHT":"DON'T HIGHLIGHT");
// Every third line the result will be 0 so it will add HIGHLIGHT, otherwise it adds DON'T HIGHLIGHT
}```
It's just an example and the 3 can be replaced to be any xTH line.
Now I'm getting "Incompatible types" errors between the boolean and the string statement...

7. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

Re: Highlight every third line.

Originally Posted by Reptyle216
Now I'm getting "Incompatible types" errors between the boolean and the string statement...
But I'm sure you can fix that yourself with a little experimentation, no need to wait for someone to come and hand you the solution right? I mean you are not going to use this thread to just outsource the fixing of each and every compile error you get, that would just be a total misuse of the forum and destructive to your own learning process.

8. Re: Highlight every third line.

The compiler is confused. It expects a String, but is given a boolean. You need extra braces around the ternary statement:
Java Code:
`System.out.println(fall + ( i%3==0 ? "HIGHLIGHT" : "DON'T HIGHLIGHT"));`
Edit: sorry gimbal, I was in a good mood... And you posted *before* me, again.... :D

9. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

Re: Highlight every third line.

Originally Posted by SurfMan
The compiler is confused. It expects a String, but is given a boolean. You need extra braces around the ternary statement:
Java Code:
`System.out.println(fall + ( i%3==0 ? "HIGHLIGHT" : "DON'T HIGHLIGHT"));`
Edit: sorry gimbal, I was in a good mood... And you posted *before* me, again.... :D
I see what it is now, thanks. I can take it from here.

And Gimbal, was it necessary for you to be so rude? I noticed you got belligerent with another poster too earlier. Just because someone needs help with part of an assignment doesn't mean they're "outsourcing" their homework. If you're not going to say anything helpful, then don't say anything at all.

10. Re: Highlight every third line.

Originally Posted by Reptyle216
I see what it is now, thanks. I can take it from here.

And Gimbal, was it necessary for you to be so rude? I noticed you got belligerent with another poster too earlier. Just because someone needs help with part of an assignment doesn't mean they're "outsourcing" their homework. If you're not going to say anything helpful, then don't say anything at all.
You have to understand that the regular posters, of which gimbal is one (at this point 1,414 posts), are confronted on a daily basis with lazy slobs that don't study and dump their shit in here. Literally. Sometimes there is not even a question, just a copy/paste from the email of the professor. They then always expect "us" to clean up their mess, or do it all for them. They are in for a massive flame. I have seen plenty contributions of gimbal with constructive comments, good examples, etc. So don't take it out on him :)

There's two reasons I can think of, why gimbal reacted this way:

1) "as I have to leave for work soon and this assignment is due in a couple days."
That is not my problem. I am not your hired pet programmer. If you're late then that's your fault, not mine.

2) "Now I'm getting "Incompatible types" errors between the boolean and the string statement... "
That 99% of the time points out a person that is too lazy to experiment with his/her own code and asks the forum to solve a relative simple error, which they have easily could have solved themselves with a little effort.

If these two reasons are not applicable to you then yes, you might be the victim of some misdirected rage. Consider it wisdom for future posts.

11. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

Re: Highlight every third line.

Plus rudeness is a highly personal thing. I've seen people find it rude that someone wished them a good morning. You might also think that I have your best interest at heart in stead of defaulting to the negative, which only reflects poorly on yourself.

Originally Posted by SurfMan
You have to understand that the regular posters, of which gimbal is one (at this point 1,414 posts)
Well on this forum anyway, consider me "imported" from the OTN forums where I harassed people quite a bit longer already ;)

12. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

Re: Highlight every third line.

1) "as I have to leave for work soon and this assignment is due in a couple days."
That is not my problem. I am not your hired pet programmer. If you're late then that's your fault, not mine.
I came here as a last resort. I had been working on the homework assignment all weekend (I go to work and school), and I still wasn't getting it by yesterday morning. At that point I had to leave for my job and couldn't tinker with it further, so I came here asking for suggestions. I probably would have signed up for a Java help forum anyway, since this could be a valuable resource in my future studies.

2) "Now I'm getting "Incompatible types" errors between the boolean and the string statement... "
That 99% of the time points out a person that is too lazy to experiment with his/her own code and asks the forum to solve a relative simple error, which they have easily could have solved themselves with a little effort.
Notice the significant amount of time between JBelg's response and my own. I was experimenting with it, and even checked my textbook and various online sources to see if I was missing something about boolean->string conversion. That's when I came back asking about the error, only to find out it was justa misplaced parenthesis (always the little things that get past me). I didn't just run it once and say "It didn't work." All he had to say was "Check the parenthesis" or something along those lines. I didn't need a damn lecture from him about my study habits; that was just ignorant.

Nonetheless, I'm understanding loops a lot better now, and I appreciate the help Surfman, JBelg, and Norm. :)

13. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

Re: Highlight every third line.

Originally Posted by gimbal2
Plus rudeness is a highly personal thing. I've seen people find it rude that someone wished them a good morning. You might also think that I have your best interest at heart in stead of defaulting to the negative, which only reflects poorly on yourself.
If you're going to blame me for being offended by your attitude, then I have no further interest in this discussion.

14. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

Okay.

15. Re: Highlight every third line.

Darn, another case of "we agree to disagree"; I want to see dog fights; blood and decapitation.

kindest regards,

Jos ;-)

16. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

Re: Highlight every third line.

Bastard! you just like to watch from the sidelines and egg people on while holding your finger on the ban button.

17. Re: Highlight every third line.

Careful JosAH. One of the dogs may turn on the spectators.

18. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

Re: Highlight every third line.

You know what, I'll give in! Woof, woof woof woof!

19. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,655
Rep Power
9

Re: Highlight every third line.

Originally Posted by Reptyle216
And Gimbal, was it necessary for you to be so rude?
The word rude is not appropriate here. Harsh comes to mind. But Gimbal wasn't harsh. If you think he was, then fasten your seat belt.

Regards,
Jim

20. Re: Highlight every third line.

And I feel tempted to defend myself with a fish; I'm famous for my fish-slapping-dance; so there ;-P

kind regards,

Jos (<--- ruthless, just ruthless)

ps. proof: Monty Python, The Fish Slapping Dance - YouTube

Page 1 of 2 12 Last

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•