-2

私のパートナーと私はプロジェクトの別々の部分に取り組んできましたが、今はそれらをまとめたいと思っていますが、うまくいきません。彼は jOptionpane のコードの一部を書きましたが、完成したプロジェクトは jFrame を使用します。

コードのこの特定の部分はコンパイルされますが、jOptionpane バージョンで機能するファイルには何も書き込まれません。基本的に、テキスト フィールドから読み取り、.txt ファイルに書き込みます。UNIX ベースの OS を使用する場合は若干の違いがあることは承知していますが、Windows でも成功していません。このサイトの他の回答をエミュレートしようとしましたが、うまくいきません。ヒントや提案はありますか?

public void jTextField1ActionPerformed (java.awt.event.ActionEvent te)               
{             
    String user,usersPass;
    FileWriter fw;
    FileReader fr;
    BufferedWriter writer;
    BufferedReader reader;



    if (te.getSource() == jTextField1)

    {

        user = jTextField1.getText();

    }
        else if (te.getSource() == jTextField2)

            {

                usersPass = jTextField2.getText();

             }


                try
                   {
                       ArrayList<String> nameList = new ArrayList<String>(100);
                       ArrayList<String> passwords = new ArrayList<String>(100);



                       File userNames = new File("Username.txt");
                       Scanner users = new Scanner(userNames);
                       File userPass = new File("Passwords.txt");
                       Scanner pass = new Scanner(userPass);



                       // FileWriter fw;
                       //  FileReader fr;
                       fw = new FileWriter("Macintosh  Hd/Users/smooth4prez/Desktop/Project/Username.txt");
                       fr = new FileReader("Macintosh Hd/Users/smooth4prez/Desktop/Project/Passwords.txt");
                       writer = new BufferedWriter(fw);
                       reader = new BufferedReader(fr);
                   }
                   catch(FileNotFoundException E)
                       {
                           E.printStackTrace();
                       }
                               catch(IOException e)
                                   {
                               e.printStackTrace();
                                   }

}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
 {

        if (evt.getSource() == jButton1)

            {
                dispose ();

                new SignIn().setVisible(true);

            }

}

これが、再加工しようとしている jOptionpane バージョンです。

import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.*;

public class LoginTest implements Serializable

{
    Scanner keyboard = new Scanner(System.in);  


 public static void main(String[] args)throws FileNotFoundException, IOException, NotSerializableException
 {
    Username();
 }
 public static void Username()throws FileNotFoundException, IOException
 {
   File userNames = new File("Username.txt");
    Scanner users = new Scanner(userNames);
    File userPass = new File("Passwords.txt");
    Scanner pass = new Scanner(userPass);

    FileWriter fWriteUser = new FileWriter("Username.txt", true);
   PrintWriter outUser = new PrintWriter(fWriteUser);
    FileWriter fWritePass = new FileWriter("Passwords.txt", true);
   PrintWriter outPass = new PrintWriter(fWritePass);       

    ArrayList<String> nameList = new ArrayList<String>(100);
  ArrayList<String> passwords = new ArrayList<String>(100);

    int userCount = 0;

    while(users.hasNext())
    {
        String oldName = users.nextLine();
        nameList.add(oldName);
        userCount = userCount + 1;
    }
    while(pass.hasNext())
    {
        String oldPass = pass.nextLine();
        passwords.add(oldPass);
    }

    String strNumUsers = "";

    strNumUsers = JOptionPane.showInputDialog("How many users would you like to register?");

    int numUsers = Integer.parseInt(strNumUsers);

    int newCount = userCount + 1;

   for(int j = 0;j < numUsers;j++)
    {

        String username = JOptionPane.showInputDialog("Enter the username");

        for(int i = 0;i < nameList.size();i++)
        {
            if(username.equals(nameList.get(i)))
            {
                JOptionPane.showMessageDialog(null, "That username is already taken");
                username = JOptionPane.showInputDialog("Enter the username");

                i = 0;
            }
        }

        nameList.add(username);              

        String password = JOptionPane.showInputDialog("Enter password");
       passwords.add(password);

        File dir = new File("Inbox" + newCount); 
        dir.mkdir();
        File folder = new File("Inbox" + newCount);

        File m;
        m = new File("Macintosh Hd/Users/smooth4prez/Desktop/Project3/Inbox" + newCount + "/messages.txt");
        m.createNewFile();

        File n;
        n = new File("Macintosh Hd/Users/smooth4prez/Desktop/Project3/Inbox" + newCount + "/outbox.txt");
        n.createNewFile();

        newCount = newCount + 1;
   }

    for(int k = userCount;k < nameList.size();k++)
    {
        outUser.println(nameList.get(k));
        outPass.println(passwords.get(k));
    }
    outUser.close();
    outPass.close();

    String signInUserName = JOptionPane.showInputDialog("Enter username to sign in");

    for(int i = 0;i < nameList.size();i++)
    {
        if(signInUserName.equals(nameList.get(i)))
        {

            String signInPassword = JOptionPane.showInputDialog("Please enter your password");
            if(signInPassword.equals(passwords.get(i)))
            {
                String response = "";

                JOptionPane.showMessageDialog(null, "Signed in!");
                i = nameList.size();

                response = JOptionPane.showInputDialog("Would you like to send a message, view messages, or exit?" + 
                "(Please enter send, view, or exit.)");

                /*switch(response.trim())
                {
                case "Send":
                case "send":
                    Messages.MessageTest(nameList, signInUserName);
                    break;
                case "View":
                case "view":
                    Messages.MessageView(nameList, signInUserName);
                    break;
                case "Exit":
                case "exit":
                    break;
                case "":
                    JOptionPane.showMessageDialog(null, "You didn't enter anything, DIPSHIT!");
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "That is not a proper response.");
                    break;
                }*/
                /*
                Save s = new Save();
                testObject Rob = new testObject();
                s.saveMyObject("myfile", Rob);

                //Saving an object with class Save.java
                /*
                Save s = new Save();
                Object myObject = new Object();
                String test = "test";
                myObject = (Object)test;
                s.saveMyObject("myfile", myObject);
                */


                //insert program to search for specific user homepage file
                //if found, load that homepage, if not found, call method to create homepage
                //and save it as a file with the title including the username
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Incorrect password");
                i = nameList.size();
            }
        }
        else if(!signInUserName.equals(nameList.get(i)))
        {
            if(i + 1 == nameList.size())
            {
            JOptionPane.showMessageDialog(null, "That is not a valid username");
            }       
        }           
    }
} 
}
4

1 に答える 1