Rise LC
Rise LC

Reputation: 37

Convert Multiple User Inputs that are integers into string

I created a scanner class and I want to convert the user inputs that are integers into a string that prints them out like num1:num2:num3

int num1 = scan.nextInt();
int num2 = scan.nextInt();
int num3 = scan.nextInt();

I want to be able to make it so I can use the string in

System.out.println();

without having to do num1 + ":" num2 + ":" + num3 every time i want to use the string.

Upvotes: 0

Views: 4170

Answers (4)

ppeterka
ppeterka

Reputation: 20726

You can introduce a local variable:

String myString = num1 + ":" num2 + ":" + num3;

Then you can use it directly:

System.out.println(myString);

Also, this could be a more convenient way:

String myString = String.format("%d:%d:%d", num1, num2, num3);

String.format enables you to specify a format string ("%d:%d:%d" this time), so if you happen to want to change it later on, you can do it easily.

As @Pshemo noted, similar to how String.format works, there is the PrintStream.printf() method, available in this scenario as System.out.printf(String format, Object... args):

System.out.printf("%d:%d:%d", num1, num2, num3);

Another aspect to consider: would you happen to experience a change in the requirements (e.g.: not 3, but more, or even arbitrary number of input), you should consider using arrays (or anz of the appropriate Collections: List or Set implementations) for storing the inputs, and writing a function to provide the desired string out of that.

In that case, the Apache Commons StringUtils can come in handy: it has a nice polymorphic join method, offering to join just anbout anything:

//given an int[] named myArray:
String myString = StringUtils.join(myArray, ':');

Upvotes: 7

user2030471
user2030471

Reputation:

You can use a StringBuilder:

StringBuilder sb = new StringBuilder();
sb.append(num1);
sb.append(":");
sb.append(num2);
sb.append(":");
sb.append(num3);
System.out.println(sb.toString());

As the append methods return a reference to the same object, these calls can be chained:

StringBuilder sb = new StringBuilder();
sb.append(num1).append(":").append(num2).append(":").append(num3);
System.out.println(sb.toString());

Varieties of overloaded append methods give you power to append several other types to the StringBuilder.

Upvotes: 4

Lukino
Lukino

Reputation: 1455

Guava Guava from Google is something that could help you. Among other stuff it has this nice utility classes.

public void concatenate() {
    // Your user input, which could be maybe optimised as well
    int int1 = 10;
    int int2 = 20;
    int int3 = 30;
    int int4 = 40;

    System.out.println(
         Joiner.on(":") // delimiter that will be used
            .join(ImmutableSet.of(int1, int2, int3, int4)));  // Guava which creates temp list for you
}

Result:

10:20:30:40

Upvotes: 0

Reshurum
Reshurum

Reputation: 98

Get the string like you did before and store it inside a string:

String numbers = num1 + ":" + num2 + ":" + num3;

Then print it:

System.out.println(numbers);

Upvotes: 0

Related Questions