2
Scanner i=new Scanner(System.in);

System.out.println("Enter an integer: ");
int in=i.nextInt();

System.out.println("Enter an floating point number: ");
double d=i.nextDouble();

System.out.println("Enter a string: ");
String str=i.next();

System.out.printf("%s%n,Sum of%2d and %.2f is %.2f%n",str,in ,d,in+d);

My problem is with formatting the String I enter through Scanner. I was trying to enter "Result is", but printf() seems to see only the "Result" part of string, so what is the command for blank space? thx

Matt Ke
  • 2,612
  • 8
  • 21
  • 36
Yurodivi
  • 43
  • 1
  • 7
  • `i.nextLine(); //To avoid new line character..... then change this line ....String str=i.nextLine();` – Sanket Makani Feb 26 '17 at 16:42
  • i.next() gets the next word. you probably want nextLine right? – Patrick Parker Feb 26 '17 at 16:42
  • When you change `next()` to `nextLine()`, be aware of this: [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/q/13102045/5221149). – Andreas Feb 26 '17 at 16:45
  • @SanketMakani On the contrary; answers should be in answers, not comments. If it's a dupe then it should be marked as a dupe. Embedding answers in comments makes it harder for people who come in the future to actually see the answer. – Dave Newton Feb 26 '17 at 16:47
  • That way i will just skip Scanner entry and go right to printf(). Im wondering how to manipulate String via Scanner. So when "Enter a string :" part comes i can enter "Result is" and get in output "Result is – Yurodivi Feb 26 '17 at 16:58

5 Answers5

3

There are several possible solutions, but I believe the following will give you consistent behavior with the other inputs:

System.out.println("Enter an floating point number: ");
double d = i.nextDouble();
i.skip("((?<!\\R)\\s)*"); // skip whitespace, stopping after any newline

System.out.println("Enter a string: ");
String str = i.nextLine();

This approach would allow you to enter all the inputs on a single line, if so desired.

For example:

1 1.2 Result is

However if you really intend for your users to press Enter after every input, then it would be most consistent to read all the inputs with Scanner's nextLine() method, then parse as needed (using Integer.parseInt, etc).


Java 9

Due to a bug in Java 9, the atomic grouping (?> ... ) must be added around the linebreak matcher \R. See bug report JDK-8176983 for details.

i.skip("((?<!(?>\\R))\\s)*"); // skip whitespace, stopping after any newline
                              // Compatibility Note: Java 9 safe use of \R

This code will also work fine and not cause any problems if used for Java 8, so actually I recommend you use this workaround version in your code just to be on the safe side (e.g. if someone may copy/paste or set target to a different JDK).


Java 7 and earlier

The linebreak matcher \R is available in Java-8 or later. Prior to that version, you would have to use the "equivalent" pattern \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029] however to work as a true equivalent it actually must be wrapped in the atomic grouping (?> ... ). See documentation bug report JDK-8176029 for details.

i.skip("((?<!(?>\\u000D\\u000A|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029]))\\s)*"); // skip whitespace, stopping after any newline
Patrick Parker
  • 4,381
  • 3
  • 15
  • 43
  • For the sake of clarity, can you show a simpler pattern without the workaround for the Java 9 bug which is now fixed? – DodgyCodeException Nov 09 '18 at 11:21
  • 1
    @DodgyCodeException ok, I have gone with your suggestion and edited accordingly. However, I actually recommend you use the Java 9 workaround version in your code just to be on the safe side (e.g. if someone may copy/paste or set target to a different JDK). – Patrick Parker Nov 12 '18 at 03:28
0

My problem is with formatting the String I enter through Scanner. I was trying to enter "Result is", but printf() seems to see only the "Result" part of string

change this:

String str=i.next();

to this:

String str=i.nextLine();

next() will only return what comes before a space. On the other hand nextLine() returns the input including space between the words.

Ousmane D.
  • 50,173
  • 8
  • 66
  • 103
0

ok fine you can make a trick like that, just add new string ang print that

String str=i.next();
  String strNew=str;
naib khan
  • 544
  • 2
  • 15
  • That doesnt solve problem just skip to output , this part here is problem System.out.printf("%s%n,Sum of%2d and %.2f is %.2f%n",str,in ,d,in+d); That '%s' command formats string i should enter via Scanner class so problem is Scanner sees only entire string as i sad ,for instance via Scanner i enter "Result is" but output is only "Result".so how i solve that.How i manipulate String in Scanner so everything shows up. – Yurodivi Feb 26 '17 at 17:01
  • see the answer now @Yurodivi – naib khan Feb 26 '17 at 17:35
0

Even I encountered this problem few days ago where I had to input a new line right after I asked for a int or double. I solved it by using this -

int j=scan.nextInt();
double f=scan.nextDouble();
scan.skip("\n");
String str=scan.nextLine();

"scan is the name of my Scanner object".

See, here I used scan.skip("\n); because when you enter a number it consumes only that number and not the end of line. Therefore scan.skip("\n") skips that line which is still present in the buffer and hence, when we scan again it starts from a new line and we get the desired string. Hope it helps !

0

Use This

String[] str;
Scanner i=new Scanner(System.in);
System.out.print("Enter a string: ");
str=i.nextLine().split("");
System.out.println("Enter an integer: ");
int in=i.nextInt();
System.out.println("Enter an floating point number: ");
double d=i.nextDouble();

for(int k=0;k<str.length;k++){
    System.out.print(str[k]);
}
System.out.printf("Sum of%2d and %.2f is %.2f%n", in, d, in+d);

This clearly solve your answer,and if you want to write string like ("Hello Dude Coder") then this code gives you out put with all whitespace without any skip.

Matt Ke
  • 2,612
  • 8
  • 21
  • 36
  • Yes, but how does this compare to other answers, specifically the answer that's been accepted? – chb Mar 27 '19 at 19:41