samedi 29 octobre 2016

why is my else statement not working java

package jdbc;

import java.sql.DriverManager;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.*;
import java.util.Scanner;  

public class driver2 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int points =0;
        Scanner ievads = new Scanner(System.in);

        while(ievads.nextLine() != null){
    System.out.println("What Country");
    String lietotIn = ievads.next();
    {
    try {   
        Class.forName("com.mysql.jdbc.Driver");  
        java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/countries","root",""); 
        java.sql.Statement quer = con.createStatement();
        String kverijs = "select name from countries where name = '" +lietotIn+ "';";
        ResultSet rezult = quer.executeQuery(kverijs);
       // System.out.println(rezult.next());


        while(rezult.next()){
            String lietotajvards = rezult.getString("name");

                   if(lietotajvards.equalsIgnoreCase(lietotIn))
                   {
                       System.out.println("Yup");
                       points+=5;
                       System.out.println(points);

                   }
                   else {
                       System.out.println("no");
                       points-=3;
                       System.out.println(points);
                   }
        }
        }

  catch(Exception ex) {
            System.out.println("Kluuda: " +ex); 
    }

}

    }
    }
}

In the code above, I am taking input from scanner and checking to see if my word is in my database. If so I print yup if not no. In my scenario, my else is being skipped. Why is it that my if statement works but when I put in a phony value it does not print no and decrease points?

Aucun commentaire:

Enregistrer un commentaire