0

Im a beginner in java and starting to do test driven development. I have a pretty basic scenario i am stuck with. I want to pass a string of numbers from 1 to x. i.e if x is 3, it will return "1, 2, 3" or if x is 5, it will return "1, 2, 3, 4, 5".

I know that i need to use an array list and a for loop but am stuck with the syntax. Someone please help!

Thanks

4
  • 1
    A "string of numbers" or a "list of numbers"? Commented Oct 16, 2011 at 23:40
  • I don't see what this has to do with TDD. Also, if you're having difficulties with basic language syntax, then perhaps trying to apply TDD is putting the cart before the horse. Commented Oct 16, 2011 at 23:41
  • 1
    Why not post what you have right now. That will make it more educational for you. Commented Oct 16, 2011 at 23:43
  • No arrays needed. You're just looping from 1 to x. Commented Oct 18, 2011 at 0:48

4 Answers 4

2

Try the following code:

int x = 5;

StringBuilder sb = new StringBuilder(); //You need import java.util.StringBuilder

for (int i = 1; i <= 5; i++) {
    sb.append(i);
    if (i!=x) {
        sb.append(',');
    }
}

String result = sb.toString(); //Here will be "1,2,3,4,5"
Sign up to request clarification or add additional context in comments.

1 Comment

you have a typo in the for statement, change 1++ -> i++
1

Here's a start:

String output = "";
int x = 5;

for (int i=1; i<=5; i++)
{
    output += i + ", ";
}

System.out.println(output);
// prints the string "1, 2, 3, 4, 5, "

4 Comments

i want to return it rather than print it. I want to make this test pass
@MohammedAli I didn't say it was perfect, I said "it's a start." Part of a learning exercise should involve you writing some code, no? ☕
public void getNumbers1ThroughX; Numbers numbers = new Numbers; Assert.assertEquals("1,2,3,4,5", numbers.getNumbers1ThroughX(5));
@ Matt Ball, Thanks for your help. I have got that far but itss the syntax I'm strugglng with. If you can give me the whole code i need, i can perhaps learn by understanding it. Thanks :)
0
String s = "";
int x = 5;

for(int i = 1; i <= x; i++) s += i + (i!=x?", ":"");

System.out.println(s); // It prints "1, 2, 3, 4, 5"

Comments

0

If it is a requirement to use array lists you can use the "add" and "get" methods in ArrayList:

List<Integer> listOfIntegers = new ArrayList<Integer>();

int = 5;
String stringOfIntegers = "";

for (int i = 1; i <= 5; i++) {
    listOfIntegers.add(i);
}

for (int i = 0; i < 5; i++) {
   stringOfIntegers += listOfIntegers.get(i);
   stringOfIntegers += ",";
}

System.out.println(stringOfIntegers);

ADDED: [Returned from a method]

public String numberString(){
    List<Integer> listOfIntegers = new ArrayList<Integer>();

    int = 5;
    String stringOfIntegers = "";

    for (int i = 1; i <= 5; i++) {
        listOfIntegers.add(i);
    }

    for (int i = 0; i < 5; i++) {
        stringOfIntegers += listOfIntegers.get(i);
        stringOfIntegers += ",";
    }

    return stringOfIntegers;
}

[Returned from a method where you tell it what number to count to]

public String numberString(int maxNumber){
    List<Integer> listOfIntegers = new ArrayList<Integer>();

    String stringOfIntegers = "";

    for (int i = 1; i <= maxNumber; i++) {
        listOfIntegers.add(i);
    }

    for (int i = 0; i < 5; i++) {
        stringOfIntegers += listOfIntegers.get(i);
        stringOfIntegers += ",";
    }

    return stringOfIntegers;
}

So for instance if you pass in a value of "7" the returned string will be: "1,2,3,4,5,6,7". To "invoke" this method you can use something like:

public static void main(String[] args){
    MyClass myClass = new MyClass(); //This will be the name of the class you created

    String myNumberString = myClass.numberString(6);

    System.out.println("My number string is: " + myNumberString); //This will print: "1,2,3,4,5,6"
}

3 Comments

Thanks for that. Appreciate it! If i want to return a string e.g. "1,2,3,4,5" rather than print line how would i do that? Also any ways i can use an ArrayList here?
Sorry i just realised you have used an arraylist here
The "stringOfIntegers" variable will contain the string you are looking for i.e. "1,2,3,4,5". If you need to to be returned from a method try the code in the edit I added.

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.