Aaron Hall
Aaron Hall

Reputation: 27

Why isn't my program adding a blank line after printing the .toString method?

I am writing code to output parts of an ArrayList named Bikes using classes and if statements.

I added the new line characters after the print statements, but the output is still not seperated.


`

for(Bicycle c : Bikes){
            if(c instanceof MountainBike){
                ((MountainBike) c).setHeight(99);
                System.out.println("New Mountain Bike Info: ");
                System.out.println(c.toString() + "\n\n");
            }
            
            if(c instanceof TandemBike){
                ((TandemBike) c).setBetweenSpace(88);
                System.out.println("New Tandem Bike Info: ");
                System.out.println(c.toString() + "\n\n");    
            }
           
            if(c instanceof RoadBike){
                ((RoadBike) c).setHandlebarType("NewType");
                System.out.println("New Road Bike Info: ");
                System.out.println(c.toString() + "\n\n");   
            }
            
        }

`

This is what the output looks like despite the \n:

New Mountain Bike Info:
Cadence: 43
Gear: 2
Speed: 3
Seat Height: 99
New Tandem Bike Info:
Cadence: 37
Gear: 1
Speed: 2
Between Space: 88
New Road Bike Info:
Cadence: 40
Gear: 1
Speed: 1
Handelbars: NewType

Upvotes: 1

Views: 50

Answers (1)

sanurah
sanurah

Reputation: 1132

Can you try below please?

String newLine = System.lineSeparator();
System.out.println(c.toString() + newLine + newLine); 

The line break could depend on your system. https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#lineSeparator%28%29

I suggest you debug and see what the lineSeparator is on your system.

Upvotes: 1

Related Questions