Re: Char Array Increment + 1
Please explain the logic of the steps your program is doing. Make a list in pseudo code.
Re: Char Array Increment + 1
Hello Nuggets!
Why are you using FileReader to read from the console? In my opinion you are using way too much code for just asking the user for input.
And also, as i can see you have in your for loop
Code java:
if (result < 25) {
System.out.print(result + 1);
So it will print the same int for every character of the String the user inputs if result is less than 25.
Hope i explained it right.
Re: Char Array Increment + 1
Quote:
Originally Posted by
andreas90
Hello Nuggets!
Why are you using
FileReader to read from the console? In my opinion you are using way too much code for just asking the user for input.
And also, as i can see you have in your for loop
Code java:
if (result < 25) {
System.out.print(result + 1);
So it will print the
same int for every character of the
String the user inputs if
result is less than 25.
Hope i explained it right.
I'm entering in a txt file to convert. I thought this statement was supposed to sort the array, then search the binary number for the character. Thus I then did the if (result < 25), meaning if the value of the character was less than 25 in the array, it would print out the character and increment it by 1.
Code :
Arrays.sort(Alphabet);
int result = Arrays.binarySearch(Alphabet, value);
Re: Char Array Increment + 1
Add some println statements to print out the results of the methods you are calling so you can see what they are doing.
Re: Char Array Increment + 1
Quote:
Originally Posted by
Norm
Add some println statements to print out the results of the methods you are calling so you can see what they are doing.
All it prints out is -1-1-1 for "ABC".
Re: Char Array Increment + 1
Why is the value always -1? Where is the value set?
Which print statement prints that? I see 3 different places print is called. Add an ID String to each so you can see which one is printing. For example: System.out.print("val=" + value);
What about the values of the other variables? What are they? print them out so you can see.
Re: Char Array Increment + 1
A note on your approach - it is redundant to use an array to get the next index of that array if you wish to increment a character value. characters can be cast to integers, the value of which is the ascii decimal value for that character - and those decimal values are in order with the alphabet. In other words:
Code :
char c = 'c';
char d = (char)(((int)c) + 1);//equals 'd'
No need to search an array for each character, let alone create that array in the first place. Search the internet for an ascii table to see the full range of decimal values
Re: Char Array Increment + 1
Code :
if (result < 25) {
System.out.println("val" + result);
Output: val-1
val-1
val-1
Code :
if (result == 25) {
System.out.print(Alphabet[0]);
Output: gives an exception on line 22.
The problem is it's not searching any binary values in the input. If it did, then the first code would print out every letter in the alphabet array from A to Y.
Re: Char Array Increment + 1
Why is result -1?
The ID string should be the name of the variable:
System.out.println("result=" + result);
Re: Char Array Increment + 1
Quote:
Originally Posted by
copeg
A note on your approach - it is redundant to use an array to get the next index of that array if you wish to increment a character value. characters can be cast to integers, the value of which is the ascii decimal value for that character - and those decimal values are in order with the alphabet. In other words:
Code :
char c = 'c';
char d = (char)(((int)c) + 1);//equals 'd'
No need to search an array for each character, let alone create that array in the first place. Search the internet for an ascii table to see the full range of decimal values
So i need to create that code for every letter in my array from A to Z? I want this program to input any string of letters, like "BALL" then increment each one of those characters to the next letter in the alphabet. So "BALL" would output "CBII".
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
So i need to create that code for every letter in my array from A to Z? I want this program to input any string of letters, like "BALL" then increment each one of those characters to the next letter in the alphabet. So "BALL" would output "CBII".
You do not need to create any array. Just use the ascii int values: get the character of interest in the string, cast it to an int, increment it's value, cast back to a char.
Re: Char Array Increment + 1
Code :
int result = Arrays.binarySearch(Alphabet, value);
This code searches for a character in the Alphabet array, then assigns it a value correct? If not, can someone please explain to me what binarySearch does.
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
Code :
int result = Arrays.binarySearch(Alphabet, value);
This code searches for a character in the Alphabet array, then assigns it a value correct? If not, can someone please explain to me what binarySearch does.
Take a look at here
Re: Char Array Increment + 1
What is the contents of the value variable you are searching for?
Use println to see.
Re: Char Array Increment + 1
Quote:
Originally Posted by
andreas90
So why isn't it only returning -1 as the index for my system.out.print(result).
Re: Char Array Increment + 1
Quote:
Originally Posted by
Norm
What is the contents of the value variable you are searching for?
Use println to see.
My value variable is set to equal 0. That's all I've done with it. It gives an error when I compile without the = 0.
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
So why isn't it only returning -1 as the index for my system.out.print(result).
What does it do instead?
Re: Char Array Increment + 1
Quote:
Originally Posted by
andreas90
What does it do instead?
Well, if I do system.out.print(result + 1); it will print out 0 instead of -1.
Re: Char Array Increment + 1
What is the purpose of search an array of char for the value of 0?
There is no 0 in the array???
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
Well, if I do system.out.print(result + 1); it will print out 0 instead of -1.
result should be -1 as you mentioned in post #16. result +1 = -1+1 = 0
Am i missing something?
Re: Char Array Increment + 1
This is getting frustrating. I modified char value = 0; and changed it to char value = 'C'. Now System.out.println(result); will output 2, which is the correct value in my char Alphabet array. How would I search every letter that has been entered through the program instead of just one? And, how would I convert that binary number into the letter that is in the char Alphabet array so it will output the letter instead of the number?
edit*, I forgot to add that my input is just "C".
Re: Char Array Increment + 1
Add 2 to it and it will print out 1.
Add 100 and you'll get 99.
Re: Char Array Increment + 1
Use the int value as an index into an array to get a char value.
Re: Char Array Increment + 1
Quote:
Originally Posted by
Nuggets
How would I search every letter that has been entered through the program instead of just one?
You do that with your for loop if i'm getting it right.
Quote:
Originally Posted by
Nuggets
And, how would I convert that binary number into the letter that is in the char Alphabet array so it will output the letter instead of the number?
edit*, I forgot to add that my input is just "C".
Check out the charAt(int) method of the String class. You can use it inside the for loop as value.