Question

Write a static method called printWithSpaces that takes a String as its parameter and prints the...

Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example:

> Methods.printWithSpaces("method")
m e t h o d

You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically

public static void printVertical(String s) {

for (int i = 0; i < s.length(); i++) {

char c = s.charAt(i);

System.out.println(c);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Methods.java
public class Methods {
    public static void printWithSpaces(String s){
        for(int i = 0;i<s.length();i++){
            System.out.print(s.charAt(i)+" ");
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a static method called printWithSpaces that takes a String as its parameter and prints the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a static method called encodeString that takes a string as input and prints to the...

    Write a static method called encodeString that takes a string as input and prints to the console a word where each letter is advanced by one value. In addition, your method should return the encoded String. You may make use of the method provided below to help you. public static char encode(char x) { int current = x; current++; return (char)current; } This method takes a character and returns an "encoded" character. In other words, encode('a') will return 'b'. Your...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • JAVA Write a method that accepts a String as an argument. The method should use recursion...

    JAVA Write a method that accepts a String as an argument. The method should use recursion to display each individual character in the String. Then, modify the method you just wrote so it displays the String backwards. The following code does not correctly display the String backwards. It merely moves the first character of the String to the end: public static void displayCharacter(String s)    {        if(s.length() == 0)            return;        else       ...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus";...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println ()

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • write a static method named greet that takes a String that represents a name and prints...

    write a static method named greet that takes a String that represents a name and prints a personalized greeting to StdDraw. So far i have in the main method... 4 public class TakeHomeAssignment 5 { 6 public static void main(String[] args) 7 { 8 StdDraw.setScale(0,500); 9 //drawEye(250,250,50); 10 String greet(Aaron); 11 // drawStickFigure(250,250); and for the method; //public static String greet( name) 31 //{ 32 33 34 //StdDraw.text(250,250,"Hello" +name); 35 36 // } Im getting errors saying its an illegal...

  • Hi, I can't figure out how to make spaces between the characters of "Davy Crockett." I...

    Hi, I can't figure out how to make spaces between the characters of "Davy Crockett." I can get the output as "Davy Crockett" but not with the added spaces. Could you give me some suggestions and feedback please? Thank you. public class Practice3 { public static void main (String [] args) { System.out.println(pentagonArea(3.14)); System.out.println("Davy Crockett"); } public static double pentagonArea(double a) { return (Math.sqrt(5*(5 + 2 * Math.sqrt(a)) ) * a *a)/4; } public static String addSpaces(String s) { String...

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

  • WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search,...

    WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search, and prints the position of each occurrence of the search value in source. For example: Input: source = "String Programming in Java is fun", search = 'i' Console Output: 3 15 19 27 Template provided from question:    public static int printIndices(String source, char search)    {    }    public static void main(String[] args)    {        System.out.println("Expected printIndices() prints indices 3...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT