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 2 of 2 FirstFirst 12
Results 26 to 39 of 39

Thread: Char Array Increment + 1

  1. #26
    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=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.

  2. #27
    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
    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?

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

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

    Default 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.
    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);
    						}
     
    					}

  5. #30
    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
    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.
    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.

  6. #31
    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
    Instead of printing result you can use result as an index in the Alphabet array and print the element in the specified index.
    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?

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Char Array Increment + 1

    its not working
    Please explain and post any error messages.
    What is printed? What is the value of i?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    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
    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.

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

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

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

    Default 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".
    	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]);
    					}
     
    					}

  11. #36
    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
    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".
    	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.

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

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

  13. #38
    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
    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.

  14. The Following 2 Users Say Thank You to andreas90 For This Useful Post:

    Norm (April 4th, 2012), Nuggets (April 4th, 2012)

  15. #39
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

Page 2 of 2 FirstFirst 12

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, 01: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