Results 1 to 7 of 7
  1. #1
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    112
    Rep Power
    0

    Default 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;
    
    class ModuleCulminatingTask {
      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(Math.toRadians(var5));  //converts var 5 to radiands
         System.out.println ("The tan of " + var5 + " is " + DForm.format(Math.tan(var5))); //gives you the tan of var5
         System.out.println(Math.toRadians(var6)); //converts var 6 to radiands
         System.out.println ("The sin of " + var6 + " is " + DForm.format(Math.sin(var6))); //gives you the sin of var6
         System.out.println(Math.toRadians(var7)); //converts var 7 to radiands
         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();
        
          var3 = Float.valueOf(userInput.readLine()).floatValue();
          var4 = Float.valueOf(userInput.readLine()).floatValue();
          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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    112
    Rep Power
    0

    Default Re: User Input Problem

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

    2 errors found:
    File: /Users/brad/Downloads/ModuleCulminatingTask.java [line: 53]
    Error: /Users/brad/Downloads/ModuleCulminatingTask.java:53: int cannot be dereferenced
    File: /Users/brad/Downloads/ModuleCulminatingTask.java [line: 53]
    Error: /Users/brad/Downloads/ModuleCulminatingTask.java:53: inconvertible types
    found : <any>
    required: char

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

    Default 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.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    112
    Rep Power
    0

    Default 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 sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:992)
    at java.lang.Float.valueOf(Float.java:388)
    at ModuleCulminatingTask.main(ModuleCulminatingTask.j ava:55)
    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. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    112
    Rep Power
    0

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

Similar Threads

  1. User input
    By iLearnJava in forum New To Java
    Replies: 3
    Last Post: 12-06-2014, 07:18 PM
  2. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  3. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 02:00 PM
  4. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Posting Permissions

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