Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 39

Thread: Char Array Increment + 1

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Char Array Increment + 1

    I want this program to search the user input for a character stored in an array, then increment the value of the character by 1. For example, if the input was "A", then increment that value by 1 so it would printout "B". My code keeps printing out "000" for an input of ABC. Why does it print out 000?
    Last edited by Nuggets; April 4th, 2012 at 10:00 AM. Reason: removed code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Char Array Increment + 1

    Please explain the logic of the steps your program is doing. Make a list in pseudo code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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
    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.

  4. #4
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by andreas90 View Post
    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
    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.
    Arrays.sort(Alphabet);
    			int result = Arrays.binarySearch(Alphabet, value);

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by Norm View Post
    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".

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default 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:

    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

  9. #9
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Char Array Increment + 1

    if (result < 25) {
    								System.out.println("val" + result);
    Output: val-1
    val-1
    val-1

    	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.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Char Array Increment + 1

    Why is result -1?

    The ID string should be the name of the variable:
    System.out.println("result=" + result);
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by copeg View Post
    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:

    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".

  12. #12
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Char Array Increment + 1

    Quote Originally Posted by Nuggets View Post
    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.

  13. #13
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Char Array Increment + 1

    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.

  14. #14
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by Nuggets View Post
    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

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Char Array Increment + 1

    What is the contents of the value variable you are searching for?
    Use println to see.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by andreas90 View Post
    So why isn't it only returning -1 as the index for my system.out.print(result).

  17. #17
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by Norm View Post
    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.

  18. #18
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by Nuggets View Post
    So why isn't it only returning -1 as the index for my system.out.print(result).
    What does it do instead?

  19. #19
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by andreas90 View Post
    What does it do instead?
    Well, if I do system.out.print(result + 1); it will print out 0 instead of -1.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default 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???
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by Nuggets View Post
    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?

  22. #22
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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".

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Char Array Increment + 1

    Add 2 to it and it will print out 1.
    Add 100 and you'll get 99.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Char Array Increment + 1

    Use the int value as an index into an array to get a char value.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Char Array Increment + 1

    Quote Originally Posted by Nuggets View Post
    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 View Post
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 12:01 PM
  2. [SOLVED] How To Increment Array Elements
    By Nuggets in forum Java Theory & Questions
    Replies: 15
    Last Post: April 1st, 2012, 12:10 PM
  3. Increment a date
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: September 30th, 2011, 05:57 AM
  4. [SOLVED] Increment Statements Difference
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: May 10th, 2011, 01:51 PM
  5. auto-increment of primary key
    By hundu in forum Enterprise JavaBeans
    Replies: 1
    Last Post: May 15th, 2010, 10:55 AM