7 Replies Latest reply: Apr 3, 2009 6:55 PM by jschellSomeoneStoleMyAlias RSS

    Help with Creating a String...

    807588
          public String getScore()
          {
              scores = "Technical Scores";
              scores += "\n";
              for(int i = 0; i < technical.size(); i ++)
              {
                  scores += technical.get(i).getJudgeName();
                  scores += " : ";
                  scores += technical.get(i).getRating();
                  scores += "\n";
              }
              scores += "Artistic Scores";
              scores += "\n";
              for(int i = 0; i < technical.size(); i ++)
              {
                  scores += artistic.get(i).getJudgeName();
                  scores += " : ";
                  scores += artistic.get(i).getRating();
                  scores += "\n";
              }
              return scores;
          }
      My code above is kinda self explanatory, but basically I need to grab a string and a Double from an object saved in an array list, and add them to a string... I am having a lot of trouble getting a string that is formatted and actually contains the data in the objects, Any help you might provide is appreciated... This is for a class, and I don't expect an answer (as I am doing the work) but I am lost here =\

      Edited by: ActingRude on Apr 3, 2009 4:35 PM
        • 1. Re: Help with Creating a String...
          jschellSomeoneStoleMyAlias
          You didn't explain what problem you are having.

          Do you really need to get a string? Normally for a display you should in fact display the values rather than attempting to do that in a string.
          • 2. Re: Help with Creating a String...
            807588
            Sorry, I am required by the assignment to return a string, the string is to be formatted like this:
            Technical Scores
            judgeName1: score1
            judgeName2: score2
            etc.
            Artistic Scores
            judgeName1: score1
            judgeName2: score2
            etc.
            for the life of me I can not get it to return a string like this, I am just getting the fallowing as a return:
            "Technical Scores \nArtisticScores\n"
            I susspect that I am using \n wrong, but I have tried \n /n '\n' "\n" and I get assorted errors but nothing useful... I hope I have made this clear...
            • 3. Re: Help with Creating a String...
              800313
              System.out.println("line1\nline2");
              should output to
              line1
              line2
              but why not change your code slightly to something like
              System.out.println("Technical Scores");
              System.out.println(getTechnicalJudgeName1()+" : "+getTechnicalScore1());
              System.out.println(getTechnicalJudgeName2()+" :" +getTechnicalScore2());
              
              System.out.println("Artistic Scores");
              System.out.println(getArtisticJudgeName1()+" : "+getArtisticScore1());
              System.out.println(getArtisticJudgeName2()+" : "+getArtisticScore2());
              • 4. Re: Help with Creating a String...
                jschellSomeoneStoleMyAlias
                Not possible. The characters \n represents an escape character. So if you are seeing "\n" in some output then that output is replacing the escape sequence with the characters '\' and 'n'.

                But at any rate you can try the following. Use 'eol' any place that you use "\n".

                String eol = System.getProperty("line.separator");
                Sorry, I am required by the assignment to return a string, the string is to be formatted like this:
                Just to be sure are you sure that the assignment doesn't require you to return a single line each time it is called? And then it calls your method multiple times to get all of the output?
                • 5. Re: Help with Creating a String...
                  jschellSomeoneStoleMyAlias
                  calypso wrote:
                  but why not change your code slightly to something like
                  Because, as the OP noted, the assignment is to return a string.
                  • 6. Re: Help with Creating a String...
                    807588
                    jschell thank you for your help, I am thinking it is probably just bluejay that is returning it then, and that it should return it correctly later,

                    calypso I did that at first until I re-read the assignment :) but thanks

                    One more question if you don't mind... now I am trying to grab the string from an outside class and I am getting a rather strange error...
                        public void listScores()
                        {
                            System.out.println( PerformanceScore.getScore() );
                        }
                    I would expect it to print the string to the terminal, but it is not letting me compile, I am getting this error:
                    non-static method getScore() cannot be referenced from a static context
                    any ideas?
                    • 7. Re: Help with Creating a String...
                      807588
                      When you attach getScore() to the class name PerformanceScore like that, you are telling the compiler that getScore() is a static or class method. (What this means is that all instances of PerformanceScore - say the instance which is this years and the instance which is last years - share the same getScore() method.) And they don't: each instance of PerformanceScore has its "own" getScore() which returns the string appropriate for that instance.

                      If you want to call the method from another class you will have to say which PerformanceScore instance you are talking about. Most likely you would do this by passing a reference to the PerformanceScore instance as an argument to listScores(). Somethig like:
                      public void listScores(PerformanceScore score)
                        {
                          System.out.println(score.getScore() );
                        }
                      Often you find you have some sort of "driver" class where the instance of PerformanceScore is created and then its getScore() method is called:
                      public static void main() 
                        {
                          PerformanceScore score = new PerformanceScore(); // or whatever the constructor should look like
                          // do stuff to give the score some data
                          listScores(score); // method implemented as above
                        }