Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help with function

Posted on 2004-11-14
3
Medium Priority
?
203 Views
Last Modified: 2010-03-31
Here is the function:

private static int CountHits(int userguess[], int adigit[], int digits,String args[]) throws Exception
{
      int Hits=0;
      int Misses=0;
      int x=0;

      for (x=0;x<digits;x++)
      {
            if (userguess[x]==adigit[x])
            {
                  Hits++;
            }
      }
//diagnostic
      String strChar = String.valueOf(args[0]);
                                                                        
if (strChar.equals("testing"))
      {
            
            x=0;
            System.out.print("DIAGNOSTIC:");
            for (x=0;x<digits;x++)
            {
                  System.out.print(adigit[x] + " ");  
            }

            System.out.print("\n");
            System.out.print("HIT RESULT:");
            for (x=0;x<digits;x++)
            {
                  if  (userguess[x]==adigit[x])
                  {
                        System.out.print("H ");  
                  }
                  else
                  {
                        System.out.print(". ");
                  }
            
            }
            System.out.print("\n");
      }
return Hits;

}

When I pass in this:
java Lab2 "testing"
everything works fine.

when i dont pass it in, I get an ArrayIndexOutOfBoundsException


how do i correct this?
0
Comment
Question by:pdidow
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12581280
can u post your main
0
 
LVL 92

Expert Comment

by:objects
ID: 12581283
>      String strChar = String.valueOf(args[0]);

If thats the args from main then thatline will cause that error
because args will be empty
0
 
LVL 13

Accepted Solution

by:
petmagdy earned 2000 total points
ID: 12581285
Hi pdidow,
//diagnostic

if(args.length >= 1)
{
     String strChar = String.valueOf(args[0]);
}
else
{
//ur descsion u may return for example
}


Cheers!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question