0

could you help me resolve my problem ?

I wrote this simple Java program:

import java.util.Scanner; public class Something { public static void main(String args[]) { Scanner s = new Scanner(System.in); char c1,c2; c1=s.findWithinHorizon(".", 0).charAt(0); c2=s.findWithinHorizon(".", 0).charAt(0); System.out.println(c1); System.out.println(c2); s.close(); } } 

When I press tf on my keyboard, the following prints on my console:

 t r 

What I am looking for is a single-line output, like

tr 

instead of

t r 

I tried System.out.println(c1, c2) but it is not working.

Thank you in advance.

4
  • 1
    System.out.println(c1+c2); Commented Apr 13, 2018 at 15:42
  • 4
    System.out.print. Commented Apr 13, 2018 at 15:42
  • 1
    @SouravSachdeva that wouldn't do what OP wants, because they are chars: it would promote them to ints and add them. Commented Apr 13, 2018 at 15:43
  • System.out.println(c1+c2); will return 230 ? Commented Apr 13, 2018 at 15:49

3 Answers 3

2

You should use the print() method from the System static class. Here is the Oracle Java documentation considering the PrintStream class, which is accessed through the System.out call (out is a PrintStream there).

Here is a working sample:

public class Something { public static void main(String args[]) { Scanner s = new Scanner(System.in); char c1,c2; c1 = s.findWithinHorizon(".", 0).charAt(0); c2=s.findWithinHorizon(".", 0).charAt(0); System.out.print(c1); System.out.print(c2); s.close(); } } 
Sign up to request clarification or add additional context in comments.

Comments

1

Use System.out.print not System.out.println

If you have multiple characters and want to print them in a single line then use the following logic.

for (int i = 0; i < s.length(); i++) { System.out.print(s.charAt(i)); } 

Comments

0

Use System.out.print(char c) instead of System.out.println(char c) the difference between print and println is that println introduce a break line at the end of each output. You also can do a concatenation System.out.println(char1+""+char2)

3 Comments

OP is printing chars, not Strings.
console gave me output 230
Try this System.out.println(char1+""+char2); It gave you that output because char also has a numeric representation called ASCII asciitable.com

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.