Results 1 to 7 of 7
  1. #1
    jrJava is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default Trouble with calling a method

    I am having trouble with calling a method named getData in my program. Does anyone know why I am getting a compile error after the getData method is first called. Basically the program is suppose to calculate the sum of 8 different scores with the exclusion of the highest and lowest score.

    import java.util.*;

    public class Judging
    {
    public static void main (String[] args)
    {
    int arraySize=8;
    double [] scores=new double[arraySize];
    String name;
    String leave=Y; //continue or exit program

    Scanner console = new Scanner(System.in);

    while (leave!=N)
    {
    System.out.print("Please enter the contestant's name: ");
    name=console.next;
    Judging.getData(scores, scores.length);
    System.out.println("The score of " + name + " is " + calculateScore(scores, scores.length));
    System.out.print("Do you want to continue (Y/N)? ");
    leave=console.next;
    while(leave != "N" || leave !="Y")
    {
    System.out.print("Please enter (Y/N): ");
    leave=console.next;
    }
    }

    public static void getData(double[] mark,int scoreSize)
    {
    String[] judges={judge 1,judge 2,judge 3, judge 4, judge 5, judge 6, judge 7, judge 8};

    for(int i=0; i< scoreSize; i++; )
    {
    System.out.print("Give a " + judges[i] + " score: )";
    mark[i]=console.nextDouble;
    while (mark[i]<1 || mark[i]>10)
    {
    System.out.print("Please enter a score between 1 & 10: ");
    System.out.print("Give a " + judges[i] + " score: ");
    mark[i]=console.nextDouble;
    }
    }
    }

    public int calculateScore(double values[],int values.length)
    {
    double sum;
    double lowest;
    lowest = values[0];
    for (int j = 0; j<=values.length;j++)
    {
    if (values[j] < lowest)
    lowest = values[j];
    }

    double high;
    high = values[0];
    for (int k = 0; i<=values.length;k++)
    {
    if (values[k] > high)
    high = values[k];
    }

    for (int index=0;index<values.length; index++)
    {
    sum = index + values[index];
    }

    sum=sum-lowest-high;
    return sum;
    }
    }
    }

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Well Well Well...

    There are n number of errors in your code. I wonder how come you reached that line where getData gets called?

    Serious issues,
    1) You are declaring other methods inside your main method [Not allowed].
    2) You are declaring Strings without quotes ""
    3) You are trying to use the console methods [like next] without brackets ()
    4) Your String array contains values without quotes "" [again]
    5) You are trying to call non-static methods directly from main.
    ... and so on.

    Please rectify all these things and see what happens?

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    jrJava is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the advice Goldest, I finally got the program to compile all the way through. I just have a few errors mainly in the while loop (while(leave!="N" || leave!="Y"). I can't seem to get the program to exit this loop.


    import java.util.*;

    public class Judging
    {
    public static void main (String[] args)
    {
    int arraySize=8;
    double [] scores=new double[arraySize];
    String name;
    String leave=" "; //continue or exit program

    Scanner console = new Scanner(System.in);

    while (leave!="N")
    {
    System.out.print("Please enter the contestant's name: ");
    name=console.next();
    Judging.getData(scores, arraySize, console);
    System.out.println("The score of " + name + " is " + calculateScore(scores, arraySize));
    System.out.print("Do you want to continue (Y/N)? ");
    leave=console.next();
    while(leave!="N" || leave!="Y")
    {
    System.out.print("Please enter (Y/N): ");
    leave=console.next();

    }
    }
    }

    public static void getData(double[] mark,int scoreSize,Scanner console)
    {
    String[] judges={"judge 1","judge 2","judge 3", "judge 4", "judge 5", "judge 6", "judge 7", "judge 8"};

    for(int i=0; i<scoreSize;i++)
    {
    System.out.print("Give a " + judges[i] + " score: ");
    mark[i]=console.nextDouble();
    while (mark[i]<1 || mark[i]>10)
    {
    System.out.print("Please enter a score between 1 & 10: ");
    System.out.print("Give a " + judges[i] + " score: ");
    mark[i]=console.nextDouble();
    }
    }
    }

    public static double calculateScore(double values[],int size)
    {
    double sum=0;
    double lowest;
    lowest = values[0];
    for (int j = 0; j<=size-1;j++)
    {
    if (values[j]<lowest)
    lowest = values[j];
    }

    double high;
    high = values[0];
    for (int k = 0; k<=size-1;k++)
    {
    if (values[k] > high)
    high = values[k];
    }

    for (int index=0;index<values.length; index++)
    {
    sum = sum + values[index];
    }

    sum=sum-lowest-high;
    return sum;
    }
    }

  4. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi - when you are comparing strings, always use .equals() method.
    Java Code:
    (while(!leave.equals("N")) || !leave.equals("Y"))
    Best,
    --user0--

  5. #5
    jrJava is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks user0

  6. #6
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    You are most welcome :)
    --user0--

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    while(leave!="N" || leave!="Y")

    Hmmmmm!

    What if input is X? It doesn't equal N - true
    What if input is &? It doesn't equal N - true
    What if input is 3? It doesn't equal N - true
    What if input is Y? It doesn't equal N - true
    What if input is N? It doesn't equal Y - true

    When will it ever be false?

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  2. Trouble working with/calling Objects! Any Help?
    By ramathews in forum New To Java
    Replies: 4
    Last Post: 03-24-2010, 02:50 PM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Trouble will calling a method
    By jonsamwell in forum New To Java
    Replies: 9
    Last Post: 08-22-2008, 10:16 PM

Posting Permissions

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