Results 1 to 5 of 5
  1. #1
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Removing a string variable from the printline?

    Problem:

    System.out.println(v1 + v2 + v3 + v4);

    The idea is that I want all four variables to only print by default, unless some condition is true and I want only three variables to pop up:

    System.out.println(v1 + v2 + v4);

    Is there some way of making the println ignore v3 without having to write a separate System.out.println();? I tried setting the variable to null, but that just printed out "null". I also experimented with things like (v1 + v2 - v3 + v4) but that was a long shot.

    Thanks!
    Last edited by DrMadolite; 08-05-2012 at 03:44 AM.

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Removing a string variable from the printline?

    Is this inside a loop? I'm asking because if it is not inside a loop, you would have to write a separate print statement every time you want to print something (regardless of if you want to ignore v3 or not).
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: Removing a string variable from the printline?

    Quote Originally Posted by awinston View Post
    Is this inside a loop? I'm asking because if it is not inside a loop, you would have to write a separate print statement every time you want to print something (regardless of if you want to ignore v3 or not).
    Nah, it's part of a nested if statement. The reason I'm asking is because I'm currently containing the entire printline inside a parent string variable (because I want to change it around):

    String show = (month + "this year, " + tense + age + yearFix + bMonth + monthFix + "months old.");
    System.out.println(show);


    I'm trying to figure out if I can change the "show" parent variable to exclude the monthFix variable in some simple way.
    Last edited by DrMadolite; 08-05-2012 at 04:07 AM.

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Removing a string variable from the printline?

    Why not just define another variable and initialize it without monthFix? If the condition is true, then print show. If the condition is false, then print show1.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: Removing a string variable from the printline?

    Quote Originally Posted by awinston View Post
    Why not just define another variable and initialize it without monthFix? If the condition is true, then print show. If the condition is false, then print show1.
    I'm doing something similar, I'm reusing the "show" variable by changing its value within each if statement:

    Java Code:
    if (firstCondition)
         String show = (month + "this year, " + tense + age + yearFix + bMonth + monthFix + "months old.");
    else if (secondCondition)
         String show = (month + "this year, " + tense + age + yearFix + bMonth + "months old.");
    else {}
    But this just seems a bit cumbersome because I don't just have 2 if conditions:

    Condition 1: Adds parts 1 + 2 + 3 + 4 + 5
    Condition 2: Adds parts 1 + 2 + 3 + 5
    Condition 3: Adds parts 2 + 3 + 4 + 5
    Condition 4: Adds parts 2 + 3 + 5

    Think of it as multi-dimensional conditioning.
    Last edited by DrMadolite; 08-05-2012 at 04:41 AM.

Similar Threads

  1. Removing spaces in String ArrayList
    By dougie1809 in forum New To Java
    Replies: 6
    Last Post: 03-13-2012, 10:25 PM
  2. Removing thr last two bytes from string
    By KarlNorway in forum New To Java
    Replies: 3
    Last Post: 03-13-2012, 10:29 AM
  3. Removing All Commas from a String
    By andy9507 in forum New To Java
    Replies: 6
    Last Post: 05-12-2011, 05:16 AM
  4. Removing words from a String
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-15-2011, 11:50 AM
  5. Removing last character from a String
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 01:43 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
  •