Re: Char Array Increment + 1
[QUOTE=andreas90;62722]You do that with your for loop if i'm getting it right.
Yes, but I have to set the variable char value to something. Like how I set char value = 'C' and it found the position of C in my array or how I set it to char value = 0 as in my first post and all it did was print out 0.
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
Yes, but I have to set the variable char value to something. Like how I set char value = 'C' and it found the position of C in my array or how I set it to char value = 0 as in my first post and all it did was print out 0.
What if you assign "value" the charAt(int) method of the String the user inputs and then do the binarySearch with it - both inside the for loop?
Re: Char Array Increment + 1
The main problem I see with this program is that no design work was done to lay out the steps for the logic to take.
It looks like the OP has tried to write a program without first designing how it should work.
The results are the mess that is posted here.
Re: Char Array Increment + 1
Modified For loop by adding in charAt method. It now prints what I input in letters. How would I increment the value of the input so it shifts one position to print out the next alphabet character? I tried doing System.out.print(result+1) but it does nothing.
Code :
for (int i = 0; i < line.length(); i++) {
value=line.charAt(i);
int result = Arrays.binarySearch(Alphabet,value);
if (value < 25) {
System.out.print(result);
} else if (result == 25) {
System.out.print(Alphabet[0]);
} else {
System.out.print(value);
}
}
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
Modified For loop by adding in charAt method. It now prints what I input in letters. How would I increment the value of the input so it shifts one position to print out the next alphabet character? I tried doing System.out.print(result+1) but it does nothing.
Code :
for (int i = 0; i < line.length(); i++) {
value=line.charAt(i);
int result = Arrays.binarySearch(Alphabet,value);
if (value < 25) {
System.out.print(result);
} else if (result == 25) {
System.out.print(Alphabet[0]);
} else {
System.out.print(value);
}
}
Instead of printing result you can use result as an index in the Alphabet array and print the element in the specified index.
Re: Char Array Increment + 1
Quote:
Originally Posted by
andreas90
Instead of printing result you can use result as an index in the Alphabet array and print the element in the specified index.
Code :
System.out.print(Alphabet[i+1]);
I came up this statement. I would print the result in the array Alphabet + 1 so it would increment it by one. But its not working. Can you help me?
Re: Char Array Increment + 1
Please explain and post any error messages.
What is printed? What is the value of i?
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
Code :
System.out.print(Alphabet[i+1]);
I came up this statement. I would print the result in the array Alphabet + 1 so it would increment it by one. But its not working. Can you help me?
I suggested you to use result not i as an index.
Re: Char Array Increment + 1
The print statement is printing out the string that I input, which is "BALL". The value of i is found when binarySearch looks for the characters that is entered in as input. I have searched the input for each value of the elements found in my array, then printed each one of them as string letters through a charAt(i) statement. I just don't know how to increment that value by 1 so it will print out the result shifted over one element of the array.
Re: Char Array Increment + 1
Modified my for loop. I input "BALL" and now it outputs, "CCCCCCCCCCCCCCCCCCCCCCCCCCBBBBBBBBBBBBBBBBBBBBBBB BBBMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMM". It did increment each character by 1, but I want the output to be the length of the input. Like "CBMM".
Code :
for (int i = 0; i < line.length(); i++) {
for (int j=0; j<Alphabet.length; j++) {
value=line.charAt(i);
int result = Arrays.binarySearch(Alphabet,value);
System.out.print(Alphabet[result+1]);
}
}
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
Modified my for loop. I input "BALL" and now it outputs, "CCCCCCCCCCCCCCCCCCCCCCCCCCBBBBBBBBBBBBBBBBBBBBBBB BBBMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMM". It did increment each character by 1, but I want the output to be the length of the input. Like "CBMM".
Code :
for (int i = 0; i < line.length(); i++) {
for (int j=0; j<Alphabet.length; j++) {
value=line.charAt(i);
int result = Arrays.binarySearch(Alphabet,value);
System.out.print(Alphabet[result+1]);
}
}
Why are you using a nested loop? What do you need the loop that goes through Alphabet? I think that you will get what you want by removing the inner for loop but are you having (and writing somewhere) the logic before writing code or you are just experimenting to see what will happen? It's very important understanding what you do because this will help you find errors or bugs.
Re: Char Array Increment + 1
Oh, for some reason when I was compiling, it kept printing out "BALL". So I add the nested for loop to see if that would work. I didn't change anything but remove the nested for loop again. Hm, now it works. Thanks for the help guys. I know it must be a pain trying to help me code properly. My problem is I have to look at a sample piece of code before I can code it myself. How long did it take you guys to learn to be able to code java properly with few errors?
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
My problem is I have to look at a sample piece of code before I can code it myself. How long did it take you guys to learn to be able to code java properly with few errors?
I am a beginner too. The only advise i can give you is write down the logic of your program and make a design of it before you try to implement it. I didn't do that when i first started java programming because i thought it was boring and not necessary. But then i started dealing with some bigger projects and i realised that it was impossible implement them without the logic in a piece of paper. So it's important to have some kind of design before you start writing code -although you may not need it in simple programs- just to get used to it for later, when you will really need it.
Hope it makes sense.
Re: Char Array Increment + 1
you can use charAt () method or convert integer to string...
:)
before this you can define a value for character.
and initialized it..