Solved

strings

Posted on 2006-11-22
6
193 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
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
pairs challenge 5 44
PDFBox - convert image to PDF, PDF resolution 3 66
backtracking recursion  code 19 40
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now