Monday 15 July 2013

java - Remove comma from end of String, using While loop -



java - Remove comma from end of String, using While loop -

i comma on end of string after user inputs text. how rid of lastly comma? old fence post problem, i'm stuck on fence.

import java.util.scanner; public class fencepost { public static void main(string[] args) { system.out.print("enter line of text: "); scanner console = new scanner(system.in); string input = console.nextline(); system.out.print("you entered words: "); scanner linescanner = new scanner(input); while (linescanner.hasnext()) { system.out.print(linescanner.next()); system.out.print(", "); } } }

i output "hello, there," comma after there.

add if statement within loop determine if there's still next line, if so, add together comma, otherwise, don't add together one:

while (linescanner.hasnext()) { system.out.print(linescanner.next()); if (linescanner.hasnext()) { system.out.print(", "); } }

java string while-loop

No comments:

Post a Comment