Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 26th, 2007, 09:37 PM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sick with a String

How should I code in java, in order to display 5 characters in 1 block and 10 blocks in 1 line after receiving string input from user such as:-

[u]Input</u>

ABCDEFGHIJKL MNOP QRSTUV XYZ AC DEFGHI JKLMNO PQRSTU VXY.....and bla...bla...


[u]Output</u>

ABCDE FGHIJ LMNOP QRSTU VXYZA CDEFG HIJKL MNOPQ RSTUV XYZAB
bla...bla...( base on input produce output, 5 characters in 1 blocks and 10 blocks in one line)

I really need advice.....Helppppppp !!!




 
Old September 28th, 2007, 12:04 PM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

There's lots of ways to do this. I've gone for one that's hopefully pretty easy to follow as it sounds like you're just getting started. Drop the forum a line of there's anything you don't understand.

Code:
public class string_split {
  public static void main(String argv[]) {
    // original string
    String original = "ABCDEFGHIJKL MNOP QRSTUV XYZ AC DEFGHI JKLMNO PQRSTU VXY AJHKDJSHJSHDKSJ DSJKDNSNDJSNUDNWINWU NIWDWNWSOMWSMWMXKMXMS LXMSKMXKLSMJFNDCDJKBC HDBDHBJCBDJBCHDB CBHDBCJBHSKDS CB DJCHDKBDJCB CCHDBCJH DHBDCHBSJCBDKS";
    // strip out spaces, replace them with null character
    String noSpaces = original.replace(' ', '\0');
    String[] groupsOfFive = new String[100]; //change for big values
    // loop through string feeding 5 character substrings 
    // into an array
    int j=0;
    int i;
    for (i=0;i<noSpaces.length()-5;i += 5) {
      groupsOfFive[j] = noSpaces.substring(i,i+5);
      j++;
    }

    // Print what we got. Linebreak every ten blocks
    for(i=0;i<j;i++) {
      if (i%10==0)
        System.out.println();
      System.out.print(groupsOfFive[i] + " ");
    }
  }
}
The output is:

Code:
charlie@charlie:~/src/java$ javac string_split.java
charlie@charlie:~/src/java$ java string_split

ABCDE FGHIJ KLMN OPQR STUV XYZA CDEF GHIJ KLMNO PQRS
TUVX YAJH KDJSH JSHDK SJDS JKDNS NDJSN UDNWI NWUN IWDWN
WSOMW SMWMX KMXMS LXMS KMXKL SMJFN DCDJK BCHD BDHBJ CBDJB
CHDB CBHDB CJBHS KDSC BDJC HDKBD JCBC CHDBC JHDH BDCHB
charlie@charlie:~/src/java$
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
 
Old October 8th, 2007, 07:47 PM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much Bro, Its great..., sorry for my late reply.






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find a string in another string in vb6 satish_k VB How-To 3 March 30th, 2007 12:17 PM
Casting String array to string Samatha ASP.NET 1.0 and 1.1 Professional 1 December 5th, 2006 07:46 AM
Help! My developer is off sick - quick question! tanya VBScript 0 May 8th, 2006 09:28 AM
syntax to find a string in a string cole SQL Server 2000 2 October 10th, 2005 06:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.