-3

I am learning Java and currently just try to use OOP in everything. I have created a Class which has two methods one returns String and another an Int. Both methods have scanners so the output is whatever user types in. In addition I have another method where I have main method and method where previous two are executed. Whenever I try to call both methods the program crashes, however it works normally if I call only one of them. Any ideas will be helpful to me.

package uno;

import dos.SupportClass;

public class MainClass {

    public static void main(String[] args) {
        MainClass mainObj = new MainClass();
        
        mainObj.checkFunctino();
    }
    
    public void checkFunctino() {
        
        SupportClass obj = new SupportClass();
        
        System.out.println("test");
        
        String check = obj.returnString();
        System.out.println(check);
        
        int temp = obj.returnInteger();
        System.out.println(temp);
    }

}


package dos;

import java.util.Scanner;

public class SupportClass { 
    public String returnString() {
        Scanner scan = new Scanner(System.in);
        String response = scan.nextLine();
        scan.close();
        return response;
    }
    
    public int returnInteger() {
        Scanner scan = new Scanner(System.in);
        int response = scan.nextInt();
        scan.close();
        return response;
    }
}

console output

Pipetus
  • 1,068
  • 1
  • 11
  • 28
  • Have a read of [ask] for information on how to ask a question. You can also take the [tour] for more information about the site! – Henry Twist Apr 01 '21 at 20:14
  • 1
    No need to explain that code, but you need to show it as text, not as screenshot. Where do you call those methods? – luk2302 Apr 01 '21 at 20:14
  • Your scanner is probably trying to get an int when it needs a String and vice versa – LuckyBandit74 Apr 01 '21 at 20:16
  • @luk2302 I have updated my post could you look at it ? – Takoshi Makotse Apr 01 '21 at 20:34
  • Try not closing the scanner, it will close the input stream the scanner reads from and that means `System.in`, which might lead to all kinds of trouble. – Kathrin Geilmann Apr 01 '21 at 21:08
  • @KathrinGeilmann Thank you for the answer. As I mentioned in a comment under the answer, it works if I do not close the scanner. But, how does the closing the scanner in one method affect the scanner that is newly created in another method? – Takoshi Makotse Apr 01 '21 at 21:25
  • The reason you shouldn't close your scanners is that if you close System.in, you won't be able to reopen it again in your program. – NomadMaker Apr 02 '21 at 05:15

1 Answers1

0

You can have a method to check if the string is numeric and call either returnString or returnNumeric

public static boolean isNumeric(String string) {
    int intValue;
        
    if(string == null || string.equals("")) {
        return false;
    }
    
    try {
        intValue = Integer.parseInt(string);
        return true;
    } catch (NumberFormatException e) {
        //left blank
    }
    return false;
}

Thanks for the update, you got that error because your int temp is not being passed. If you get rid of the close(), that would work!

  • Thank you for the answer. could you please tell me why would I need to remove close(), it is unclear because in the integer scanning method I create a new Scanner. Also is it normal to not close() Scanner in the method? – Takoshi Makotse Apr 01 '21 at 21:04
  • The close() method of Reader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If any read or other similar operation is performed on the stream, after closing, it raises IOException Detail is in the link below: https://www.geeksforgeeks.org/reader-close-method-in-java-with-examples/ – Hon Huy Ngo Apr 03 '21 at 00:49