Need help sending user input to another method

Let me give a brief explanation. In July, I taught a 5-week course through a Java company. They covered basic things like console application, crud operations, mysql and n-tier architecture. Since the course ended, I did not use it much, because I returned to work, and other medical reasons surfaced .... blah blah.

I was told that the company had made a simple program to reflect what I learned. It turns out I saved very little.

I decided to make a program for video games. It will be used to watch your video games so you don’t have to look in your bookcase (or store your games as always.)

To interrupt the chase, I cannot force the user to enter data from one class to another. My AddGame Method In my presentation layer, it is supposed to send user input to the NewGame method at my logical level. The error I am getting is that the column header cannot be null.

Here is my AddGame method

    public static Games AddGame() {
    Games g = new Games();
    Logic aref = new Logic();
    Scanner scanline = new Scanner(System.in);
    System.out.println("Please enter the title of your game:");
    scanline.nextLine();
    System.out.println("Please enter the rating of your game:");
    scanline.nextLine();
    System.out.println("Please enter the platform for your game:");
    scanline.nextLine();
    System.out.println("Please thenter the developer of your game:");
    scanline.nextLine();
    aref.NewGame(g);
    return g;

}

here is my NewGame method

    public static void NewGame(Games g)
{
    try {
         Class.forName(driver).newInstance();
         Connection conn = DriverManager.getConnection(url+dbName,userName,password);
         PreparedStatement ps = (PreparedStatement) conn.prepareStatement("INSERT INTO games(Title,Rating,Platform,Developer) " +
                "VALUES(?,?,?,?)");
         ps.setString(1, g.getTitle());
         ps.setString(2, g.getRating());
         ps.setString(3, g.getPlatform());
         ps.setString(4, g.getDeveloper());
         ps.executeUpdate();
         conn.close();

         } catch (Exception e) {
         e.printStackTrace();
}
}

I have not learned how to use sleep mode and I only know how to make a console application. Everything I was looking for was either sleeping or a web application. (sorry if the code seems messy or crappy) Please any advice would be very helpful. Thanks in advance!

+4
source share
4 answers

assuming your game class has setters for instance variables,

System.out.println("Please enter the title of your game:");
g.setTitle(scanner.nextLine());

System.out.println("Please enter the rating of your game:");
g.setRating(scanner.nextLine());

System.out.println("Please enter the platform for your game:");
g.setPlatform(scanner.nextLine());

System.out.println("Please thenter the developer of your game:");
g.setDeveloper(scanner.nextLine());

nextLine , , Games .

+1

AddGame Games . , .

Games, , :

public static Games addGame() {

    Scanner scanline = new Scanner(System.in);
    System.out.println("Please enter the title of your game:");
    String title = scanline.nextLine();

    System.out.println("Please enter the rating of your game:");
    String rating = scanline.nextLine();

    System.out.println("Please enter the platform for your game:");
    String platform = scanline.nextLine();

    System.out.println("Please thenter the developer of your game:");
    String developer = scanline.nextLine();

    Games g = new Games(title, rating, platform, developer);
    aref.NewGame(g);
    return g;
}

: Java- camelCase, . AddGame newGame.

+1

, , :

Games g = new Games();
Scanner scanline = new Scanner(System.in);
System.out.println("Please enter the title of your game:");
scanline.nextLine();
System.out.println("Please enter the rating of your game:");
scanline.nextLine();
System.out.println("Please enter the platform for your game:");
scanline.nextLine();
System.out.println("Please thenter the developer of your game:");
scanline.nextLine();

, , , . , - :

Scanner scanline = new Scanner(System.in);
System.out.println("Please enter the title of your game:");
g.setTitle(scanline.nextLine());
...

Games. , , , Games , , null.

+1
source

You need to change your code below

You need to pass the values ​​from scanline.nextLine();to yours g.setTitle(), g.setRating(), g.setPlatform()andg.setDeveloper().

System.out.println("Please enter the title of your game:");
g.setTitle(scanline.nextLine());
System.out.println("Please enter the rating of your game:");
g.setRating(scanline.nextLine());
System.out.println("Please enter the platform for your game:");
g.setPlatform(scanline.nextLine());
System.out.println("Please thenter the developer of your game:");
g.setDeveloper(scanline.nextLine());
aref.NewGame(g);
0
source

All Articles