• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

What did I do wrong?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was writting a program to work out the bitwise operators, but I hit a wall it seems. Here is the code:

this will compile, but when I run it I got error that says at the line with **** mark I have an out of bound index for an array.
could some one tell me why this is the case?
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This means either array a or array testCase is being accessed with an illegal index. Can you explain how are you running this program and what will be the values of s?
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only check if (s[0].equals(testCase[choice])) while choice < s.length.
so add the condition to your loop, else the loop never breaks unless it finds a match.
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the s [] is the argument supplied when you execute this program. So it will contain something, I assume I will remember to input something and so I did not bother to check for null content. As for the breaking out, I have not condition to break out in the loop because I want to break out when the content of s[0] is the same as one of the bit operator in my testCase [] array, so I put in the if structure and when the equality does happen, the break; in it should break the loop.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what happens if that equality condition isn't met?
Taariq's right about the problem being with the for loop missing that condition - it's just the condition you need is slightly different.
What your for loop does is take the first member of the s array and then checks it against each member of the testCase array. Say, for example, the choice array has three members - first time through the loop, it checks if s[0] equals testCase[0], second time through if s[0] equals testCase[1] and third time if s[0] equals testCase[2]. That works just fine if s[0] equals testCase[0], testCase[1], or testCase[2] - it finds a match and it breaks out of the loop. However, if it doesn't find a match, the for loop continues to check to see if s[0] equals testCase[3] and there is none in this case - that's when you get your exception. Doesn't matter what the actual length of testCase is in practice - it can always happen that the for loop will reach the end of it and then try to access another array member which doesn't exist. So you need your for loop to look like this:-

Even if you're certain that something within the body of your for loop will cause it to terminate, it's always a good idea to put in that condition just in case.
Hope this solves the problem.
Kathy
[ June 24, 2002: Message edited by: Kathy Rogers ]
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help.
I modified it so that my testCase [] contains characters instead of symbols. It seems symbols for the bitwise operators causes problem.

so, I drop all these symbols and use characters. However, just what are all these symbols used for? And why is & treated like space?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did u try putting "" around the symbol arg u r passing....
'coz &, < , > etc symbols means certain other things when we use it...
if u r on Unix then "&" means put the process in background...things like that...
meanwhile i am trying to write similar code to c what u r exactly facing...
regards
maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi tried the following,

works for me fine when i run it with,
1. $java symbol "&"
2. $java symbol "|"
3. $java symbol "*" (not given any o/p here)
i tried this on Windows ME as well. it works fine...
tell me if i got u right and tried the correct thing ...
regards
maulin
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maulin, if you want I can paste my whole code here...though it is looonnnngggg.
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again, thanks Maulin
Yeah, I realize what I did wrong was when I input the bit operators, I did not enclose them with " ", so JVM treat the symbols as part of command line options. At least that is what I think.
My program was to take one bit operators and then use them. The out put would be the numbers involved, the result, and binary format for both the numbers involved and the result.
Thanks for all the help.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chung Huang:
I did not enclose them with " ", so JVM treat the symbols as part of command line options. At least that is what I think.


In fact, most of these options get evaluated by the operating system and would even never reach the JVM...
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically, you're all wrong... To be technically correct:
Depending on what OS "Shell" you're using, those symbols are interpreted by the Shell, not the Operating System or the JVM. The JVM is simply a program that the Shell executes. Here is the breakdown:

The first "word" on the command line is the program to execute. The rest are considered arguments to pass to the command that is being executed. As a matter of fact, 'java' is argument 0. If you were to compile a C or C++ program and run it, args[0] would be the name of your program. The JVM removes this since it would always contain 'java' and your Java program isn't concerned with the name of the JVM.
Anyway, The shell parses this entire string "java Test 1 2 3" before it figures out what to do with it. The shell has certain keywords and operators as well. Keep in mind Shell Scripting. The shell expects "words" such as: if, else, elif, fi, &, |, ||, &&, >>, <<, !, # and almost pretty much any operator you can use in Java, you can use in the Bash shell! Look how similar this is:

See, the Bash shell looks for specific "keywords" and operators. There are a couple of ways to escape this on the command line:
A) Put a \ before the operator
B) Surround it with double quotes
C) Surround it with single quotes
This will tell the Shell to not interpret this character as an operator or keyword, but take it literally (for lack of better words).
That's why you were having issues with just submiting an unescaped operator on the command line. The shell was trying to interpret that character as it should. Putting double quotes around it told the shell to escape interpreting it, you mean it to be passed along as-is.
Just wanted to help you understand the whole idea since you were a bit confused. This will help you in the future!
[ July 31, 2003: Message edited by: Dean Jones ]
[ July 31, 2003: Message edited by: Dean Jones ]
[ July 31, 2003: Message edited by: Dean Jones ]
 
Sheriff
Posts: 16910
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dean Jones:
Technically, you're all wrong... To be technically correct:
...{snip}...


Technically, they're not all wrong Ilja had it right.
Edit: OK, my bad. You said shell, not OS, which is what Ilja said. Maybe he wasn't making that fine of a distinction... I know I wasn't (to me the shell is, in all practicality, the OS, but that's not technically correct either) You win
[ July 31, 2003: Message edited by: Junilu Lacar ]
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic