Need Java Help - Prompting User for Input

Locked
Cooldown
Posts: 660
Joined: Mon Jun 05, 2000 7:00 am

Need Java Help - Prompting User for Input

Post by Cooldown »

I've taken a Comp Sci course this semester which covers Java. While we haven't started to learn the actual code and syntax, I decided to jump ahead. I have a little experience with C++ and Java from Comp Sci from HS, but it's been a while so everything is a little bit hazy so bear with me.

What I want to do is simple--prompt the user to enter an integer (or String then convert it to an integer). Then store the input in a variable. This is what I have....if there is better way to do this then by all means...I'm open ears

Code: Select all

import java.io.*;
import java.util.*;

public class problem1
{
	public static void main (String[] args)
	{
		InputStreamReader stdin = new InputStreamReader(System.in);
		BufferedReader console = new BufferedReader(stdin);
 
                System.out.println("Enter a number");
    		String input = stdin.readLine();
    		int number = Integer.parseInt(input);
    		System.out.println(number);
  
	}

}
I get the following error:

Code: Select all

C:\Java>javac problem1.java
problem1.java:12: cannot find symbol
symbol  : method readLine()
location: class java.io.InputStreamReader
                String input = stdin.readLine();
                                    ^
1 error
I've searched on Google and attempted to look up for answer on why this is happening to no avail. Everything seems right, why am I getting an error? :(
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Need Java Help - Prompting User for Input

Post by ^misantropia^ »

Make that line console.readLine() and it should compile fine. console is an instance of BufferedReader, stdin of InputStreamReader.
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Need Java Help - Prompting User for Input

Post by ^misantropia^ »

Also, this bit:

Code: Select all

InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
is superfluous.

Code: Select all

BufferedReader console = new BufferedReader(System.in);
is sufficient.
Cooldown
Posts: 660
Joined: Mon Jun 05, 2000 7:00 am

Re: Need Java Help - Prompting User for Input

Post by Cooldown »

Thanks! Seems that I also need throws IOException after (String args[]).

And apparently I need both

Code: Select all

InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
Otherwise, I get an error. :dork:

Code: Select all

import java.io.*;
import java.util.*;

public class prob2
{
   public static void main (String[] args) throws IOException
   {
      InputStreamReader stdin = new InputStreamReader(System.in);
      BufferedReader console = new BufferedReader(stdin);

          System.out.print("Enter a number: ");
          String input = console.readLine();
          int number = Integer.parseInt(input);
          System.out.print(number);

   }

}
This compiles fine. :smirk:
Cooldown
Posts: 660
Joined: Mon Jun 05, 2000 7:00 am

Re: Need Java Help - Prompting User for Input

Post by Cooldown »

I'm having difficulty with an if statement.

Code: Select all

import java.io.*;
import java.util.*;

public class prob2
{
   public static void main (String[] args) throws IOException
   {
      InputStreamReader stdin = new InputStreamReader(System.in);
      BufferedReader console = new BufferedReader(stdin);

          System.out.print("Enter a number:");
          String input = console.readLine();
          int number = Integer.parseInt(input);
          System.out.println(number);

          System.out.println("Do you like cheese? (Y/N):");
		  String coupon = console.readLine();

		  if (coupon == "N" || coupon == "n"){
		   	System.out.print("You don't like cheese.");
		}
		else{
			System.out.print("You like cheese.");
		}

   }
The output is not as desired. If I enter "N" or 'n' as my input to the "Do you like cheese? (Y/N):" question it ends up outputting "I like cheese." :(
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Need Java Help - Prompting User for Input

Post by ^misantropia^ »

Thanks! Seems that I also need throws IOException after (String args[]).
You don't. And don't want to. Google for checked exceptions. Rule of thumb: catch any non-fatal exceptions, only let the real show stoppers pass through:

Code: Select all

try {
    console.readLine();
} catch (IOException e) {
    e.printStackTrace();
}
A tip: compiling with -Xlint:all lets the compiler catch most bad coding practices. And use the Java coding conventions, your future co-workers will like you better for it (plus, it'll help you get a job easier).
Cooldown
Posts: 660
Joined: Mon Jun 05, 2000 7:00 am

Re: Need Java Help - Prompting User for Input

Post by Cooldown »

Okay, got my problems solved.

^misantropia^ , what IDE do you prefer to use when programming in Java, if any?
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Need Java Help - Prompting User for Input

Post by ^misantropia^ »

joe :)

And NetBeans. The integration with Tomcat rocks.
Locked