Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: My code is not displaying on the console. In need of much help!

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My code is not displaying on the console. In need of much help!

    I'm trying to display some project and quiz scores to the console but the console is not displaying anything when I try to run the code. It just shows an empty screen.

    Here is the student class:

     public class Student {
     
     
         private String firstname;
     
         private String lastname;
     
         private int studentID;
     
         private double[] projects;
     
         private double[] quizzes;
     
        public Student(String firstname, String lastname, int ID) {
     
        firstname = firstname;
        lastname = lastname;
        studentID = ID;
        projects = new double[15];
        quizzes = new double[10];
     
     
        int i = this.projects.length - 1;
        while (i >= 0){
        this.projects[i] = -1;
        i--;
        }
     
        int i1 = this.quizzes.length - 1;
        while (i1 >= 0){
        this.quizzes[i1] = -1;
        i1--;
     
        }
        }
     
     
        public boolean setProjectScore(double projectScore, int projectID){
     
            if ((projectID >= projects.length) || (projectID < 0)) {
     
            return false;
     
        }
        else {
     
            projects[projectID] = projectScore;
     
            return true;
        }
     
        }
     
     
     
        public boolean setQuizScore(double quizScore, int quizID){
     
            if ((quizID >= quizzes.length) || (quizID < 0)) {
     
            return false;
        }
        else {
     
            quizzes[quizID] = quizScore;
     
            return true;
        }
     
        }
     
     
        public int getProjectsLength(){
     
            return this.projects.length;
     
        }
     
        public int getQuizzesLength() {
     
             return this.quizzes.length;
     
            }
     
     
        public double getProjectScore(int index){
     
            if (index < 0 || index > projects.length -1) {
     
                return -1;
            }
            return projects[index];
        }
     
     
     
        public double getQuizscore(int index) {
     
            if (index < 0 || index > quizzes.length -1) {
     
                return -1;
     
            }
            return quizzes[index];
        }
     
        public String getProjectScoreForID(int ID){
            String grade = "";
     
            if(ID<=this.projects.length-1){
     
                if (projects[ID] >= 90) {
                    grade = "A";
                }
                else if (80 <= projects[ID] && projects[ID] < 90) {
                    grade = "B";
                }
                else if (70 <= projects[ID] && projects[ID] < 80) {
                    grade = "C";
                }
                else if (60 <= projects[ID] && projects[ID] < 70) {
                    grade = "D";
                }
                else if (50 <= projects[ID] && projects[ID] < 60) {
                    grade = "F";
                }
            }
     
            return grade;
        }
     
        public String getQuizScoreForID(int ID){
     
            String grade = "";
     
            if(ID<=this.quizzes.length-1){
     
                if (quizzes[ID] >= 90) {
                    grade = "A";
                }
                else if (80 <= quizzes[ID] && quizzes[ID] < 90) {
                    grade = "B";
                }
                else if (70 <= quizzes[ID] && quizzes[ID] < 80) {
                    grade = "C";
                }
                else if
     
                (60 <= quizzes[ID] && quizzes[ID] < 70) {
     
                	grade = "D";
                }
     
     
                else if (50 <= quizzes[ID] && quizzes[ID] < 60) {
                    grade = "F";
     
     
                }
     
            }
     
            return grade;
        }
     
        public int getNextQuizIndex(){
     
            int i = 0;
        do {
     
            if (quizzes[i] == -1.0){
     
            return i;
        }
     
        i++;
     
        }
     
        while (i < quizzes.length);
                      return -1;
        }
     
     
     
     
        public int getNextProjectIndex(){
     
            int i = 0;
        do {
     
            if (projects[i] == -1.0){
     
            return i;
        }
     
        i++;
     
        }
     
        while (i < projects.length);
     
     
        return -1;
        }
     
        }

      public class Main {
     
     
     
            public static void main(String[] args) {
     
                Student jimmy = new Student(null, null, 0);
     
                int currentIndex=0;
                int projectLength = jimmy.getProjectsLength();
                int quizLength = jimmy.getQuizzesLength();
     
                while (currentIndex <= projectLength){
                    System.out.println(jimmy.getProjectScoreForID(currentIndex));
                    currentIndex++;
                }
                while (currentIndex <= quizLength) {
                    System.out.println(jimmy.getQuizScoreForID(currentIndex));
                    currentIndex++;
                }
        }
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    Add a call to System.out.println() that prints out the values of the variables used in the while() statement. Put the println statement before the while() statement. What it prints out should help you understand what the code is doing.

    Also add some printlns in the method that is called to show the values of the variables it in using.


    I see one problem in the code: there is a chain of if/else if/else if statements that does NOT having an ending else statement for the case where none of the above if statements are true. You need to add an ending else that prints out the value of the variable that failed the tests.
    Last edited by Norm; September 13th, 2012 at 12:34 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    Like this?

    System.out.println(projectLength);

    I added that before the while loop and the console just printed out 15 which is how many project scores there are. How can I display the grades of the projects?

    Thanks for the help

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    System.out.println(projectLength);
      vs this:  add an id string:
    System.out.println("pL="+projectLength);
    Also add some printlns in the method that is called to show the values of the variables it is using.

    I see one problem in the code: there is a chain of if/else if/else if statements that does NOT having an ending else statement for the case where none of the above if statements are true. You need to add an ending else that prints out the value of the variable that failed the tests.
    Last edited by Norm; September 13th, 2012 at 12:47 PM. Reason: spelling
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    Are u talking about in the main method?

    sorry but I was not sure how to write the 'else' statement so I tried something like this:

    else {

    grade = "-1";
    }

    I'm sure that is wrong :\

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    Add the debug printlns to all called methods so you can see what the variables' values are.

    What happens when you compile and execute the code? What is printed?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    debug printlns? I'm sorry but I've yet to hear of these or is that how one uses debugging? I've yet to debug a project since I'm still so new :\

    but I've added this line after the jimmy object

    jimmy.setProjectScore(100,0);

    It has printed the grade 'A' and -1 for the rest of the scores

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    When trying to find a bug in code (debug) you can use the System.out.println() statement to display the values of variables so you can see what the computer sees and understand what the code is doing and why it is doing it.

    What output are you expecting to be printed? And why are you expecting that output?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    I am expecting the grades in getProjectScoreForID() to display in the console. Once I have those grades displayed, I'll continue on with the rest of the grades for project and quiz.

    My project is to display a student name with 15 project scores and 10 quiz scores.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    Where do the values for the scores come from? How are they loaded into the program?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    I was trying to use the main method to pull the grades from getProjectScoresForID with this:

    System.out.println(jimmy.getProjectScoreForID(curr entIndex));

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    pull the grades from getProjectScoresForID with this
    How and where are the grades PUT into the jimmy object so that you can pull them?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    I'm using the if statements for the grades and am now using this in the main method to pull the grades from the if statements:

    jimmy.setProjectScore(100,0);
    jimmy.setProjectScore(85,0);
    jimmy.setProjectScore(73,0);

    The only problem is that only the second entry is displaying the grade i.e. 85 'B'

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code is not displaying on the console. In need of much help!

    Look at the definition of the setProjectScore() method and at how you are calling it in post#3.
    what is the second arg used for? What values are you using when you call the method?

    Your code tries to "pull" 15 values when you are only adding 3 values. Should the method in the class tell its caller how many values are available in the array instead of always saying 15 (the length of the array which is only partly used)?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  2. Displaying the other
    By Twoacross in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 08:58 AM
  3. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM
  4. my code displaying some minor wrong o/p ....plz suggest me an answer !
    By naved in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 11:59 AM
  5. Not Displaying what I need
    By rob3097 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2010, 10:31 PM