Write a method that converts its String parameter so that letters are written in
blocks five letters long. For example, consider the following two versions of the
same sentence:

Plain: This is how we would ordinarily write a sentence.
Blocked : Thisi showw ewoul dordi naril ywrit easen tence

p.s....still got error in JCreator(JAVA).....dont know how to solve it.....

public class String {

    public static void main(String [] args) {

        String convertToFive;
        int string = new string;
        string = s;
            {
        StringBuffer result = new StringBuffer();
        for (int k = 0; k < s.length(); k++) {
            if ((k+1) % 5 != 0){
                result.append(s.charAt(k));
                }

            else if ((k + 1) % 5 == 0)
                result.append(s.charAt(k) + " ");
        }
        return result.toString();
    } // convertToFive()

    String s = "This is how we would ordinarily write a sentence.";
    String stripS = s.replaceAll(" ", "");

  }//end main

}//end class

Edited 3 Years Ago by happygeek: fixed formatting

Write a method that converts its String parameter so that letters are written in
blocks five letters long. For example, consider the following two versions of the
same sentence:

Plain: This is how we would ordinarily write a sentence.
Blocked : Thisi showw ewoul dordi naril ywrit easen tence

p.s....still got error in JCreator(JAVA).....dont know how to solve it.....

public class String {

public static void main(String [] args) {

String convertToFive;
int string = new string;
string = s;
{
StringBuffer result = new StringBuffer();
for (int k = 0; k < s.length(); k++) {
if ((k+1) % 5 != 0){
result.append(s.charAt(k));
}

else if ((k + 1) % 5 == 0)
result.append(s.charAt(k) + " ");
}
return result.toString();
} // convertToFive()

String s = "This is how we would ordinarily write a sentence.";
String stripS = s.replaceAll(" ", "");

}//end main

}//end class

One, code tags and formatting. Much easier to read:

[code=JAVA] // paste code here

[/code]

Two, elaborate on what the error is. Compile error? Run-time error? Under what circumstances? What line number? Etcetera. Saying you have an error isn't specific enough.

What does this mean?

int string = new string;
string = s;

Java is case sensitive, so if you mean "String", "string" isn't the same thing. Also, what is s ? You define s lower in the code as a String data member, but remember you are in main, which is static, so you can't use that class data members.

Be careful about how you name things. There is a Java String class, so you may want to consider renaming your class. Also, naming an integer string is pretty confusing.

[EDIT]
OK, I see s is defined in main so it's NOT a class variable, but it's defined below where you use it. You need to define it above where you first use it. Code tags will make those brackets line up so it's easier to see what goes with what.

I prefer to use

java.util.StringTokenizer

So you can see the example below. That works fine.

package mytoken;

import java.util.StringTokenizer;

public class MyTokenizer {
	
	private String myToken = "";
	
	/**
	 * 
	 * @param myString
	 */
	private void convertTokenizerToString(String myString){
		StringTokenizer tokenizer = new StringTokenizer(myString);
		while(tokenizer.hasMoreTokens()){
			myToken+= tokenizer.nextToken().toString() ;
		}
	}
	
	/**
	 * 
	 * @param xToken - value to group tokens - in this example it is 5
	 * @return
	 */
	private String splitIntoXToken(int xToken){
		int mod = 0;
		int runner = 0;
		try{
			mod= myToken.length() % xToken; // modulo value
			runner= (myToken.length()-mod)/xToken;
		}catch(ArithmeticException ex){
			System.err.println("\n cannot devide by zero \n");
			ex.printStackTrace();
		}
		
		
		int index = 0;
		String helpString= "";
		for(int i=0; i<runner; i++ ) {
			helpString+= myToken.substring(index, index+xToken) + " ";
			index+=xToken;
		}
		helpString +=myToken.substring(index, index+mod);
		
		return helpString;
	}
	
	public static void main(String[] args) {
		MyTokenizer myTokenizer = new MyTokenizer();
		String myString= "This is my String";
		
		myTokenizer.convertTokenizerToString(myString);
		System.out.println("My String before: "+myString);
		System.out.println("My String after it: "+myTokenizer.splitIntoXToken(5));
	
	}
	
}

Write a method that converts its String parameter so that letters are written in
blocks five letters long. For example, consider the following two versions of the
same sentence:

Plain: This is how we would ordinarily write a sentence.
Blocked : Thisi showw ewoul dordi naril ywrit easen tence

p.s....still got error in JCreator(JAVA).....dont know how to solve it.....

public class String {

public static void main(String [] args) {

String convertToFive;
int string = new string;
string = s;
{
StringBuffer result = new StringBuffer();
for (int k = 0; k < s.length(); k++) {
if ((k+1) % 5 != 0){
result.append(s.charAt(k));
}

else if ((k + 1) % 5 == 0)
result.append(s.charAt(k) + " ");
}
return result.toString();
} // convertToFive()

String s = "This is how we would ordinarily write a sentence.";
String stripS = s.replaceAll(" ", "");

}//end main

}//end class

a good hint for the rest of your career.. do not go and try to solve the questions of chapter 26 if you haven't understood (or read) the introduction to the language.

first of all ... there is already an Object named String, maybe you've heard of it?

or maybe the next line you produced

int string = new string;

do NOT try to tell me that your compiler handled that the way you wanted it to. further more, indeed, in Java, unlike in certain other languages, you can not use a variable before it is declared. tried to ask your teacher what the error your compiler gives you means??

This article has been dead for over six months. Start a new discussion instead.