Challenge 3

Go down

Challenge 3

Post by Admin on Mon Aug 28, 2017 2:47 pm

[b]Challenge Question: [/b]
Read a pair of small integers from a single input line in the file 'test.in' and print their sum to the file 'test.out'.

avatar
Admin
Admin

Posts : 25
Join date : 2017-08-17
Location : Torrance CA

View user profile http://southbayrobot.com

Back to top Go down

Re: Challenge 3

Post by Admin on Mon Aug 28, 2017 2:50 pm

import java.io.*;
import java.util.*;

class test {
 public static void main (String [] args) throws IOException {
   // Use BufferedReader rather than RandomAccessFile; it's much faster
   BufferedReader f = new BufferedReader(new FileReader("test.in")); // input file name goes above
   PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out")));
   // Use StringTokenizer vs. readLine/split -- lots faster
   StringTokenizer st = new StringTokenizer(f.readLine()); // Get line, break into tokens
   int i1 = Integer.parseInt(st.nextToken());    // first integer
   int i2 = Integer.parseInt(st.nextToken());    // second integer
   out.println(i1+i2);                           // output result
   out.close();                                  // close the output file
 }
}
avatar
Admin
Admin

Posts : 25
Join date : 2017-08-17
Location : Torrance CA

View user profile http://southbayrobot.com

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum