Question

I'm doing a Java project at the moment and feel that StringBuffer would help a lot in performing this particular task, where I am 'building' a string representation of a Matrix:

/**
 * Returns a String representation of the Matrix using the getIJ getter
 * function. You should use MaF.dF to format the double numbers to a
 * sensible number of decimal places and place the numbers in columns.
 * 
 * @return A String representation of the Matrix
 */
public String toString() {
    //String stringBuilder = new String
    StringBuffer beep = new StringBuffer;
    for(i=0; i<m-1; i++){
        for(j=0; j<n-1; j++){

        }
    // You need to fill in this method
}

I've tried it in a Java program by itself and it seemed okay, but then I tried some sample Java program from online and it didn't compile. Is StringBuffer built into Java, or would it require importing a package? I am not allowed to import any packages for this project.

Also, if anyone could tell me what exactly MaF.dF is? How it is used? I think it's part of MaInput which I cannot find anything on online.

Was it helpful?

Solution

StringBuffer is built into Java and you do not need to import any package. (Its package is java.lang which is "automatically" imported.)

See: http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html

Check the code below. It runs without any import.

public class StringBufferUse {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("a");
        sb.append("b");
        sb.append(123);
        System.out.println(sb.toString()); /* prints "ab123" */
    }
}

OTHER TIPS

Use StringBuilder instead of StringBuffer. It is the recommended way to build Strings.

You can find the classes of the JDK in the Javadoc of the JDK:

http://docs.oracle.com/javase/7/docs/api/

You don't have to explicitly import classes from java.lang. For other classes you need to define the import statement or use the fully quallified name (e.g. java.util.List instead of simply List).

This is what Javadoc is for.

Just make sure your documentation matches the version of Java you're using. This is for Java 7, if you're using Java 1.6 or Java 1.5, make sure you're looking at the correct documentation.

Checking Javadoc for 1.7, and yes, StringBuffer is there.

Is StringBuffer built into Java, or would it require importing a Package?

StringBuffer is under java.lang. When in doubt, always check the extensive java documentation resources found in the internet.

Also, if anyone could tell me what exactly MaD.dF is/ how it is used [I think it's part of MaInput which I cannot find anything on online] that would be very helpful.

I think the comment is referring to an object MaD.dF or something. It would be useful if you posted some other parts of the code or a link to where you got the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top