Results 1 to 4 of 4
  1. #1
    derb2k2 is offline Noob
    Join Date
    Jul 2011
    Location
    M-I-yayo
    Posts
    11
    Rep Power
    0

    Default how to keep on the same line. Help needed.

    hello everyone. I would like to know how I can have a string appear on the same line after the program is run. i.e. after the user enters "hours", i want the text hours to appear next to it on the same line and not the next. sorry if I didnt' post this correctly.


    public static void main (String[] args)
    {
    int hours, mins, secs, totalsecs;

    Scanner inhours = new Scanner (System.in);
    Scanner inmins = new Scanner (System.in);
    Scanner insecs = new Scanner (System.in);

    System.out.print ("Please enter the number of hours: ");

    hours = inhours.nextInt(); System.out.print ("hours.");
    System.out.println ();
    mins = inmins.nextInt(); System.out.print ("minutes.");
    System.out.println ();
    secs = insecs.nextInt(); System.out.print ("seconds.");
    System.out.println ();
    System.out.println ();


    int HOURS = hours * 3600;
    int MINS = mins * 60;
    int SECS = secs * 1;

    totalsecs = HOURS + MINS + SECS;

    System.out.println ("You entered " + hours + " hours," + mins + " minutes,"
    + secs + " seconds.");

    System.out.println ("The given amount of time translates to "
    + totalsecs + " seconds.");
    }
    }

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    As far as I know, you can't. Certainly putting the code on the same line isn't going to accomplish it. Very interesting attempt though.

    You have to hit enter to submit the information to the program, thus causing a newline. That's why most people do a print statement beforehand doing something like Hours: instead.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    derb2k2 is offline Noob
    Join Date
    Jul 2011
    Location
    M-I-yayo
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    As far as I know, you can't. Certainly putting the code on the same line isn't going to accomplish it. Very interesting attempt though.

    You have to hit enter to submit the information to the program, thus causing a newline. That's why most people do a print statement beforehand doing something like Hours: instead.
    hrmm, that sounds good actually. Thank you. Kinda weird that there isn't a command to help with that either way. But then again im newb lol

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default

    Java's support for console I/O is extremely poor. I revisited this issue recently, thinking that the newish java.io.Console allowed reading single characters instead of whole lines. Nope... not on the JRE I'm using, anyway.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Constructors pls explan the code line by line in comments
    By vibaviattigala in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 04:03 AM
  2. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  3. tracing java application line by line using netbeans
    By chandrasekhar123 in forum NetBeans
    Replies: 1
    Last Post: 08-03-2010, 02:46 PM
  4. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 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
  •