0

I'm a bit new to java, and I am wondering what I did wrong in my code. The problem is at "System.out.println(pw.next());" This code is for a Pokemon like game, and the scanner is supposed to scan for the person's username. I am far from finished with the code, and the layout is a bit weird because I tried fixing the error myself.

It would also be highly appreciated if anyone has tips for creating a fun game.

package test;
import java.lang.Math;
import java.util.*;

public class Pokemon {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome to Daniel's Game");
        System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
        System.out.println("It is your job to save the world from catastrophe");
        System.out.println("Are you up for the Challenge?");
        String y = sc.next();
        if(y.equals("yes")||y.equals("Yes")) {
            System.out.println("You better be");
        } else {
            System.out.println("The world ends cuz you suck");
            System.exit(0);
        }
        sc.close();
        name();

    }
    public static void name() {
        System.out.println("What is Your Name?");
        Scanner pw = new Scanner(System.in);
        String o = pw.next();
        pw.close();
        Scanner ew = new Scanner(System.in);
        System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");

        int x = ew.nextInt();

        ew.close();
    }

}
Mihir Kekkar
  • 528
  • 3
  • 11
me Me
  • 3
  • 5

2 Answers2

1

Below should work :

package test;
import java.lang.Math;
import java.util.*;
public class Pokemon {

    private static Scanner sc;

    public static void main(String[] args) {
         sc = new Scanner(System.in);
        System.out.println("Welcome to Daniel's Game");
        System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
        System.out.println("It is your job to save the world from catastrophe");
        System.out.println("Are you up for the Challenge?");
        String y = sc.next();
        if(y.equals("yes")||y.equals("Yes")) {
            System.out.println("You better be");
        } else {
            System.out.println("The world ends cuz you suck");
            System.exit(0);
        }
       // sc.close();
        name();

    }
    public static void name() {
        System.out.println("What is Your Name?");
        String name = sc.next();
       // Scanner pw = new Scanner(System.in);
       // String o = pw.next();
      //  pw.close();
        //Scanner ew = new Scanner(System.in);
        System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");

        int x = sc.nextInt();

        sc.close();
    }

}
VN'sCorner
  • 1,532
  • 1
  • 9
  • 13
0

Maybe your errors happened because you use Scanners in a bad way, just one is enough and pass it as a parameter through the main class is a good custom because you can close it after the function is terminated.

Try this...

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome to Daniel's Game");
        System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
        System.out.println("It is your job to save the world from catastrophe");
        System.out.println("Are you up for the Challenge?");
        String y = sc.next();
        if (y.equals("yes") || y.equals("Yes")) {
            System.out.println("You better be");
        } else {
            System.out.println("The world ends cuz you suck");
            System.exit(0);
        }
        name(sc);
        sc.close();
}

public static void name(Scanner sc) {
        System.out.println("What is Your Name?");
        String o = sc.next();
        System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");
        int x = sc.nextInt();
}
Alberto Ursino
  • 366
  • 3
  • 18