Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.28 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package oosd2;
  7.  
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.util.Arrays;
  13. import java.util.Scanner;
  14.  
  15. /**
  16. *
  17. * @author Complab - 2
  18. */
  19. public class Login extends javax.swing.JFrame {
  20.  
  21.  
  22. /**
  23. * Creates new form Login
  24. */
  25. public Login() {
  26. initComponents();
  27. String Username = jTextField1.getText();
  28. String pass = jPasswordField1.getText();
  29. String fileName= "Members.csv";
  30. File file= new File(fileName);
  31. String[] values = new String[12];
  32. int Index = 0;
  33.  
  34. try{
  35. Scanner inputStream= new Scanner(file);
  36. while(inputStream.hasNextLine()){
  37. String data= inputStream.nextLine();
  38. String[] parts = data.split(",");
  39. values[Index] = parts[0];
  40. values[Index+1] = parts[1];
  41. Index = Index+2;
  42. }
  43. inputStream.close();
  44. }catch (FileNotFoundException e) {
  45. e.printStackTrace();
  46. }
  47.  
  48. boolean isExists = false;
  49. // iterate over array
  50. for (int i = 0; i < values.length; i++) {
  51. // get the value at current array index
  52. String arrayValue = values[i];
  53. // compare values
  54. if (Username.equals(arrayValue)) {
  55. isExists = true;
  56. // if value is found, terminate the loop
  57. break;
  58. }
  59. }
  60. if (isExists) {
  61. System.out.println("String is found in the array");
  62. } else {
  63. System.out.println("String is not found in the array");
  64. }
  65. }
  66.  
  67. /**
  68. * This method is called from within the constructor to initialize the form.
  69. * WARNING: Do NOT modify this code. The content of this method is always
  70. * regenerated by the Form Editor.
  71. */
  72. @SuppressWarnings("unchecked")
  73. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  74. private void initComponents() {
  75.  
  76. jButton1 = new javax.swing.JButton();
  77. jLabel1 = new javax.swing.JLabel();
  78. jLabel2 = new javax.swing.JLabel();
  79. jTextField1 = new javax.swing.JTextField();
  80. jPasswordField1 = new javax.swing.JPasswordField();
  81.  
  82. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  83.  
  84. jButton1.setText("Login");
  85. jButton1.addActionListener(new java.awt.event.ActionListener() {
  86. public void actionPerformed(java.awt.event.ActionEvent evt) {
  87. jButton1ActionPerformed(evt);
  88. }
  89. });
  90.  
  91. jLabel1.setText("Password");
  92.  
  93. jLabel2.setText("Username");
  94.  
  95. jTextField1.setText("Enter Username");
  96.  
  97. jPasswordField1.setText("jPasswordField1");
  98.  
  99. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  100. getContentPane().setLayout(layout);
  101. layout.setHorizontalGroup(
  102. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  103. .addGroup(layout.createSequentialGroup()
  104. .addGap(183, 183, 183)
  105. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  106. .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  107. .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE))
  108. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  109. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  110. .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
  111. .addComponent(jTextField1))
  112. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  113. .addGroup(layout.createSequentialGroup()
  114. .addGap(331, 331, 331)
  115. .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
  116. .addContainerGap(399, Short.MAX_VALUE))
  117. );
  118. layout.setVerticalGroup(
  119. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  120. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  121. .addContainerGap(210, Short.MAX_VALUE)
  122. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  123. .addComponent(jLabel2)
  124. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  125. .addGap(51, 51, 51)
  126. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  127. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
  128. .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  129. .addGap(32, 32, 32)
  130. .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
  131. .addGap(158, 158, 158))
  132. );
  133.  
  134. pack();
  135. }// </editor-fold>
  136.  
  137. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  138. // TODO add your handling code here:
  139. String Username = jTextField1.getText();
  140. String pass = jPasswordField1.getText();
  141. if(Username.equals("Anim") && pass.equals("123"))
  142. {
  143. JOptionPane.showMessageDialog(this,"Login Successful",
  144. "",JOptionPane.ERROR_MESSAGE);
  145. }
  146. else
  147. {
  148. JOptionPane.showMessageDialog(this,"Incorrect login or password",
  149. "Error",JOptionPane.ERROR_MESSAGE);
  150. }
  151. }
  152.  
  153. /**
  154. * @param args the command line arguments
  155. */
  156. public static void main(String args[]) {
  157. /* Set the Nimbus look and feel */
  158. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  159. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  160. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  161. */
  162. try {
  163. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  164. if ("Nimbus".equals(info.getName())) {
  165. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  166. break;
  167. }
  168. }
  169. } catch (ClassNotFoundException ex) {
  170. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  171. } catch (InstantiationException ex) {
  172. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  173. } catch (IllegalAccessException ex) {
  174. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  175. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  176. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  177. }
  178. //</editor-fold>
  179.  
  180. /* Create and display the form */
  181. java.awt.EventQueue.invokeLater(new Runnable() {
  182. public void run() {
  183. new Login().setVisible(true);
  184.  
  185. }
  186. });
  187. }
  188.  
  189. // Variables declaration - do not modify
  190. private javax.swing.JButton jButton1;
  191. private javax.swing.JLabel jLabel1;
  192. private javax.swing.JLabel jLabel2;
  193. private javax.swing.JPasswordField jPasswordField1;
  194. private javax.swing.JTextField jTextField1;
  195. // End of variables declaration
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement