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!
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).
Re: Removing a string variable from the printline?
Quote:
Originally Posted by
awinston
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.
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.
Re: Removing a string variable from the printline?
Quote:
Originally Posted by
awinston
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:
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.