Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

strings

Posted on 2006-11-22
6
Medium Priority
?
201 Views
Last Modified: 2010-03-31
i have a String of characters.  what i want to do is output these charachters in groups of 5 i.e. qwert-yuiop. im not quite sure with how to construct the for loop
0
Comment
Question by:isarasoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17999769
Homework?
0
 
LVL 15

Expert Comment

by:lyonst
ID: 17999956
Store the string in an array.

The loop will read each character in the array and look something like this..

Use a for loop example if 20 characters.. this can of course be set to the number of characters in the array

for ( int counter = 1; counter <=20; Counter ++)

{

 value = array(counter);

 output += value;

if (counter % 5 == 0)

 output += "-";

}
0
 
LVL 92

Expert Comment

by:objects
ID: 18000207
you can get your string as an array using:

char[] array = s.toCharArray();

to access elements of the array use:

char c = array[index];

or alternately you can access characters in the string using:

char c = s.charAt(index);

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:isarasoo
ID: 18000521
hi this is what ive tried  and im not having much look with it:

char[] array = b.toCharArray(); //created a char array
            for ( int l = 1; l <=b.length(); l++)


            {
               char value = array[l];
                char output += value;
               if (l % 5 == 0)              

               output += "-";


            }
please tell me what im doing wrong,  im fairly new to programming

0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 18000539
output should be a string, and created outside your loop


String output = "";
char[] array = b.toCharArray(); //created a char array
            for ( int l = 1; l <=b.length(); l++)


            {
               char value = array[l-1];  // java array indexes start at 0
                output += value;
               if (l % 5 == 0)              

               output += "-";


            }
0
 
LVL 1

Author Comment

by:isarasoo
ID: 18000571
oh my god ive tried everything but that

thanx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question