0

How do I fix this error?

java.util.NoSuchElementException: No line found.

I looked at other similar posts but couldn't figure out what to do with the solution. It looks (to me) like every sc.nextLine(); had a corresponding line with it.

Important Code is below Sorry, for the bad fromatting/Understanding. Its my first time here and I just started getting into programming.

package assignment2;
import java.util.Scanner;

public class StringToolsTester {

   public static void main(String[] args) {
       StringTools obj = new StringTools();
       
       
       //Method to get the reverse of the users string input
       Scanner s = new Scanner(System.in);
       System.out.print("reverse - Enter Any String: ");
       String z = s.nextLine();
       System.out.println("Reversed String: "+obj.reverse(z));
       s.close();
       
       //Method to get initials of the users string input
       Scanner a = new Scanner(System.in);
       System.out.print("initials - Enter Full Name: ");
       String initial = a.nextLine();
       System.out.println("Initials: "+obj.initials(initial));
       a.close();
      
       //Method to get the most frequent character from the users string input
       Scanner b = new Scanner(System.in);
       System.out.print("mostFrequent - Enter String: ");
       String most = b.nextLine();
       System.out.println("Most frequent character = "+obj.mostFrequent(most));
       b.close();
      
       //Method to get the binary equivalent of the users binary string input
       Scanner c = new Scanner(System.in);
       System.out.print("binaryToDecimal - Enter Binary: ");
       String str = c.nextLine();
       System.out.println(StringTools.binaryToDecimal(str));
       c.close();
  • Maybe look here [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/q/218384/8945198) Also, please learn about debugging, as it will help you so many times. – AP11 Jan 25 '21 at 19:00
  • I don't see the image of the code you mentioned. Which is actually good, since you should never post code as an image; copy it to your post as text and format it appropriately. –  Jan 25 '21 at 19:00
  • Welcome to Stack Overflow! Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. – GhostCat Jan 25 '21 at 19:00
  • 1
    Well, you shouldn't make more `Scanner` classes, if you know you will read the input again. – AP11 Jan 25 '21 at 19:10
  • @NathanPyke I was right, it's the different `Scanner` classes, initialize just one and work with only one, then it's going to run. – AP11 Jan 25 '21 at 19:18
  • 1
    Scanner.close() closes the underlying input stream (System.in). So you can't use the subsequent Scanner objects because System.in is already closed. – k314159 Feb 01 '21 at 14:55

0 Answers0