Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree6Likes

Thread: Highlight every third line.

  1. #1
    Reptyle216 is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Highlight every third line.

    Are you looking for code that counts to three repeatedly?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Highlight every third line.

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

  4. #4
    Reptyle216 is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default 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. #5
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default 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 06:42 PM.

  6. #6
    Reptyle216 is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Highlight every third line.

    Quote Originally Posted by JBelg View Post
    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. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Highlight every third line.

    Quote Originally Posted by Reptyle216 View Post
    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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default 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. #9
    Reptyle216 is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Highlight every third line.

    Quote Originally Posted by SurfMan View Post
    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. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: Highlight every third line.

    Quote Originally Posted by Reptyle216 View Post
    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.
    gimbal2 likes this.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

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

    Quote Originally Posted by SurfMan View Post
    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 ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Reptyle216 is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Highlight every third line.

    I'll address both possible reasons.

    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. #13
    Reptyle216 is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Highlight every third line.

    Quote Originally Posted by gimbal2 View Post
    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. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Highlight every third line.

    Okay.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default 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 ;-)
    Norm and gimbal2 like this.
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Highlight every third line.

    Careful JosAH. One of the dogs may turn on the spectators.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Highlight every third line.

    You know what, I'll give in! Woof, woof woof woof!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Highlight every third line.

    Quote Originally Posted by Reptyle216 View Post
    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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. GNU Source-highlight 3.1.6
    By java software in forum Java Software
    Replies: 0
    Last Post: 01-01-2012, 12:58 PM
  2. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  3. highlight content
    By Saran185 in forum NetBeans
    Replies: 1
    Last Post: 01-31-2011, 10:25 AM
  4. Date highlight
    By Shyamz1 in forum New To Java
    Replies: 8
    Last Post: 11-26-2010, 02:19 PM
  5. GNU Source-highlight 2.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-12-2007, 08:39 AM

Posting Permissions

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