Page 1 of 1

Basic Console I/O Nice for starting with beginners, since most teachers I know don't Rate Topic: -----

#1 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Post icon  Posted 14 September 2008 - 11:06 AM

This is an introduction to Console I/O. In other words, basic interactive programs.

I am going to show you the methods print() and println() first, as these will either inform the user of the output or prompt them for what they need to enter.

Let's get to the code!


The print() and println() Methods

public class test
{
    public static void main(String[] args)
    {
        // the print() method simply outputs text to the screen

        System.out.print("This is text.");
    }
}


This will output This is text. to the screen. Now, a common mistake I find in beginners with this is that they wanna know how to put the next text on the next line. This next one is how you do this. It's as simple as changing the method.

public class test
{
    public static void main(String[] args)
    {
        // the println() method will output whatever's in the
        // parentheses, then (basically) hits <Enter>, or 
        // terminates the current line.

        System.out.println("This is text.");
    }
}


This will output the same thing as the last one, BUT it will terminate the line after it does. Anything else that is outputted to the screen right after this will be on the next line.
_______________________________________________________________

Inputting Data

This is fairly simple. I'll show you 2 different ways, the old way and the new (better) way.

BufferedReader

The first way will be the old way, using BufferedReader and InputStreamReader. This is the old way and it's not as good as the new way because it lets you enter anything, even if it's not what you prompted for, which causes errors/Exceptions during runtime.

// MAKE SURE TO IMPORT java.io.*!!!!
import java.io.*;

public class test
{
    public static void main(String[] args)
    {
        // this line is not necessary, but most people use it
        // System.in is the keyboard input stream
        InputStreamReader rdr = new InputStreamReader(System.in);

        // use the InputStreamReader as the parameter to read from keyboard
        BufferedReader reader = new BufferedReader(rdr);

        // prompt for what to enter
        System.out.print("Enter a number: ");

        // get the input (ALWAYS A STRING)
        String in = reader.readLine();

        // transfer that String to an integer
        int number = Integer.parseInt(in);

        // output the number
        System.out.println(number);
    }
}


As you can see, the InputStreamReader object going INSIDE the BufferedReader constructor can be a little confusing to a beginner, and it was a big thing I always missed.

When you use BufferedReader, the input is ALWAYS a String...ALWAYS. You have to parse the String into a number or whatever you want it to be to use it in anything mathematical.


The New Way (Scanner)

The new way, Scanner, prevents the BufferedReader's old mistake of letting the user input anything. It's also MUCH easier to boot!

// does NOT use java.io
import java.util.Scanner;

public class test
{
    public static void main(String[] args)
    {
        // much easier declaration
        Scanner scan = new Scanner(System.in);

        // prompt for input
        System.out.print("Enter a number: ");

        // get the input, NO PARSING.
        // The nextInt() method prevents the user
        // from crashing the program here...
        // As it only accepts number(s) as input
        int number = scan.nextInt();

        // output the number
        System.out.println(number);
    }
}


This code sample does the EXACT same thing as the first one, except you can only enter numbers! :D

The fact that it protects against faulty input makes it a much better alternative to the primitive BufferedReader, HOWEVER...

You must have JAVA 5 (JDK 1.5) or later to use Scanner!

Is This A Good Question/Topic? 3
  • +

Replies To: Basic Console I/O

#2 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Posted 28 September 2008 - 11:55 AM

You can enter Strings with the scanner?

Scanner sin = new Scanner(System.in);
String sName;
sName = sin.next();



Anyways:

Quote

InputStreamReader rdr = new InputStreamReader(System.in);

// use the InputStreamReader as the parameter to read from keyboard
BufferedReader reader = new BufferedReader(rdr);


Is it better to do that in two lines instead of what line?

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Posted 03 October 2008 - 02:11 PM

1) Yes, Scanner does read a string.

2) Either would work. The benefit of Locke's method is that you can reuse that stream, but since he never did, it seems quite pointless. Personally, I tend to stick with stuff like that wherever possible.

Heck, you could even do this:
String x = new Scanner(System.in).next();

Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 09 October 2008 - 07:48 PM

Yeah, I think it looks better my way though. :P

Not 1 really long line of code. 2 medium sized ones. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1