Results 1 to 8 of 8
  1. #1
    wtbarr is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Help with outputting a specific string array

    Hey guys,

    I have a program to write and I have it almost all the way down, but there is one issue that I either can't solve for a reason I'm unsure of or I just tried solving it the wrong way entirely, which means I have a bunch of fixing up to do. I was hoping you guys could give me some guidance about which one of those are the case, and ways to fix my mistakes.

    The code is meant to read in data (a collection of the letters 'a', 'b', and 'c') that will allow for a calculation of points based on those answers and an overall percent based on those points. That is not a problem, I figured that out pretty quick. The next part is to use that data to determine whether the grade/rating for the person is an E (excellent), A (Above Average), S (Satisfactory), or U (Unsatisfactory). As of now, my code is having trouble passing this method when I call it back to main.

    Here's the code, the method that is the problem is the ratePatient method, and the call to it in main (String[] rate):

    (Also ignore the comments, it was requested that I

    Java Code:
    import java.io.*;
    import java.util.*;
    public class TraitEvaluation {
    
        public static void main(String[] args) throws FileNotFoundException {
            Scanner input = new Scanner(new File("patientward.txt"));
            int trait = 1;
            while(input.hasNextLine()) {
                String line = input.nextLine();
                int[] score = countPoints(line);
                double[] percent = countScore(score);
    	    String[] rate = ratePatient(rating, percent);
                printResults(trait, score, percent, rate);
                trait++;
            }
        }
       
        public static void printResults(int trait, int[] score, double[] percent, String[] rate){
    
            System.out.println("Trait: " + trait);
            System.out.println("Patient Scores: " + Arrays.toString(score));
            System.out.println("Patient Percentages: " + Arrays.toString(percent));
            System.out.println("Patient Rating: " + Arrays.toString(rate));
            System.out.println();
        }
       
        public static int[] countPoints(String line) {
    
            int[] score = new int[10];
            for(int i=0; i<line.length(); i++){
                int patient = i%10;
                int earned = 0;
                if(line.charAt(i) == 'b'){
                    earned = 2;
                } else if(line.charAt(i) == 'c'){
                    earned = 5;
                }
                score[patient]= score[patient] + earned;
            }
            return score;
        }
       
        public static double[] countScore(int[] score) {
    
            double[] points = new double[10];
            for(int i=0; i<score.length; i++){
                points[i] = 100 * score[i] / 25;
            }
            return points;
        }
       
        public static String[] ratePatient(String[] rating, double[] percent){   
                for(int i=0; i<percent.length; i++){
                    if(percent[i]>=70){
                        rating[i] = "E";
                    } else if (percent[i]>=60){
                        rating[i] = "A";
                    } else if (percent[i]>=40){
                        rating[i] = "S";
                    } else {
                        rating[i] = "U";
                    }
                }
                return rating;
        }
    }

    The output should look like this:

    trait 1
    patient scores: [0, 2, 10, 15, 25, 20, 25, 16, 13, 10]
    patient percentages: [0, 8, 40, 60, 100, 80, 100, 64, 52, 40]
    patient rating: [U, U, S, A, E, E, E, A, S, S]
    trait 2
    patient scores: [15, 25, 20, 25, 19, 16, 10, 2, 4, 8]
    patient percentages: [60, 100, 80, 100, 76, 64, 40, 8, 16, 32]
    patient rating: [A, E, E, E, E, A, S, U, U, U]
    trait 3
    patient scores: [25, 15, 25, 16, 16, 10, 0, 0, 10, 10]
    patient percentages: [100, 60, 100, 64, 64, 40, 0, 0, 40, 40]
    patient rating: [E, A, E, A, A, S, U, U, S, S]
    trait 4
    patient scores: [25, 10, 25, 19, 19, 10, 0, 0, 10, 15]
    patient percentages: [100, 40, 100, 76, 76, 40, 0, 0, 40, 60]
    patient rating: [E, S, E, E, E, S, U, U, S, A]




    Any help to get that last line working properly would be greatly appreciated. Thank you if you decide to help out!

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Help with outputting a specific string array

    Seriously, did you write the countPoints and countScore methods or did you get that from somewhere and try to insert a new method ratePatient? If you applied the same logic and structure in ratePatient as in the other methods, everything worked fine.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    wtbarr is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Help with outputting a specific string array

    I wrote all of the methods as such because that was the assignment, I didn't get them from somewhere else. However, after trying to mirror the structure of those two methods, I'm stuck.

  4. #4
    wtbarr is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Help with outputting a specific string array

    Still looking for help on this if anyone can help me figure out either the right call to this method, the right parameters, or just in general figure out where I went wrong in the ratePatient method.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Help with outputting a specific string array

    Hi wtbarr,

    Where is the variable rating used in line 12 defined?

    Reagrds.
    Last edited by Ronin; 02-10-2013 at 10:32 AM. Reason: typo

  6. #6
    wtbarr is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Help with outputting a specific string array

    Good question; it originally wasn't, and having tried to figure out how and where to declare it, I really have nothing. I tried declaring it as String[] rating = {"E", "A", "S", "U"}; in main, but the error I'm getting now is

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at TraitEvaluation.ratePatient(TraitEvaluation.java:7 1)
    at TraitEvaluation.main(TraitEvaluation.java:19)"

  7. #7
    wtbarr is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Help with outputting a specific string array

    Bump, still looking for assistance on this one. =/

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Help with outputting a specific string array

    Hi wtbarr,

    The error you are seeing is caused by a reference to an array index which does not exist.

    Instead of passing rating into the method you could create an array of length percent within the method itself and just return this.

    Regards.

Similar Threads

  1. Get characters between specific elements in string
    By hacikho in forum New To Java
    Replies: 6
    Last Post: 11-24-2011, 08:37 PM
  2. Replies: 1
    Last Post: 11-13-2011, 06:23 PM
  3. Replies: 1
    Last Post: 11-13-2011, 11:25 AM
  4. How to withdraw a specific from a String?
    By equal in forum New To Java
    Replies: 6
    Last Post: 05-29-2011, 09:02 PM
  5. No of a specific char in a string
    By alinaqvi90 in forum New To Java
    Replies: 13
    Last Post: 05-27-2010, 07:59 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
  •