# Thread: User Input Problem

1. Senior Member
Join Date
Apr 2012
Posts
112
Rep Power
0

## User Input Problem

I am trying to get user input for a char value and am having some difficulty getting input for a char value. Can anyone point out where I am going wrong.

Java Code:
```//imports packages
import java.io.*;
import java.text.DecimalFormat;

public static void main (String args []) {

//declares variables

float var3, var4;

long var5 = (int)(Math.random()*10);
long var6 = (int)(Math.random()*10);
int var7 = (int)(Math.random()*10);
int var8 = (int)(Math.random()*10);

//Sets the maximum digits for an answer
DecimalFormat DForm = new DecimalFormat ();
DForm.setMinimumFractionDigits(2);
DForm.setMaximumFractionDigits(3);

System.out.printf("The random numbers generated are: " + "\n" + var5 + "\n"); //tells the user the generated number and creates a new line
System.out.print( var6 + "\n"); //tells the user the generated number and creates a new line
System.out.print( var7 + "\n"); //tells the user the generated number and creates a new line
System.out.print( var8 + "\n"); //tells the user the generated number and creates a new line

System.out.println ("The tan of " + var5 + " is " + DForm.format(Math.tan(var5))); //gives you the tan of var5
System.out.println ("The sin of " + var6 + " is " + DForm.format(Math.sin(var6))); //gives you the sin of var6
System.out.println ("The cos of " + var7 + " is " + DForm.format(Math.tan(var7))); //gives you the cos of var7

BufferedReader userInput; //declares a new instance of buffered reader
userInput = new BufferedReader(new InputStreamReader(System.in)); //make the reader a regular input buffer

System.out.println("Please enter a single whole number into the first two fields, then and number in the last two"); //tells the user to type in a number

try //initializes a try... catch to catch errors and prevent instant crashing
{

char var1 = (char) userInput.read().charValue();

System.out.println("You entered " + var3 + "and " + var4); //tells you the numbers

}
catch(IOException ioe) //looks for things that don't make sense (improper inputs etc)
{
System.out.println ("An unexpected error occurred"); //tells you when an error occurs
}

}
}```

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: User Input Problem

Please show both the input and the expected output. Also, for console input I find the Scanner class easier to use.

Regards,
Jim

3. Senior Member
Join Date
Apr 2012
Posts
112
Rep Power
0

## Re: User Input Problem

When I try to compile the program I get the following error message

2 errors found:
found : <any>
required: char

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: User Input Problem

That's a compile problem. The method read() returns a primitive value of type int. Then you try to invoke a method of charValue(). You can't dereference an int.
And I noticed you called the Math.tan() method rather than the Math.cos() method.

Regards,
Jim
Last edited by jim829; 01-10-2015 at 12:28 AM.

5. Senior Member
Join Date
Apr 2012
Posts
112
Rep Power
0

## Re: User Input Problem

I changed line 46 to the following and it compiles but I still get an error when I try to enter data

Java Code:
`      char var1 = (char) userInput.read();`
java.lang.NumberFormatException: empty String
at java.lang.Float.valueOf(Float.java:388)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: User Input Problem

You need to use print statements to help debug your program. And don't combine method calls. Don't do this:

Java Code:
`var3 = Float.valueOf(userInput.readLine()).floatValue();`
Do this:

Java Code:
```String str = userInput.readLine();
Float fltval = Float.valueOf(str);
float v = fltval.floatValue();```
Otherwise, you won't be able to print the return values of the methods.

Regards,
Jim

7. Senior Member
Join Date
Apr 2012
Posts
112
Rep Power
0

## Re: User Input Problem

I am really really sorry but I do not fully understand. The code works when I remove the one line of code related to getting a char value; that is where the problem seems to be.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•