Solved

strings

Posted on 2006-11-22
6
196 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
hibernate insert example 13 34
Running JavaFX on JDeveloper 12C 1 55
servlet and mdb, jms error 1 39
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

820 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