开发者

Appending something to the result of scan.next() in Java

开发者 https://www.devze.com 2023-02-14 20:49 出处:网络
I have String add_data[] = new String[6]; then I do System.out.println(\"COMPANY NAME: \"); add_data[0] = scan.next();

I have

String add_data[] = new String[6];

then I do

System.out.println("COMPANY NAME: ");
add_data[0] = scan.next();

I need to append a character to what to get from scan.next() & put that into add_data[0]. Please guide me how to accom开发者_如何学编程plish this task.


Assuming scan refers to java.util.Scanner this can be accomplished easily:

add_data[0] = scan.next() + 'c';


Let's use a well-named function.

public String nextLine(Scanner aInputStream) {
    return (aInputStream.next() + 'x');
}

add_data[0] = nextLine(new Scanner());

Well free to tuck that Scanner parameter into the function as a local variable if it bothers you.

0

精彩评论

暂无评论...
验证码 换一张
取 消