1

This is my code:

int numb = 50;
int odd;
int even;
while (numb <= 100) {
    if (numb % 2 == 0) {
        even = numb;
        System.out.println(even);
        System.out.print(", ");
        numb++;
    }
    System.out.println("");
    if (numb % 2 != 0) {
        odd = numb;
        System.out.print(odd);
        System.out.print(", ");
        numb++;
    }
}

So far, it prints two columns of answers but i need to separate rows of evens and odds. What am I doing wrong?

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
  • 1
    Why not just add the evens and odds in two separate arrays or lists, and then print these two arrays/lists at the end, after the while-loop ends? – Chthonic Project Sep 25 '17 at 15:36
  • I cannot use arrays, i'd love to but cannot. That is a condition of this program in a whole. – The Captain Sep 25 '17 at 15:38
  • @ChthonicProject printing this array/list in a good format would probably require another loop. Simplest way would be to just use two Stringbuilders where you append the evens/odds and print those after the while loop is done. – OH GOD SPIDERS Sep 25 '17 at 15:38
  • you don't need the second if, you can just use else. also, numb++ doesn't need to be repeated - you're going to do that whatever the result of the if statement. and, you don't really need variables odd and even. – pecks Sep 25 '17 at 15:39
  • @pecks I tried removing the second if and used an else, it made me remove the print statement to separate the lines. How else can have that line separation? – The Captain Sep 25 '17 at 15:42
  • @OHGODSPIDERS The toString() method in Java 8 already uses StringBuilder to do this. For older versions, your argument is correct. – Chthonic Project Sep 25 '17 at 15:45

2 Answers2

4

You can use two StringBuilder and keep appending sOdd and sEven to respective StringBuilder and after the loop you can print them.

Something similar

StringBuilder sOdd = new StringBuilder("");
StringBuilder sEven = new StringBuilder("");

while (numb <= 100) {
    if (numb % 2 == 0) {
        sEven.append(numb).append(",");
    }
    if (numb % 2 != 0) {
        sOdd.append(numb).append(",");
    }
   numb++;
}

System.out.println(sEven.toString()+"\n"+sOdd.toString());
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Why using the "String", if we can use other mutable string classes like "StringBuilder" or "StringBuffer". – Punit Sep 25 '17 at 15:43
  • @Punit true. As OP was not specific. I can change it, if you wish ;) – Ravi Sep 25 '17 at 15:43
  • Perfect, that is running perfectly, I did not know that strings can work that way. I am a low intermediate java user so it is good to know they can work as such. – The Captain Sep 25 '17 at 15:45
0

I f you're particular about single while loop and constant extra space you can do this:

int numb1 = 50,numb2=51;
while (numb1 <= 100 || numb2<=100) 
{
if (numb1<=100) 
{
    System.out.print(numb1);
    System.out.print(", ");
    numb1=numb1+2;
}


else if (numb2<=100) 
{
    if(numb2==51)
    System.out.println();

    System.out.print(numb2);
    System.out.print(", ");
    numb2=numb2+2;
}
}