The Artima Developer Community
Sponsored Link

Java Answers Forum
How do you get input from the keyboard?

5 replies on 1 page. Most recent reply: Aug 14, 2003 8:13 PM by Matt Pociask

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
J Stevens

Posts: 7
Nickname: js
Registered: Jul, 2003

How do you get input from the keyboard? Posted: Jul 31, 2003 5:55 PM
Reply to this message Reply
Advertisement
This is how you output,
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); //Display the string.
    }
}

What about getting data from the keyboard and having it print?
example
public void getInformation(){
System.out.println("What is your name? ");
variable =  _______________.readline();  <--- what goes here?
System.out.println("Hello, " + variable );
}

I am using the following:

java.io.*;
java.lang.Object;
java.lang.*;

Should I use some other class or package? Which API covers this?


Thanks in advance


David

Posts: 18
Nickname: malven
Registered: Jul, 2003

Re: How do you get input from the keyboard? Posted: Jul 31, 2003 6:14 PM
Reply to this message Reply
> variable = _______________.readline(); <--- what goes here

Use System.in to read from standard input, which by convention is input from the keyboard. System.in is an instance of java.io.InputStream, which you can use to read bytes. To read and correctly interpret characters you use java.io.Readers (and java.io.Writers).

One twist is that is you want to read characters a line at a time (i.e., real all input until the user hits the enter key) you need to create a BufferedReader "wrapper" like this:

BufferedReader reader =
    new BufferedReader(new InputStreamReader(System.in));
String s = is.readLine();

J Stevens

Posts: 7
Nickname: js
Registered: Jul, 2003

non-static variable cannot be referenced from a static context Posted: Jul 31, 2003 6:56 PM
Reply to this message Reply
David:

Thanks again. But I am getting and error that I am not familiar with.

For every variable, I am getting this message:

non-static variable ___________ cannot be referenced from a static context.....
Whether it is: str, printData, or readData

here is the code:
import java.io.*;
 
	public static void main (String [] args) throws IOException
	{
 
 
 
		// Read your name
		printData = new PrintWriter(new OutputStreamWriter(System.out),true);
		printData.println("What is your name?");
		// Process input.
		readData = new BufferedReader(new InputStreamReader(System.in));
		printData.print("Hello " + str);
		try {
	    while ((str = readData.readLine()) != null) {
		printData.println("    Your name is " + str);
 
		if (str.equals("")) {
		    writer.println("  You forgot your name!\n");
		    break;
		}
		printData.print("\nYou are really that bad at remembering names?  ");
		printData.flush();
	    }
	} catch (IOException e) {
	    e.printStackTrace();
	}
 
	}
}

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: non-static variable cannot be referenced from a static context Posted: Aug 1, 2003 6:22 AM
Reply to this message Reply
I'm assuming that you haven't pasted *all* of your code. I'm guessing that you've defined 'printData' and 'readData' as instance variables:

public class Test {
private PrintWriter printData;
private BufferedReader readData;
}

However, these are INSTANCE variables, but the code where you're trying to use them is a STATIC block (public static void main...). You can't do that. It should be:

PrintWriter printData = new PrintWriter(new OutputStreamWriter(System.out),true);
printData.println("What is your name?");
// Process input.
BufferedReader readData = new BufferedReader(new InputStreamReader(System.in));
printData.print("Hello " + str);

J Stevens

Posts: 7
Nickname: js
Registered: Jul, 2003

Re: non-static variable cannot be referenced from a static context Posted: Aug 1, 2003 10:08 AM
Reply to this message Reply
David:

Hello. Yes I did not paste all of the code in the forum. I did instantiate the PrintWriter and BufferedReader the same as you typed them.

You made a point here that I am not too familiar with. "However, these are INSTANCE variables, but the code where you're trying to use them is a STATIC block (public
static void main...). You can't do that.
It should be: ..... "


When I compile the code, I receive errors stating that the string (str) is a non-static variable (str) cannot be referenced from a static context.

To solve this error, should I create a new method to get the input then and not code it in the main method?
import java.io.*;
 
 
public class WhoseName
{
String str;
private PrintWriter printData;
private BufferedReader readData;
 
 
	public static void main (String [] args) throws IOException
	{
 
 
 
		// Read your name
 
		PrintWriter printData = new PrintWriter(new OutputStreamWriter(System.out),true);
		printData.println("What is your name?");
		// Process input.
		BufferedReader readData = new BufferedReader(new InputStreamReader(System.in));
		printData.print("Hello " + str);
 
 
		try {
	    while ((str = readData.readLine()) != null) {
		printData.println("    Your name is " + str);
 
		if (str.equals(""))
		{
			printData.println("  You forgot your name!\n");
			break;
			}printData.print("\nYou are really that bad at remembering names?  ");
			printData.flush();
			}
	    }
	    catch (IOException e) {
			e.printStackTrace();
			}
		}
 
 
}

Matt Pociask

Posts: 9
Nickname: mpociask
Registered: Aug, 2003

Re: How do you get input from the keyboard? Posted: Aug 14, 2003 8:13 PM
Reply to this message Reply
Are you familiar with object oriented programming and how it is implimented in java? The modifier, static, before your main method says that whenever you create an instance of your class and call that method, the method and its variables a common to each instance of that class. You cannot have variables that aren't common to each class (non-static) refereced from a method that is common to each class (static), becase the static method won't know which version of that variable to use.

Try adding the following to your code:

public class foobar
{

// Your existing code

}

Change foobar to whatever you named your file. (without the .java part)

Also, don't include java.lang.*, java.lang.Object. They are imported automatically. Also, I don't think you need the IO package, but you might need it for the BufferedReader class.

I hope this helps. If the first paragraph is way over your head, you need to brush up on object oriented design.

Good luck!

Flat View: This topic has 5 replies on 1 page
Topic: java linklist debug error Previous Topic   Next Topic Topic: Can Java Frame contain a window of web browser?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use