Advertisement

12.23.2003 at 02:53PM PST, ID: 20833718
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Java String Array
Tags: string, array, java
How do I  write a method that takes an array of Strings as a parameter and works out a total numerical score. As an example, {"A", "B"} would give a score of 3+2 = 5. The method returns
this score as an int. if my program below is as follow:

public class ModuleAnalyser
{
      private int score;

      public int turnAnswerToScore(String myGrade)
      {
          if(myGrade == "A") {
              score = 3;
          }
          else if(myGrade == "B") {
            score = 2;
          }
          else if(myGrade == "C") {
            score = 1;      
          }
          else if(myGrade == "D") {
            score = 0;
            }
            return score;
      }
}

Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: villechith
Solution Provided By: jimmack
Participating Experts: 9
Solution Grade: A
Views: 1320
Translate:
Loading Advertisement...
12.23.2003 at 02:56PM PST, ID: 9994207

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.23.2003 at 06:20PM PST, ID: 9994858

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.23.2003 at 06:41PM PST, ID: 9994909

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.23.2003 at 07:34PM PST, ID: 9995021

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.24.2003 at 12:57AM PST, ID: 9995746

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.24.2003 at 02:27AM PST, ID: 9995947

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.24.2003 at 02:34AM PST, ID: 9995966

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.07.2004 at 04:14PM PST, ID: 10067065

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.07.2004 at 05:55PM PST, ID: 10067602

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.08.2004 at 04:50PM PST, ID: 10076797

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.08.2004 at 04:54PM PST, ID: 10076820

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.17.2004 at 04:24PM PST, ID: 10138278

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.17.2004 at 05:37PM PST, ID: 10138543

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.18.2004 at 06:50AM PST, ID: 10140360

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.08.2005 at 01:49PM PST, ID: 13259104

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.08.2005 at 02:45PM PST, ID: 13259582

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.12.2005 at 10:47PM PDT, ID: 14428390

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
12.23.2003 at 02:56PM PST, ID: 9994207

Rank: Guru

    public int turnAnswerToScore(String[] myGrades)
     {
         int score = 0;

         for (int i = 0; i < myGrades.length; i++)
         {
             if(myGrades[i] == "A") {
                 score += 3;
             }
             else if(myGrades[i] == "B") {
                 score += 2;
             }
             else if(myGrades[i] == "C") {
                 score += 1;    
             }
             else if(myGrades[i] == "D") {
                 score += 0;
             }
         }
         
         return score;
     }
Accepted Solution
 
12.23.2003 at 06:20PM PST, ID: 9994858
I have a piece of elegant code for this, but first I need to know if the scores increment according to the alphabet. In other words, every letter is 1 less/more than the next/previous letter, or are the scores random?
-nat
 
12.23.2003 at 06:41PM PST, ID: 9994909

Rank: Genius

Don't like == with Strings (even if it works ;-)) This approach can be used:

public static int turnAnswerToScore(String[] myGrades) {
    int score = 0;
    final int REF = 'A' + 3;
    for (int i = 0;i < myGrades.length;i++) {
      score += REF - Character.toUpperCase(myGrades[i].charAt(0));
    }
    return score;
  }
 
12.23.2003 at 07:34PM PST, ID: 9995021

Rank: Genius

   public int turnAnswersToScore(String[] myGrades)
     {
         int score = 0;

         for (int i = 0; i < myGrades.length; i++)
         {
             switch (myGrades[i].charAt(0))
             {
               case 'A':
                 score += 3;
                 break;
               case 'B':
                 score += 2;
                 break;
               case 'C':
                 score += 1;
                 break;
             }
         }
         
         return score;
     }
 
12.24.2003 at 12:57AM PST, ID: 9995746
for the performance, it's better to change this:
for (int i = 0; i < myGrades.length; i++)

into:
int grades = myGrades.Length;
for (int i = 0; i < grades; i++)

try to avoid to terminate loops with method calls. it's more performant to use the integer variable "grades". first of all because operations on primitive datatypes (and especially the integer) are the fastest and because the int variable is directly accessible from the heap.

i know for this little piece of code, it won't have a big influence, but when you fine tune every loop in your program, you'll see some improvements

grtz

Lee
 
12.24.2003 at 02:27AM PST, ID: 9995947
Thank you all!  my problem is solved less than a day!  Thank you all of you, and sorry becos I can not accept all of your answer, eventhough your code are all excellent!
 
12.24.2003 at 02:34AM PST, ID: 9995966

Rank: Guru

Thank you for the points, but I must point out that CEHJ is right.  You should not use == to test the strings, you should use .equals().
 
01.07.2004 at 04:14PM PST, ID: 10067065
Some of the answers given are just plain wrong or misleading...and why would you add them up without dividing by the count?

For example...
   "for the performance, it's better to change this:
   for (int i = 0; i < myGrades.length; i++)
   into:
   int grades = myGrades.Length;
   for (int i = 0; i < grades; i++)
   try to avoid to terminate loops with METHOD CALLS. ..."

but myGrades.length is not a method call.  Methods have () after them.

And, no, using "==" does NOT work in this case unless you convert to char first.  Beyond that...perhaps some modularity is better...USE SUBROUTINES!  Even this solution is short-sighted, considering many schools use "A+", "A-" etc.

class SOMECLASS
{
    public static double turnAnswersToScore(String[] myGrades)
    {
         int score = 0;

         for (int i = 0; i < myGrades.length; i++)
          score += GradeToScore(myGrades[i].charAt(0));
         return score/(double)myGrades.length;
    }

   public static int GradeToScore(char grade)
   {
        switch (grade)
             {
               case 'A':
                 return 4;
               case 'B':
                 return 3;
               case 'C':
                 return 2;
               case 'D':
                 return 1;
               default:
                 return 0;
             }
         }
     }

 
01.07.2004 at 05:55PM PST, ID: 10067602

Rank: Genius

> switch (grade)

agreed :)
 
01.08.2004 at 04:50PM PST, ID: 10076797

Rank: Genius

>>but myGrades.length is not a method call.  Methods have () after them.

While what you say is strictly true, the spirit of leeprovoost's comment is correct: it still causes a relatively expensive instruction to be called in a loop which can be avoided
 
01.08.2004 at 04:54PM PST, ID: 10076820

Rank: Genius

i'd think the permance difference would be negligible (if any), and not that relevant for this example anyway.
Not saying there's anything wrong with leeprovoost's comment though, and it was clarified with the fact that it won't make much difference.

 
01.17.2004 at 04:24PM PST, ID: 10138278
I'd actually argue that in this particular case (since it's not a method call) it'll be slower to assign mygrades.length outside the loop.

The program is probablying going to be run on an x86 so you're going to be short on registers and pushing the values onto the stack. No if you assign it early rather than 'ad-hoc' you're going to be popping a lot more data off to get to the value you need.
 
01.17.2004 at 05:37PM PST, ID: 10138543
Just ran a quick test with the two versions:

import java.util.*;

public class LoopTest1{

public static void main(String[] args){
  Date start, end;
  start = new Date();
 
  Double[] theArray = new Double[20];
  int result = 0;
 
  for (int j=0;j<90000000;j++)
  {
    for (int i =0; i < theArray.length;i++)
      result = result + i;
  }
  end = new Date();
  System.out.println("Top of stack version(inside loop) took : "+ ( end.getTime() - start.getTime() ) );
       
}
}

AND THE OTHER VERSION:

import java.util.*;

public class LoopTest2{

public static void main(String[] args){
  Date start, end;
  start = new Date();
 
  Double[] theArray = new Double[20];
  int result = 0;
 
  int arrayLength =  theArray.length;
  for (int j=0;j<90000000;j++)
  {
    for (int i =0; i < arrayLength;i++)
      result = result + i;
  }
  end = new Date();
  System.out.println("Bottom of stack version (outside loop) took : "+ ( end.getTime() - start.getTime() ) );
       
}
}

Times:
C:\myjava>java LoopTest1
Top of stack version(inside loop) took : 6438

C:\myjava>java LoopTest1
Top of stack version(inside loop) took : 6500

C:\myjava>java LoopTest1
Top of stack version(inside loop) took : 6485

C:\myjava>java LoopTest1
Top of stack version(inside loop) took : 6516

C:\myjava>java LoopTest2
Bottom of stack version (outside loop) took : 7484

C:\myjava>java LoopTest2
Bottom of stack version (outside loop) took : 7485

C:\myjava>java LoopTest2
Bottom of stack version (outside loop) took : 7515

C:\myjava>java LoopTest2
Bottom of stack version (outside loop) took : 7578


This seems to confirm it since the loop is inside a method all the variables are local and use the stack.  I'm pretty sure this is right.
 
01.18.2004 at 06:50AM PST, ID: 10140360
That's very interesting....I didn't expect that result.

For timings, though, I would not use Date but System.getCurrentTimeMilli() -- that's not exactly the right method name, but it's something like that.

and to be a fare comparison with the code, the order should be different...but it should make your comparison eve more correct:

  for (int j=0;j<90000000;j++)
  {
    int arrayLength =  theArray.length;
    for (int i =0; i < arrayLength;i++)
      result = result + i;
  }

I have to say, though, this seems like a lot quibbling over a minor item...I see this a lot in software development...people start arguing over some minor point while the major issues go left unresolved or even unrecognized.  

And this seems to be an issue of 1 X 10 ** -5 ms

Of course, in "real time" software, these issues are potentially important if that item is hit millions of times...I should know, I do real-time java...sometimes you just have to try various ways and find ways to time them....(but not with Date)

 
02.08.2005 at 01:49PM PST, ID: 13259104
Gee, all this to help a guy doing his home work?  come on guys
 
02.08.2005 at 02:45PM PST, ID: 13259582
hey...but thanks to these guys for helping me, I actually understood it!!! who knows I might have given up on java if not because of these guys.  BTW, the first code is very plain simple, which for the beginner it is very helpful.  At the same time, i think the code from other guys are very good as well! thanks again,
 
07.12.2005 at 10:47PM PDT, ID: 14428390
long thread ..

this just caught my eye ;

----
 for the performance, it's better to change this:
for (int i = 0; i < myGrades.length; i++)

into:
int grades = myGrades.Length;
for (int i = 0; i < grades; i++)
----

- you gotta be kidding, right ? (not intended in the offensive style) ... if my JVM/javac cant optimize *that* .. what the hell *can* it ?
Ill have to test that out....

 
 
20080236-EE-VQP-29