Java - using String Method

Discussion in 'Programmer's Corner' started by J.live, Jan 24, 2011.

  1. J.live

    Thread Starter Member

    Sep 10, 2010
    35
    0
    Write a program that reads four-digit integer as a string , such as 1998 and then displays it, one digit per line, like so:
    1
    9
    9
    8


    Attempt:
    Scanner keyboard2 = new Scanner (System.in);
    String integer2= "Input a 4 digit number:";
    System.out.println(integer2);

    How do I make it read ? How do I go about this using string method ? I am lost. Thanks
     
  2. J.live

    Thread Starter Member

    Sep 10, 2010
    35
    0
    bump, anyone :/
     
  3. rapidcoder

    Member

    Jan 16, 2011
    37
    3
    Use:
    Code ( (Unknown Language)):
    1.  
    2. String lineOfText =
    3.   new BufferedReader(new InputStreamReader(System.in)).readLine();
    4.  
    5.  
    Anyway, the problem is trivial if you coded it in Scala (a much better Java):
    Code ( (Unknown Language)):
    1.  
    2. println(readLine mkString "\n")
    3.  
     
Loading...