How to Write Console Programs In Java
Console programs in Java can be easily written using System.out.println
for output and Scanner
for input.
For example, here is a simple guessing game which runs in the console:
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Random rand = new Random();
int secretNumber = rand.nextInt(100) + 1;
int guess = 0;
int numGuesses = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("I'm thinking of a number between 1 and 100. Can you guess what it is?");
while (guess != secretNumber) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
numGuesses++;
if (guess < secretNumber) {
System.out.println("Too low! Guess higher.");
} else if (guess > secretNumber) {
System.out.println("Too high! Guess lower.");
} else {
System.out.println("Congratulations, you guessed the number in " + numGuesses + " guesses!");
}
}
}
}
Another example will be of a downloader which takes as input an url to be downloaded:
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
public class Downloader {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java Downloader [url]");
System.exit(1);
}
String url = args[0];
String fileName = url.substring(url.lastIndexOf('/') + 1);
try (BufferedInputStream in = new BufferedInputStream(new URL(url).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(fileName)) {
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
System.out.println("File downloaded successfully.");
} catch (IOException e) {
System.err.println("Error downloading file: " + e.getMessage());
}
}
}
This downloader takes a single command line argument, which is the URL of the file to download. It creates a BufferedInputStream
to read the data from the URL, and a FileOutputStream
to write the data to a file. The program reads the data in chunks of 1024 bytes, and writes each chunk to the file until there is no more data to read.
If the download is successful, the program outputs a success message. If there is an error, the program outputs an error message.
Note that this is just a basic example and does not handle all possible errors and edge cases.