2013-10-26 56 views
0

錯誤的值我無法弄清楚如何告訴用戶有當他們去尋找一個標題爲「沒有發現這樣的標題」。當我測試它,並鍵入從數據庫中標題它顯示了正確的信息:試圖告訴用戶他們進入在Java

Game Id: 2 
Title:  Goldeneye 007 
Rating: T 
Platform: Nintendo 64 
Developer: RockStar 

,但如果我在隨機信息輸入輸出看起來是這樣的:

Game Id: 0 
Title:  asdsdfdfg 
Rating: null 
Platform: null 
Developer: null 

我使用的是基本控制檯應用程序在Java與MySQL我有兩個層次。 我表示層:

private static Games SearchForGame() { 
     Logic aref = new Logic(); 
     Games g = new Games(); 
     @SuppressWarnings("resource") 
     Scanner scanline = new Scanner(System.in); 
     System.out.println("Please enter the name of the game you wish to find:"); 
     g.setTitle(scanline.nextLine()); 
     aref.SearchGame(g); 

      System.out.println(); 
      System.out.println("Game Id: " + g.getGameId()); 
      System.out.println("Title:  " + g.getTitle()); 
      System.out.println("Rating: " + g.getRating()); 
      System.out.println("Platform: " + g.getPlatform()); 
      System.out.println("Developer: " + g.getDeveloper()); 

     return g; 


    } 

和邏輯層

public Games SearchGame(Games g) { 

     try { 
      Class.forName(driver).newInstance(); 
      Connection conn = DriverManager.getConnection(url+dbName,userName,password); 
      String sql = "SELECT GameId,Title,Rating,Platform,Developer FROM games WHERE Title=?"; 
      java.sql.PreparedStatement statement = conn.prepareStatement(sql); 
      statement.setString(1, g.getTitle()); 
      ResultSet rs = statement.executeQuery(); 

      while(rs.next()){ 

      g.setGameId(rs.getInt("GameId"));   
      g.setTitle(rs.getString("Title")); 
      g.setRating(rs.getString("Rating")); 
      g.setPlatform(rs.getString("Platform")); 
      g.setDeveloper(rs.getString("Developer")); 
      } 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 
      return g; 
    } 

回答

0

使用的if聲明?

if(g.getRating() != null /*or g.getGameId() == 0 or many other things*/) { 
    System.out.println(); 
    System.out.println("Game Id: " + g.getGameId()); 
    System.out.println("Title:  " + g.getTitle()); 
    System.out.println("Rating: " + g.getRating()); 
    System.out.println("Platform: " + g.getPlatform()); 
    System.out.println("Developer: " + g.getDeveloper()); 
} else { 
    System.out.println(); 
    System.out.println("No such title found"); 
    //throw some sort of exception (and plan to catch it) so that you 
    //can get out of this method without returning g full of null values 
} 

return g; 
0

你可以用很多方式做到這一點,這裏將解釋一個。

在SearchGame方法使用isBeforeFirst()方法來檢查,如果您有任何數據。

if(!resultSet.isBeforeFirst()){ 
    return null; 
} 

而在你的SearchForGame()如果對象爲null,則顯示一條消息。

if(g != null) { 
    System.out.println(); 
    System.out.println("Game Id: " + g.getGameId()); 
    System.out.println("Title:  " + g.getTitle()); 
    System.out.println("Rating: " + g.getRating()); 
    System.out.println("Platform: " + g.getPlatform()); 
    System.out.println("Developer: " + g.getDeveloper()); 
} else { 
    System.out.println("No data found"); 
} 
0

檢查空值是一種不良的流量控制形式。您應該考慮布爾結果。

if(aref.SearchGame(g)) { 
    System.out.println(); 
    System.out.println("Game Id: " + g.getGameId()); 
    . . . 
else { 
    System.out.println("No such title found"); 
} 

然後在你的邏輯,只是這樣做:

public boolean SearchGame(Games g) { 

    boolean found = false; 
    try { 

    [your sql select here] 

    if (rs.next()) { 

     [access result set] 

     found = true; 
    } 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    return found; 
} 

但更好的方法是退回到遊戲的實例的列表,然後檢查是否該列表是空的,像這樣。

List<Game> SearchGames(String title) 

這是一個良好的堅實的API,你可以使用它像這樣:

List<Game> games = aref.SearchGames(title); 
if(games.size() > 0) { 
    Game g = games.get(0);  
    System.out.println(); 
    System.out.println("Game Id: " + g.getGameId()); 
    . . . 
else { 
    System.out.println("No such title found"); 
} 

這也可以讓你找到你想要的類似標題的多個遊戲。

相關問題