Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8444093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:18:58+00:00 2026-06-10T09:18:58+00:00

I’m having trouble with my code. the logic of my code is, when it

  • 0

I’m having trouble with my code. the logic of my code is, when it doesn’t find the username I entered a confirmation box will appear, what really happen is when a entered username is found in a line
it doesn’t launched the confirmation box “that’s alright” but it continue searching throughout every line and initialize else {statement} btw, my else statement is the confirmation box.

a sample data.dat that my code writes

shawn | qwerty1234
cloe | password1234
jones | shittybrix

When I entered “shawn” it launches my if statement “that’s alright”
but it continues searching throughout the string and the entered Username “shawn” became not equal to cloe and jones, it launches the else statement giving me two confirmation box respectively.
I want to avoid that error but I don’t know how.
THE ERROR OCCURS IN REGISTER.JAVA

this is my code for login:

package login;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JOptionPane;

public class Login extends javax.swing.JFrame {

    public Login() {
        initComponents0();
    }

    @SuppressWarnings("unchecked")
    private void initComponents0() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        uname = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        login = new javax.swing.JButton();
        reset = new javax.swing.JButton();
        pwd = new javax.swing.JPasswordField();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Login Pane");
        jLabel2.setText("User Name:");
        jLabel3.setText("Password:");
        login.setText("Login");
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String un = uname.getText();
                try {
                    try (BufferedReader br = new BufferedReader(new Filereader("data.dat"))) {
                        String strLine;
                        while ((strLine = br.readLine()) != null) {
                            if (strLine.startsWith(un)) {
                                JOptionPane.showMessageDialog(null, "Hello: " + un, "Registration", JOptionPane.INFORMATION_MESSAGE);
                            } else {
                                int sel = JOptionPane.showConfirmDialog(null, "It seems that you haven't registered yet? \n Lunch Registration Pane?", "Admin", JOptionPane.INFORMATION_MESSAGE);
                                if (sel == JOptionPane.YES_OPTION) {
                                    java.awt.EventQueue.invokeLater(new Runnable() {
                                        @Override
                                        public void run() {
                                            new Register().setVisible(true);
                                        }
                                    });
                                }
                            }
                        }
                    }
                } catch (IOException | HeadlessException ez) {
                    JOptionPane.showMessageDialog(null, "A null file was created in order to \n avoid File Catch errors", "Admin", JOptionPane.INFORMATION_MESSAGE);
                    File file = new File("data.dat");
                    try {
                        try (FileWriter writer = new FileWriter(file, true)) {
                            String data0 = "null";
                            String data1 = "null";
                            writer.write(data0 + " | " + data1 + "\n");
                        }
                    } catch (IOException | HeadlessException z) {
                        JOptionPane.showMessageDialog(null, e);
                    }
                }
            }
        });
        reset.setText("Reset Field");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(jLabel1)
                .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(uname, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(pwd))))
                .addContainerGap(30, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(uname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(login)
                .addComponent(reset))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton login;
    private javax.swing.JPasswordField pwd;
    private javax.swing.JButton reset;
    private javax.swing.JTextField uname;
    // End of variables declaration
}

this it the registration code it launches when I clicked yes in the confirmation box

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 *
 * @author Jfetizanan
 */
class Register extends javax.swing.JFrame {

    /**
     * Creates new form GUIREG
     */
    public Register() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        unamereg = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        pwdreg = new javax.swing.JTextField();
        submit = new javax.swing.JButton();
        resetreg = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Registration Pane");
        jLabel2.setText("User Name:");
        jLabel3.setText("Password:");
        submit.setText("Submit");
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Runned");
                File file = new File("data.dat");
                try {
                    try (FileWriter writer = new FileWriter(file, true)) {
                        String data0 = unamereg.getText();
                        String data1 = pwdreg.getText();
                        writer.write(data0 + " | " + data1 + "\n");
                    }
                    JOptionPane.showMessageDialog(null, "Registered", "Registration", JOptionPane.INFORMATION_MESSAGE);
                } catch (IOException | HeadlessException z) {
                    JOptionPane.showMessageDialog(null, e);
                }
            }
        });
        resetreg.setText("Reset Field");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(jLabel1)
                .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(unamereg))
                .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pwdreg)
                .addGroup(layout.createSequentialGroup()
                .addComponent(submit, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(resetreg, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap(42, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(unamereg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(pwdreg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(submit)
                .addComponent(resetreg))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField pwdreg;
    private javax.swing.JButton resetreg;
    private javax.swing.JButton submit;
    private javax.swing.JTextField unamereg;
    // End of variables declaration
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:19:00+00:00Added an answer on June 10, 2026 at 9:19 am

    Managed to find the issue through that big mess. In your if statement that pops up saying hello ‘blah’, you need to make it break out of the while loop, and move the else statement. Copy whats in the else statement just out of the while loop (just because it doesnt match the first guy, doesnt mean that it wont match a later one, so dont yell at them til the end). so have it like this:

    BufferedReader br = new BufferedReader(
            new InputStreamReader(fstream));
    String strLine;
    boolean registered = false;
    while ((strLine = br.readLine()) != null) {
        if (strLine.startsWith(un)) {
            registered = true;
            break;
        }
    }
    if(registered) {
        //blah
    } else {
        //blah
    }
    

    Also, you should change Lunch registration to Launch registration 😉

    In response to your comment, it’s due to this line:

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    

    You have that in both of your frames, it says to stop the JVM as soon as you close it. You want this for your login frame (if they close the login window, stop the program), but not for the registration window. For that, you just want javax.swing.WindowConstants.DISPOSE_ON_CLOSE, which says it will dispose of the frame upon closing it (good cleanup). If all frames are disposed, the JVM will end (assuming no other threads are running that is), so as long as Login stays open, it shouldn’t exit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need a function that will clean a strings' special characters. I do NOT
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.