Java: how to live without scanf



  • I'm a 4th year CS student. My school doesn't teach Java, but I got some Java experience during an internship this summer, and I decided it would be a good choice for my latest "in a language of your choice" assignment.

    The one thing I didn't get any experience with was reading from standard input. I ended up doing:

    
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    
    for (int i=0; i<numWeeks; i++) {
      StringTokenizer inputLine = new StringTokenizer(input.readLine());
      highStressRevenue[i] = Long.parseLong(inputLine.nextToken());
      lowStressRevenue[i] = Long.parseLong(inputLine.nextToken());
    }
    

    for what I could have done, if I'd stuck with C, as:

    
    for (int i=0; i<numWeeks; i++)
      scanf("%d %d", &highStressRevenue[i], &lowStressRevenue[i]);
    

    My "there has to be a better way" sense is tingling. I mean, even C# has Console.ReadLine. Is there a simpler workaround to the lack of scanf that I can use next time?



  • Did you google Java readLine?

    (I'm pretty sure you googled java scanf)



  • @b-redeker said:

    Did you google Java readLine?

    (I'm pretty sure you googled java scanf)

    I did, and it's what I used. Without BufferedReader.readLine, I would have had to use two tokenizers, at which point I would probably have decided it was simpler to read the file byte-by-byte with a switch statement.


  • Discourse touched me in a no-no place

    Java? Console input? HA! They just made that sane in JDK5 or 6. Look up the 'scanner' class (yes, it's named all stupid) - which, for the record, was added grudgingly and only under the pressure of the Java Schools movement so they didn't have to drown CS101 students in goddamned readers and tokenizers (or each instructor would hack up their own stdin library)



  • @Weng said:

    Look up the 'scanner' class (yes, it's named all stupid)

    Hah, learned something again. That looks useful even if the names are indeed a bit clunky.



  • Even better, change your language of choice.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.