Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

print all the content in a "node list" on the same line

I´m a beginner and I’m working with java currently, I need to write in a single line an equation like 5x^3 + 2x^4 + … using the content inside nodes.

Here is my class Node:

public class Node {
    private Termo element;
    private Node next;
    
    public Node(){
        element = null;
        next= null;
    }
    
    public Node( Termo element, Node next){
        this.element= element;
        this.next= next;
    }
    
     public void setnext( Node next){
        this.next= next;
    }
    
    public Node getnext(){
        return proximo;
    } 
    
     public void setElement( Termo element){
        this.element= element;
    }
    
    public Termo getElement(){
        return element;
    }
    
    @Override
    public String toString(){
        return "Termo: " + getElement().getCoef() + "x^" +getElement().getExp();
    }
}

My class Termo (which is an object with the coefficient and the exponent)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private double coef;
private int exp;

public Termo() {
    coef=0;
    exp=0;
}

public Termo(double coef, int exp) {
    this.coef = coef;
    this.exp = exp;
}

public double  getCoef() {
    return coef;
}

public void setCoef(int coef) {
    this.coef = coef;
}

public int getExp() {
    return exp;
}

public void setExp(int exp) {
    this.exp = exp;
}

public Object getObject(){
    Termo obj = new Termo(this.coef, this.exp);
    return obj;
}

@Override
public String toString() {
    return coef + " " + exp;
}

I created this method to print the content , but i want it in a single line:

public void escrevePolinomio (Node lista){
        if(lista != null){
            System.out.println(lista.getElement().getCoef()+"x^"+lista.getElement().getExp());
            lista=lista.getnext();
            escrevePolinomio(lista);
        }
    }

>Solution :

Use System.out.print() instead of System.out.println():

public void escrevePolinomio (Node lista){
    if(lista != null){
        System.out.print(lista.getElement().getCoef()+"x^"+lista.getElement().getExp());
        lista=lista.getnext();
        escrevePolinomio(lista);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading