Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,362 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,494 people online right now. Registration is fast and FREE... Join Now!




Basic Console I/O

 
Reply to this topicStart new topic

> Basic Console I/O, Nice for starting with beginners, since most teachers I know don't

Locke
Group Icon



post 14 Sep, 2008 - 10:06 AM
Post #1


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

java
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.

java
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.

java
// 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!

java
// 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! biggrin.gif

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!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

chili5
*****



post 28 Sep, 2008 - 10:55 AM
Post #2
You can enter Strings with the scanner?

java

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?

java

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Go to the top of the page
+Quote Post

gabehabe
Group Icon



post 3 Oct, 2008 - 01:11 PM
Post #3
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:
java
String x = new Scanner(System.in).next();
Go to the top of the page
+Quote Post

Locke
Group Icon



post 9 Oct, 2008 - 06:48 PM
Post #4
Yeah, I think it looks better my way though. tongue.gif

Not 1 really long line of code. 2 medium sized ones. biggrin.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 08:12PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month