Solved

strings

Posted on 2006-11-22
6
199 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

615 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