Download code

Jump to: navigation, search

Back to Basic_constructs_(Java)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

input.txt

1 Now is the Winter of our Discontent
2 Made glorious Summer by this son of Yorke;


BasicConstructs.java

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(Java)?oldid=13238
 14 */
 15 
 16 import java.io.*;
 17 
 18 public class BasicConstructs {
 19 
 20     public void loops()
 21     {
 22         System.out.println("For loop");
 23 	for(int i = 0; i < 10; i++) {
 24 	    System.out.println(i);
 25 	}
 26 	System.out.println();
 27 
 28 
 29         System.out.println("While loop");
 30 	int j = 0;
 31 	while(j < 10) {
 32 	    System.out.println(j);
 33 	    j++;
 34 	}
 35 	System.out.println();
 36 
 37         System.out.println("Do-while loop");
 38 	int k = 0;
 39 	do {
 40 	    System.out.println(k);
 41 	    k++;
 42 	} while (k < 10);
 43 	System.out.println();
 44     }
 45 
 46     void io() throws IOException
 47     {
 48       String line = null;
 49 
 50       System.out.println("Standard I/O");
 51       BufferedReader in = new BufferedReader(
 52           new InputStreamReader(System.in));
 53       System.out.print("Type a line of text (enter with Return): ");
 54       line = in.readLine();
 55       System.out.println("You entered: " + line);
 56       System.out.println();
 57 
 58       try {
 59           BufferedReader fin = new BufferedReader(
 60 	      new FileReader("input.txt"));
 61           line = fin.readLine();
 62 	  if (line == null) 
 63 	      throw new EOFException("Unexpected End of File encountered while reading \"input.txt\"");
 64           fin.close();
 65       }
 66       catch (IOException e) {
 67           System.err.println("ERROR: Cannot read \"input.txt\"");
 68           throw e;
 69       }
 70 
 71       try {
 72           PrintWriter fout = new PrintWriter(
 73 	      new BufferedWriter(
 74 	          new FileWriter("output.txt")));
 75           fout.println(line);
 76           fout.close();
 77       }
 78       catch (IOException e) {
 79           System.err.println("ERROR: Cannot write to \"output.txt\"");
 80           throw e;
 81       }
 82     }
 83 
 84     private void usage()
 85     {
 86         System.err.println("Usage java BasicConstructs loops|io");
 87         System.exit(1);
 88     }
 89 
 90     public static void main(String[] args) throws Exception
 91     {
 92         BasicConstructs bc = new BasicConstructs();
 93 
 94         if (args.length < 1) {
 95 	    System.err.println("ERROR: Wrong number of arguments");
 96 	    bc.usage();
 97 	}
 98 
 99         for (int i = 0; i < args.length; i++) {
100 	    if ( "loops".equals(args[i]) ) bc.loops();
101 	    else if ( "io".equals(args[i]) ) bc.io();
102 	    else {
103 	       System.err.println("ERROR: Unexpected command line argument: " + args[i]);
104 	       bc.usage();
105 	    }
106 	}
107      
108         System.exit(0);
109     }
110 }


hijacker
hijacker
hijacker
hijacker