here's my code:
for(int i=0; i<plainTextUpper.length()-1; i++)
{
System.out.println(charCodeAt(i));
}
It won't compile though, because it says charCodeAt's symbol was not found. Am I missing a library? The only one I have imported right now is java.util.*
Assuming that this is Java and not Javascript, you probably want this:
for(int i=0; i<plainTextUpper.length()-1; i++)
{
System.out.println(plainTextupper.codePointAt(i));
}
Note that this will not process the last character of plainTextUpper. You probably also want to either get rid of the -1 or change the comparison operator to <= in the for termination test.
Related
I am working on this program and I keep getting this error in Java Eclipse saying that "i cannot be resolved to a variable." when I try to get the output..
Here is the relevant code.
for (int i = 0; i < animal.length; i++){
animal[i].move();
animal[i].makeSound();
if (animal[i] instanceof Leopard)
animal[i].findTree();
if (animal[i] instanceof Bat)
animal[i].locateInsect();
if (animal[i] instanceof Chameleon)
animal[i].changeColor();
}
System.out.println(animal[i].getName());
System.out.println();
This is the line that shows the error but I am not sure why its causing an error message.
System.out.println(animal[i].getName());
for (int i = 0; i < animal.length; i++){
//...
}
System.out.println(animal[i].getName());
You're trying to reference the i outside of the forloop where you created it, so it doesn't exist.
Simply move System.out.println(animal[i].getName()); inside of the forloop like this:
for (int i = 0; i < animal.length; i++){
//...
System.out.println(animal[i].getName());
}
You need to understands the basics of java variable scope
http://www.java-made-easy.com/variable-scope.html
In checkstyle I have enabled the check for Indentation. I'm getting a weird problem with it.
The check is working fine for everything other than statement label.
I have a code snippet like the following :
public void doIt(int k) {
for (int i = 0; i < k; i++){
search:{
for (int j = 0; j < i; j++){
if (j == i){
break search;
}
}
}
}
}
The indent level is set as 4.
Now, if I put the statement label (search) at level 11, it should give one warning as
- label child at indentation level 11 not at correct indentation, 12
But the problem is, its giving Multiple markers at that line :
- label child at indentation level 11 not at correct indentation, 12
- label child at indentation level 11 not at correct indentation, 8
So, no matter in which level I indent the label, there will always be one/two warnings.
I didn't enabled duplicate checks for indentation with two different Indent Level.
How am I getting two warnings for a single check? How to resolve this issue?
This is a limitation of the IndentationCheck. The label indentation is hardcoded to be one level less than the normal indentation at this point in the tree (verified by looking at the Checkstyle 5.6 sources). The next token, which is the left curly brace, or, if you leave out the braces, the for statement, must be on the expected level. So you could format the code like this with no errors:
public void doIt(int k) {
for (int i = 0; i < k; i++){
search:
for (int j = 0; j < i; j++){
if (j == i){
break search;
}
}
}
}
It is of course a matter of personal taste, but I don't like it. I would recommend not using Checkstyle for checking the formatting, and instead use an automatic code formatter. Eclipse, for instance, has a nice formatter built in.
I have this code, it should find a pre known method's name in the chosen file:
String[] sorok = new String[listaZ.size()];
String[] sorokPlusz1 = new String[listaIdeig.size()];
boolean keresesiFeltetel1;
boolean keresesiFeltetel3;
boolean keresesiFeltetel4;
int ind=0;
for (int i = 0; i < listaZ.size(); i++) {
for (int id = 0; id < listaIdeig.size(); id++) {
sorok = listaZ.get(i);
sorokPlusz1 = listaIdeig.get(id);
for (int j = 0; j < sorok.length; j++) {
for (int jj = 1; jj < sorok.length; jj++) {
keresesiFeltetel3 = (sorok[j].equals(oldName)) && (sorokPlusz1[id].startsWith("("));
keresesiFeltetel4 = sorok[j].startsWith(oldNameV3);
keresesiFeltetel1 = sorok[j].equals(oldName) && sorok[jj].startsWith("(");
if (keresesiFeltetel1 || keresesiFeltetel3 || keresesiFeltetel4) {
Array.set(sorok, j, newName);
listaZarojeles.set(i, sorok);
}
}
System.out.println(ind +". index, element: " +sorok[j]);
}
ind++;
}
}
listaZ is an ArrayList, elements spearated by '(' and ' ', listaIdeig is this list, without the first line (because of the keresesifeltetel3)
oldNameV3 is: oldName+ ()
I'd like to find a method's name if this is looking like this:
methodname
() {...
To do this I need the next line in keresesifeltetel 3, but I can't get it working properly. It's not finding anything or dropping errors.
Right now it writes out the input file's element's about 15 times, then it should; and shows error on keresesifeltetel3, and:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
I think your problem is here: sorokPlusz1[id]. id does not seem to span sorokPlusz1's range. I suspect you want to use jj and that jj should span sorokPlusz1's range instead of sorok's and that sorok[jj].startsWith("(") should be sorokPlusz1[jj].startsWith("(").
But note that I'm largely speculating as I'm not 100% sure what you're trying to do or what listaZ and listaIdeig look like.
You're creating sorok with size = listaZ's size, and then you do this: sorok = listaZ.get(i);. This is clearly not right. Not knowing the exact type of listaZ makes it difficult to tell you what's wrong with it. If it's ArrayList<String[]>, then change
String[] sorok = new String[listaZ.size()]; to String[] sorok = null; or String[] sorok;. If it's ArrayList<String> then you probably want to do something more like sorok[i] = listaZ.get(i);
Now for some general notes about asking questions here: (with some repetition of what was said in the comments) (in the spirit of helping you be successful in getting answers to questions on this site).
Your question is generally unclear. After reading through your question and the code, I still have little idea what you're trying to do and what the input variables (listaZ and listaIdeig) look like.
Using non-English variable names makes it more difficult for any English speaker to help. Even changing sorok to array and keresesiFeltetelX to bX would be better (though still not great). Having long variable names that aren't understandable makes it much more difficult to read.
Comment your code. Enough comments (on almost every line) makes it much easier to understand your code.
Examples. If you have difficulty properly explaining what you want to do (in English), you can always provide a few examples which would assist your explanation a great deal (and doing this is a good idea in general). Note that a good example is both providing the input and the desired output (and the actual output, if applicable).
class TwoDimArryAlloc {
public static void main(String[] args) {
int itemno[][] = new int[][] { {2234,2235,2236,2237,2238} , {3334,3335,3336,3337,3338} };
int it;
String itemdesc[][] = new String[][] {{"Womans Item1","Womans Item2","Womans Item3","Womans Item4","Womans Item5"},{"Mans Item1","Mans Item2","Mans Item3","Mans Item4","Mans Item5"}};
for (int i=0; i<2; i++)
{
for(int j=0; j<5; j++)
{
System.out.println(itemno[][]);
}
}
}
}
In the above program, I got the following error:
C:\Java Programs\TwoDimArryAlloc.java:18: '.class' expected
System.out.println(itemno[][]);
^
1 error
Process completed.
Can anyone help me solve this error?
You need to provide the array index:
System.out.println(itemno[i][j]);
^ ^
Instead of
System.out.println(itemno[][]);
you want to say
System.out.println(itemno[i][j]);
Without specifying which indexes you want, it is bad syntax, which confuses the compiler so much that it emits an impenetrable error message.
(The first pass of compilation is to construct a syntax tree, and this happens before the compiler figures out which names denote types and which ones denote variables. So when it sees itemno[][] it reasons that this could be the beginning of a valid expression if only itemno were the name of a type, but in that case the full expression would have to be itemno[][].class -- so that's what it asks for even though you meant something entirely different).
{
int itemno[][] = { {2234,2235,2236,2237,2238} , {3334,3335,3336,3337,3338} };
int it;
String itemdesc[][] = {{"Womans Item1","Womans Item2","Womans Item3","Womans Item4","Womans Item5"},{"Mans Item1","Mans Item2","Mans Item3","Mans Item4","Mans Item5"}};
for (int i=0; i<2; i++)
{
for(int j=0; j<5; j++)
{
System.out.println(""+itemno[i][j]);
}
}
}
You forgot to provide index in SYSO statement. Make it as follow:
System.out.println(itemno[i][j]);
You forgot to use the indexes:
System.out.println(itemno[i][j]);
public boolean catDog(String str)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
{
String sub = str.substring(i, i+1);
if (sub.equals("cat") && sub.equals("dog"))
count++;
}
return count == 0;
}
There's my code for catDog, have been working on it for a while and just cannot find out what's wrong. Help would be much appreciated!*/
EDIT- I want to Return true if the string "cat" and "dog" appear the same number of times in the given string.
One problem is that this will never be true:
if (sub.equals("cat") && sub.equals("dog"))
&& means and. || means or.
However, another problem is that your code looks like your are flailing around randomly trying to get it to work. Everyone does this to some extent in their first programming class, but it's a bad habit. Try to come up with a clear mental picture of how to solve the problem before you write any code, then write the code, then verify that the code actually does what you think it should do and that your initial solution was correct.
EDIT: What I said goes double now that you've clarified what your function is supposed to do. Your approach to solving the problem is not correct, so you need to rethink how to solve the problem, not futz with the implementation.
Here's a critique since I don't believe in giving code for homework. But you have at least tried which is better than most of the clowns posting homework here.
you need two variables, one for storing cat occurrences, one for dog, or a way of telling the difference.
your substring isn't getting enough characters.
a string can never be both cat and dog, you need to check them independently and update the right count.
your return statement should return true if catcount is equal to dogcount, although your version would work if you stored the differences between cats and dogs.
Other than those, I'd be using string searches rather than checking every position but that may be your next assignment. The method you've chosen is perfectly adequate for CS101-type homework.
It should be reasonably easy to get yours working if you address the points I gave above. One thing you may want to try is inserting debugging statements at important places in your code such as:
System.out.println(
"i = " + Integer.toString (i) +
", sub = ["+sub+"]" +
", count = " + Integer.toString(count));
immediately before the closing brace of the for loop. This is invaluable in figuring out what your code is doing wrong.
Here's my ROT13 version if you run into too much trouble and want something to compare it to, but please don't use it without getting yours working first. That doesn't help you in the long run. And, it's almost certain that your educators are tracking StackOverflow to detect plagiarism anyway, so it wouldn't even help you in the short term.
Not that I really care, the more dumb coders in the employment pool, the better it is for me :-)
choyvp obbyrna pngQbt(Fgevat fge) {
vag qvssrerapr = 0;
sbe (vag v = 0; v < fge.yratgu() - 2; v++) {
Fgevat fho = fge.fhofgevat(v, v+3);
vs (fho.rdhnyf("png")) {
qvssrerapr++;
} ryfr {
vs (fho.rdhnyf("qbt")) {
qvssrerapr--;
}
}
}
erghea qvssrerapr == 0;
}
Another thing to note here is that substring in Java's built-in String class is exclusive on the upper bound.
That is, for String str = "abcdefg", str.substring( 0, 2 ) retrieves "ab" rather than "abc." To match 3 characters, you need to get the substring from i to i+3.
My code for do this:
public boolean catDog(String str) {
if ((new StringTokenizer(str, "cat")).countTokens() ==
(new StringTokenizer(str, "dog")).countTokens()) {
return true;
}
return false;
}
Hope this will help you
EDIT: Sorry this code will not work since you can have 2 tokens side by side in your string. Best if you use countMatches from StringUtils Apache commons library.
String sub = str.substring(i, i+1);
The above line is only getting a 2-character substring so instead of getting "cat" you'll get "ca" and it will never match. Fix this by changing 'i+1' to 'i+2'.
Edit: Now that you've clarified your question in the comments: You should have two counter variables, one to count the 'dog's and one to count the 'cat's. Then at the end return true if count_cats == count_dogs.