2017-06-23 16 views
0

私がやっているのは、特定のIDでログインすると、入力されたIDに基づいて学校名を取得するときです。たとえば、これは私のファイルにありますtesting.txt私は0114343にログインして、学校が "SOC"にラベルを変更すると "学校のコミュニケーション"であることを確認したいファイルからデータをプルするログインIDに基づいてファイルからjava

これはこれまでのコードですが動作しませんそれはSOCそのうちの私はログインしています。どんな助力もありがとうございます。

LoginClass

public class TestingProgram { 

JButton button1,button2; 
JLabel label; 
JTextField idField,passwdField; 
BufferedWriter toFile; 
BufferedReader validation; 
String valuesArray[]={}; 
String idTitle; 
int exists=0; 
public TestingProgram() 
{ 

    JFrame frame = new JFrame(); 
    JPanel pnl = new JPanel(); 

    //Buttons 
    button1 = new JButton("Add"); 
    button2 = new JButton("Login"); 

    //TextField 
    idField = new JTextField(15); 
    passwdField = new JTextField(15); 

    pnl.add(button1); 
    pnl.add(button2); 
    pnl.add(idField); 
    pnl.add(passwdField); 


    button2.addActionListener((ActionEvent e)->{ 
     Validation(); 
    }); 

    frame.add(pnl,BorderLayout.NORTH); 
    frame.setSize(500,500); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

} 

    public String getIdTitle() 
{ 
    idTitle = idField.getText(); 
    return idTitle; 
} 

public void Validation() 
{ 
try{ 
     validation = new BufferedReader(new FileReader("testing.txt")); 
     String values=null; 
     while((values = validation.readLine())!=null) 
      { 
       valuesArray = values.split(","); 
       if(idField.getText().equals(valuesArray[0]) && passwdField.getText().equals(valuesArray[1])) 
       { 
        exists = 1; 
        break; 
       }      
      }  
       if(exists==1) 
       { 
        SecondTest sec = new SecondTest(); 
        sec.second(); 
        sec.frame.setTitle(getIdTitle()); 
       } 
       else 
       { 
        System.out.println("Does not exsits in file"); 
       } 
     } 

     catch(FileNotFoundException ex){ 
      ex.printStackTrace(); 
     } 
     catch(IOException ex){ 
      ex.printStackTrace(); 
     }    

} 

public static void main(String[] args) 
{ 
    new TestingProgram(); 
} 

} 

第二のクラス

public class SecondTest 
{ 
JFrame frame; 
JButton button; 
JLabel label; 
JPanel panel; 
BufferedReader validation; 
String valuesArray[] = {}; 
String school; 
public void second() 
{ 
    frame = new JFrame(); 
    button = new JButton("Button"); 
    label = new JLabel(""); 
    panel = new JPanel(); 
    panel.add(button); 
    panel.add(label); 

    try{ 
     validation = new BufferedReader(new FileReader("testing.txt")); 
     String values=null; 
     while((values=validation.readLine())!=null) 
     { 
      valuesArray=values.split(","); 
      if(valuesArray[2].equals("SchoolofCommunication")) 
      { 
       school = "SOC"; 
       break; 
      } 
      else if(valuesArray[2].equals("SchoolofEngineering")) 
      { 
       school = "SOE"; 
       break; 
      }   
     } 
     if(school.equals("SOC")) 
      { 
       label.setText("SOC"); 
      } 
      else 
      { 
       label.setText("SOE"); 
      } 


    } 
     catch(FileNotFoundException ex){ 
      ex.printStackTrace(); 
     } 
     catch(IOException ex){ 
      ex.printStackTrace(); 
     }  

    frame.add(panel,BorderLayout.CENTER); 
    frame.setSize(450,450); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

答えて

0

second()は、IDコンテキストを持っていません。 whileループは単にファイルを読み込み、SOCはファイル内の最初のエントリとなり、すぐに破損します。

while((values=validation.readLine())!=null) { 
     valuesArray=values.split(","); 
     if(valuesArray[2].equals("SchoolofCommunication")) { 
      school = "SOC"; 
      break; 
     } 

     // this else branch is not reachable based on the contents of the file 
     else if(valuesArray[2].equals("SchoolofEngineering")) { 
      school = "SOE"; 
      break; 
     } 

    } 

ので、あなたはIDコンテキストを渡す必要があります。

second(String id) 

、ファイルに該当する行を見つけるために、whileループでそれを使用します。

while((values=validation.readLine())!=null) { 
     valuesArray=values.split(","); 
     if(valuesArray[0].equals(id) && valuesArray[2].equals("SchoolofCommunication")) { 
      school = "SOC"; 
      break; 
     } else if((valuesArray[0].equals(id) && valuesArray[2].equals("SchoolofEngineering")) { 
      school = "SOE"; 
      break; 
     } 
    } 
+0

ああ、これが解決私の問題は多くのアンドリューに感謝 –

関連する問題