LiveWire Peer Support Network

Printable Version of Topic "Help with Java"

- LiveWire Teen Forums & College Forums (http://www.golivewire.com)
-- (http://www.golivewire.com/forums/support-technical.html)
--- Programming & Application Development (http://www.golivewire.com/forums/forum-211-s-0.html)
---- Help with Java (http://www.golivewire.com/forums/peer-yyybttp-support-a.html)


-- Posted by Majo at 2:05 pm on Sep. 12, 2008

I need to get I don't know how many different things to print out on different lines. How should I go about doing this? What would be the best way? I don't know if I should do it line by line like:

Code:
System.out.println("Hello");
System.out.println("Goodbye");

...or not because that's not really efficient, right? And we have to use the proper print function, as well, and I'm not sure if that's it. It'd also be nice if I could do it without stretching my page out like a mile long just to code it properly...


-- Posted by marshmellowman at 2:08 pm on Sep. 12, 2008

Does the \n work like in C++? So you'd have:
Code:
System.out.println("Hello\nGoodbye");


-- Posted by Majo at 2:15 pm on Sep. 12, 2008

It does but I'm not sure how to use it. I got two lines to print out separately but then it all fell apart. I need to do something like this:

Code:
System.out.println("Car make and model: Volkswagen Golf"\n "Expected Annual Mileage: " + mileage);

Assuming that's right, that's as far as I got before I couldn't figure out what to do next. Next, I need to add in something like:

Code:
("Cost of a gallon of gas: " + gallon)

But I don't know how to get that in there.


-- Posted by marshmellowman at 2:20 pm on Sep. 12, 2008

The \n actually has to be within the text, no outside the quotes. So yours would be like:
Code:
System.out.println("Car make and model: Volkswagen Golf \n Expected Annual Mileage: " + mileage "\n Cost of a gallon of gas:" + gallon);
That's how I remember it anyway for C++, it might be different for Java, I'm not too sure.


-- Posted by Majo at 2:23 pm on Sep. 12, 2008

Alright, I'll give that a go and see what happens when I try to compile it, thanks.


-- Posted by ampamp at 2:23 pm on Sep. 12, 2008

The \n has to be inside the " marks. I don't see any reason to use them, when printing things out. I would just put them into indiviual println, to make it easier to read later.


-- Posted by Majo at 2:25 pm on Sep. 12, 2008

I don't know if that's "proper" or not, though.

Gah, I think I'll quit this one and email the professor to see what he wants, my back is killing me and I'm hungry anyways.


www.golivewire.com