# Java programming... easy question

**URL:** <https://boards.straightdope.com/t/java-programming-easy-question/202512>\
**Category:** Factual Questions\
**Created:** [September 19, 2003, 12:43am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512 "2003-09-19T00:43:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![culov](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@culov](https://boards.straightdope.com/u/culov)\
**Post date:** [September 19, 2003, 12:43am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/1 "2003-09-19T00:43:28Z")

</div>

Hi, what i need to do is get input from the user, but i have no idea how. Lets just say i want to make a program like this:

"Please enter a number: " //user enters 3 for example  
“You entered 3”

How would i go about doing that with java? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![DarrenS](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@DarrenS](https://boards.straightdope.com/u/DarrenS)\
**Post date:** [September 19, 2003, 12:52am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/2 "2003-09-19T00:52:34Z")

</div>

Something like:

```auto

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

String line = r.readLine();

if (line != null)
{
  // process it here
}

```

---

<div class="post-metadata">

**Author:** ![culov](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@culov](https://boards.straightdope.com/u/culov)\
**Post date:** [September 19, 2003, 1:07am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/3 "2003-09-19T01:07:43Z")

</div>

Well other than declaring variables, doing math, and displaying it, i know NO java. I literally came into class my first day and was told to make a program that i need input for without being taught one thing. I mean, i usually would learn well if im taught simply the basics, but it seems like the most important step was skipped in this damn class…

---

<div class="post-metadata">

**Author:** ![culov](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@culov](https://boards.straightdope.com/u/culov)\
**Post date:** [September 19, 2003, 1:09am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/4 "2003-09-19T01:09:15Z")

</div>

Oops forgot to put the main point into my last post… anyways, the point is that i dont understand what you mean by “process here”, and im also getting a compile error for “if (line != null)”

Thanks,  
culov

---

<div class="post-metadata">

**Author:** ![Duckster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/duckster/32/1244_2.png) [@Duckster](https://boards.straightdope.com/u/Duckster)\
**Post date:** [September 19, 2003, 1:11am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/5 "2003-09-19T01:11:57Z")

</div>

Sounds like you need to grab a copy of “Learning Java for Dummies,” or something similar. Plus, I’m sure you can find free online resources to get you started as well.

---

<div class="post-metadata">

**Author:** ![mrbuddylee](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mrbuddylee](https://boards.straightdope.com/u/mrbuddylee)\
**Post date:** [September 19, 2003, 2:36am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/6 "2003-09-19T02:36:39Z")

</div>

They’re probably talking about taking an argument when the program runs. I don’t have the exact code (I’m aweful at writing code completely from scratch), but this might help.  
part of java program  
Public void main(String args)  
{  
for(int x=0;x\<args.length();x++)  
{  
System.out.println("Command line argument entered: " + args);  
}  
}  
To run:  
java program\_name argument1 argument2 etc

hth.

---

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [September 19, 2003, 4:51am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/7 "2003-09-19T04:51:22Z")

</div>

Here’s the Java “Bible” FWIW:

[http://java.sun.com/docs/books/tutorial/](http://java.sun.com/docs/books/tutorial/)

---

<div class="post-metadata">

**Author:** ![elfbabe](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@elfbabe](https://boards.straightdope.com/u/elfbabe)\
**Post date:** [September 19, 2003, 5:41am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/8 "2003-09-19T05:41:43Z")

</div>

All right, because I’m nice, I will write something for you that does exactly what you asked, no more. Then I will explain it.

This, by the way, runs exactly as written.

// All code copyright elfbabe, 2003.

// You need the Java io libraries to do stuff with input  
import [java.io](http://java.io).\*;

// class culov = in a file called culov.java  
public class culov {

```
// Main method. Program starts running here.
// Throws IOException means that if something messed up
// happens with the input/output, you're not doing anything.

```

// must say this, or compiler will complain  
public static void main(String args) throws IOException{

```
// You're creating a BufferedReader called "in" that
// will read input from the console, System.in.
BufferedReader in = new BufferedReader
    (new InputStreamReader(System.in));

// Display the stuff in quotes on the console
System.out.println("Please enter a number:");

// The user gets a blank line

```

// whatever they type is interpreted  
// as an integer  
int theInput =  
Integer.parseInt(in.readLine());

// Display what’s in quotes  
// Add theInput to the end  
System.out.println("You entered " + theInput);  
}  
}

This will store whatever the user entered, number or not, as an Int. It’s dumb, though, so if the user enters something OTHER than an int, you’ll get a bunch of exceptions, and probably a crappy grade. At least I’d hope so. There are lots of ways of dealing with it, you can look them up. But this should get you started. If you want to store input as a String, it’s just String theInput = in.readLine();

Good luck.

---

<div class="post-metadata">

**Author:** ![Monstre](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Monstre](https://boards.straightdope.com/u/Monstre)\
**Post date:** [September 19, 2003, 5:45am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/9 "2003-09-19T05:45:07Z")

</div>

> [@](#):
>
> from _culov_  
> Well other than declaring variables, doing math, and displaying it, i know NO java. I literally came into class my first day and was told to make a program that i need input for without being taught one thing.

Is this a **Java** class?? Or a class about something else, for which programming is needed?

It seems ridiculous to start a Java class by expecting students to write immediate programs that do input from standard input (on the console), since that is more complicated than standard input in other languages (like learning quick cin statements in C++ or scanf calls in C). Console output is easy from lesson 1 in Java, but console input not as easy for the first day. You have to know about some of the classes to use (Reader class and various subclasses, like BufferedReader) along with the methods in them.

I taught an intro Java course this summer. Here is a link to the first of the online notes outlines I created for my students. There are a few code examples there, including an example of a simple input class that provides functions for reading integers, floats, or strings.

[Java Basics](http://www.cs.fsu.edu/~myers/cis3931/notes/basics.html)

---

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [September 19, 2003, 11:54am UTC](https://boards.straightdope.com/t/java-programming-easy-question/202512/10 "2003-09-19T11:54:18Z")

</div>

How High’s the Water Mama? - Johnny Cash

Ball of Confusion - Temptations

Rhythm of the Rain - Cascades

Twisting the Night Away - Sam Cooke
