1

I'm new to Java and I've been trying to turn this Array list of Terms (Letters and Numbers) into a String, (unless its 1, in which case the program should ignore the element).

Is this the proper way to go about accomplishing this or should I use a different method?

My code is as follows:

public String display() {

    for (int i = 0; i < terms.size(); i++) {

        char p = terms.get(i);

        if(Character.isDigit(p = 1)) {
            continue;
        } else if (Character.isDigit(p)) {
            return String.valueOf(p);
        } else {
            return Character.toString(p);
        }
    }

    p = display(); 
    return display();
} 

Note: I know this is wrong - I just need someone to point me in the right direction.

Input Example: {Term('C', 1),Term('O',2)} would give "CO2"

13
  • Where is terms defined? What is its specific type? Is it a List of generic type? If so, which type? Commented Apr 11, 2016 at 6:50
  • Also this should generate an endless recursion. Commented Apr 11, 2016 at 6:50
  • Can you show an input example and the expected output? Commented Apr 11, 2016 at 6:51
  • terms is an arraylist Commented Apr 11, 2016 at 6:52
  • 1
    @Alfred : please share the complete code and expected input and output Commented Apr 11, 2016 at 6:53

2 Answers 2

1

Assuming your Term is some custom class like below (based on the sample input you have mentioned):

public class Term{
    private String element;
    private int number;
    public String getElement(){
        return element;
    }
    public void setElement(char element){
        this.element = element;
    }
    public int getNumber(){
        return number;
    }
    public void setNumber(int number){
        this.number = number;
    }
}

Your concatenation method can be something like this:

public String display() {

    StringBuilder sb = new StringBuilder(50);
    for (int i = 0; i < terms.size(); i++) {
        Term p = terms.get(i);
        sb.append(p.getElement());
        if(p.getNumber()!=1) {
            sb.append(p.getNumber());
        } 
    }

    return sb.toString();
} 
Sign up to request clarification or add additional context in comments.

Comments

0

you can try this

import java.util.ArrayList;
import java.util.List;

public class TestSkack {
    public static void main(String[] args) {

        List<KeyValue> terms=new ArrayList<>();

        terms.add(new KeyValue('C', 1));
        terms.add(new KeyValue('O', 2));
        System.out.println(display(terms));

    }

    public static String display(List<KeyValue> terms){
        String result="";
        for(KeyValue keyValue:terms){
            if(keyValue.getValue()==1)
                result+=keyValue.getKey();
            else
                result+=String.valueOf(keyValue.getKey())+keyValue.getValue();
        }
        return result;
    }
}

class KeyValue{
    Character key;
    Integer value;


    public KeyValue(Character key, Integer value) {
        super();
        this.key = key;
        this.value = value;
    }
    public Character getKey() {
        return key;
    }
    public void setKey(Character key) {
        this.key = key;
    }
    public Integer getValue() {
        return value;
    }
    public void setValue(Integer value) {
        this.value = value;
    }


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.