DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Thread: array help

  1. #1
    Join Date
    Jan 2004
    Posts
    1

    Question array help

    Hi and thanks in advanced.

    this is my first attempt at java and im having trouble because i want to do things like in C++.

    ok i simply would like to create a program that will ask the user to enter a number of grades to be averaged. Store that number in a variable. Insert the veriable number in an array ( to set size ). Then promt the user to enter the gpa's to be averaged and save them in the array. heres my code so far.
    Code:
    import javax.swing.*;
    import java.text.*;
    
    public class average
    {
    public static void main(String args[])
    {
    String input;
    int a = 0;
    int c = 1;
    double b = 0;
    
    
    input = JOptionPane.showInputDialog("Please enter the number of grades to enter");
    a = Integer.parseInt(input);
    
    double[] average;
    average = new double[a];
    
    while(c <= a)
    {
    average[b] = JOptionPane.showInputDialog("Enter gpa #" + c); // <---------ERROR HERE
    c =c+1;
    }
    //more to come here
    System.exit(0);
    }
    }
    i dont know enough java to know why this error is here, this is the error
    Code:
    C:\java>javac average.java
    average.java:22: possible loss of precision
    found   : double
    required: int
    average[b] = JOptionPane.showInputDialog("Enter gpa #" + c);
            ^
    average.java:22: incompatible types
    found   : java.lang.String
    required: double
    average[b] = JOptionPane.showInputDialog("Enter gpa #" + c);
                                            ^
    2 errors
    lol i hope any1 can help. again thanks in advanced and god bless

    g.g.

    [ArchAngel added CODE tags]

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    None of your problems are really Java-specific - they're all typing problems that you get in any typed language.

    1. You're trying to index an array with a varible of type 'double' (the variable 'b'). This doesn't make sense - it's equivalent to typing myArray[10.3]! The compiler message is saying that it expects you to use an 'int', rather than 'double'.

    2. You're trying to assign a 'String' object (the value returned from .showInputDialog(...)) to a 'double' primitive (an element of the 'average' array). You can't just do that, you must parse the String into a double. For this, you can use Double.parseDouble(String):

    http://java.sun.com/j2se/1.3/docs/api/index.html

    Hope this helps.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    hey wait a minute.. i know you..

    (time to add my signature

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    hehe...
    ArchAngel.
    O:-)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links