0

In this program, string variable fd doesn't wait taking input. Can someone help me with this program. i can get the input if i use the new scanner object though.

import java.util.Scanner;
public class Strmethod {
    public static void main(String[] args)
    {
        String ch,fd;
        int s,e;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter A String:");
        ch=sc.nextLine();
        System.out.println("String is "+ch);
        System.out.println("Enter Two Numbers For Substring:");
        s=sc.nextInt();
        e=sc.nextInt();
        System.out.println("Substring:"+ch.substring(s,e));
        System.out.println("Enter a Word to search:");
        fd=sc.nextLine();
        System.out.println(ch.contains(f));
        String jn=String.join("/","hello","g","u","y","s");
        System.out.println(jn);
        System.out.println(ch.startsWith("H"));
        System.out.println(ch.startsWith("e"));
        System.out.println("Length:"+ch.length());
        String newstr=ch.replace("Hello","Hey");
        System.out.println("String is "+ch);
            
    }
}

Output Attachment

RAGAVAN M
  • 19
  • 2
  • 4
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – maloomeister Oct 27 '21 at 07:04

1 Answers1

0

Buffer for Scanner

The above picture shows the input in the form of a buffer your program is using.

Explanation: When you first use the nextLine() function it reads the whole buffer up to \n character. Now the next buffer line starts and you have used nextInt() to read an integer 3 again nextInt() to read 6 now you have pressed enter i.e. \n character that is appended to the last of the buffer.

The current position of the buffer is that already contains the \n character so when you use the nextLine() method it consumes the \n character from the buffer and takes no input from the console.

Solution: The solution is to use the nextLine() whenever using the last nextInt() or similar functions that don't read the full line.

The final program would be

        ...
        System.out.println("Enter Two Numbers For Substring:");
        s=sc.nextInt();
        e=sc.nextInt();
        sc.nextLine(); // <--- to read the last \n character from the buffer
        System.out.println("Substring:" + ch.substring(s,e));
        System.out.println("Enter a Word to search:");
        fd=sc.nextLine();
        ...
Asad Shakeel
  • 1,949
  • 1
  • 23
  • 29
  • 2
    The question has a clear duplicate (linked above). Please don't answer questions that are duplicates. If you want to add something, answer on the linked question please. – Federico klez Culloca Oct 27 '21 at 07:48
  • so should I delete this now? – Asad Shakeel Oct 27 '21 at 08:25
  • Yes, it would be better. If you don't want to lose the time it took you maybe post it on the other answer, but I'm not sure it adds much to what's already in there. Too bad for the pretty graphics, though :( – Federico klez Culloca Oct 27 '21 at 08:51
  • Actually, I know it was a duplicate but the questioner is a student so I answered according to his inputs so he would better understand the logic behind it with the help of graphics :) BTW, thanks for your input and I'll keep that in mind for future Q/A. – Asad Shakeel Oct 27 '21 at 09:21