CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2011
    Posts
    8

    Help adding scanner input to array

    Hopefully this will be a simple question. I have to create a program where the user enters 5 values,I add them, average them and state what is above the average. I have created this to a point. I successfully compiled and ran the program using JOptionpane, but because of the output I'd prefer to just put it in the console. My problem is that when I attempt to add the values to the array, it gives me the error:

    Unit9.java:29: incompatible types
    found : java.lang.String
    required: double
    myArray[i] = myscanner.next();

    I'm not sure why this is doing this. This is the top of my code.


    import java.util.Scanner;


    public class Unit9

    {
    public static void main (String [] args)

    {
    double total, average, round, value;
    total = 0;

    //create object using Scanner class
    Scanner myscanner = new Scanner(System.in);


    double []myArray = new double[5];

    for(int i=0; i<5;i++)

    {
    System.out.println("Please enter a value:");
    myArray[i] = myscanner.next();

    What I'm looking for is why this isn't working. I've used scanner input many times successfully, but it's not working here.

    Thanks,

    John

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Help adding scanner input to array

    The error message shows what the compiler is having trouble with
    Unit9.java:29: incompatible types
    found : java.lang.String
    required: double
    myArray[i] = myscanner.next();

    The next() method returns a String, but the type of myArray is NOT String, it is double.
    The compiler doesn't have a way to convert a String to double hence: incompatible types.
    You have to write the code to do that.
    See the Double class for methods to convert a String to a double.
    Or see the Scanner class for a method that reads a double instead of a String.
    Norm

  3. #3
    Join Date
    May 2011
    Location
    Finland
    Posts
    12

    Re: Help adding scanner input to array

    try
    myArray[i] = myscanner.nextDouble();

    myscanner.next() expects char inputs

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Help adding scanner input to array

    myscanner.next() expects char inputs
    next() will take anything as input. It's what it returns that is important for the OPs problem.
    Norm

  5. #5
    Join Date
    Jun 2011
    Posts
    8

    Re: Help adding scanner input to array

    Thank you. Thanks for explaining what I did wrong as well. It's an intro to Java class, but we have gone over so much that is hard to understand until you actually use it. Java is the first programming class I've taken. I'm told the other Object oriented languages are similiar. Just out of curiosity, why did it work when I used JOptionpane but not with the scanner? Does JOptionpane not care as much about the type of variable?

    Anyways thank you for your assistance. I still have a lot of work to do to get everything done in it.

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Help adding scanner input to array

    Read the API documentation for the JOptionPane's method that you were using. What data type did it return?
    Norm

  7. #7
    Join Date
    Jun 2011
    Posts
    8

    Re: Help adding scanner input to array

    See that's something I don't understand. All I did was the standard JOptionpane input that was I was taught.

    myArray[i] = Double.parseDouble(JOptionPane.showInputDialog("Enter a value"));

    I guess now that I just posted the line, I declared it as a double as I requested the user to enter the data.

    I have a new problem that is more of an appearance thing for me then anything. My example shows these numbers as currency. So when I print my values I've just been adding $ to the code.

    System.out.println("$"+myArray[i]);

    but if the value is a whole number it prints it with only 1 decimal place. How can I have it print 2 zero's after the whole number. I did a search for turning a double into currency and only became more confused.

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Help adding scanner input to array

    Code:
    Double.parseDouble(JOptionPane.showInputDialog(...
    There are two parts to that line you posted:
    The inner part: the showInputDialog() method returns a ???? see the API doc for details
    The outer part: the parseDouble() method takes a ??? and converts it to a ??? See doc for details

    The ??? in the above could be String and double or some other data types. Instead of typing it in and being wrong I left it for your to look up and verify.

    How can I have it print 2 zero's after the whole number
    There is a java class that will format numbers for you: DecimalFormat
    Also the printf() method will do some formatting.
    Norm

  9. #9
    Join Date
    Jun 2011
    Posts
    8

    Re: Help adding scanner input to array

    Norm:

    Thanks for your help. I tried the printf but it didn't work very well with my program. I opted to use the DecimalFormat method and that worked fine. I even used it to change how I rounded my averages.

    As for understanding JOptionpane, perhaps it's in a more complex programming class. As I understand it, all the classes we import are things added to Java because they found people used them over and over. So is the programming around for JOptionpane that might explain things better. I guess I should start with the API and go from there.

    Thanks again.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured