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

  • SEARCH
  • Home
  • 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 8951635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:42:33+00:00 2026-06-15T13:42:33+00:00

Im making a contact list, and my code compiles and runs, when you add

  • 0

Im making a contact list, and my code compiles and runs, when you add the contact, type or email or phone its supposed to read them to you, the problem I have here is when I add the contacts and press next or previous, first or last it doesnt read the Name Contact, but it reads everything else…does it have to do something with spacing ? here is what I have…thanks

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;



 public class ContactSystem extends JFrame{
//Specify the size of five string fields in the record
final static int NAME_SIZE = 32;
final static int TYPE_SIZE = 32;
final static int CITY_SIZE = 20;
final static int PHONE_SIZE = 15;
final static int EMAIL_SIZE = 15;
final static int RECORD_SIZE =
        (NAME_SIZE + TYPE_SIZE + CITY_SIZE + PHONE_SIZE + EMAIL_SIZE);

//access contact.dat using RandomAccessFile
private RandomAccessFile raf;

//Text Fields
private JTextField jbtName = new JTextField(NAME_SIZE);
private JTextField jbtType = new JTextField(TYPE_SIZE);
private JTextField jbtCity = new JTextField(CITY_SIZE);
private JTextField jbtPhone = new JTextField(PHONE_SIZE);
private JTextField jbtEmail = new JTextField(EMAIL_SIZE);

//Buttons
private JButton jbtAdd = new JButton("Add");
private JButton jbtFirst = new JButton("First");
private JButton jbtNext = new JButton("Next");
private JButton jbtPrevious = new JButton("Previous");
private JButton jbtLast = new JButton("Last");

public ContactSystem(){
    //open or create a random access file

    try {
        raf = new RandomAccessFile("contact.txt", "rw");
    }
    catch(IOException ex) {
        System.out.print("Error: " + ex);
        System.exit(0);
    }

    //Panel p1 for holding labels Name , Type, Email or phone
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3,1));
    p1.add(new JLabel("Name Contact"));
    p1.add(new JLabel("Email Address"));
    p1.add(new JLabel("Type of Contact"));

    //Panel jpPhone for holding Phone
    JPanel jpPhone = new JPanel();
    jpPhone.setLayout(new BorderLayout());
    jpPhone.add(new JLabel("Phone"), BorderLayout.WEST);
    jpPhone.add(jbtPhone, BorderLayout.CENTER);

    //Panel jpEmail for holding Phone
    JPanel jpEmail = new JPanel();
    jpEmail.setLayout(new BorderLayout());
    jpEmail.add(new JLabel("Phone"), BorderLayout.WEST);
    jpEmail.add(jbtEmail, BorderLayout.CENTER);

    // Panel p2 for holding jpPhone and jpEmail
    JPanel p2 = new JPanel();
    p2.setLayout(new BorderLayout());
    p2.add(jpPhone, BorderLayout.WEST);
    p2.add(jpPhone, BorderLayout.CENTER);

    JPanel p3 = new JPanel();
    p3.setLayout(new BorderLayout());
    p3.add(jbtCity, BorderLayout.CENTER);
    p3.add(p2, BorderLayout.EAST);

    //panel p4 for holding jtfName, jtfType, and p3
    JPanel p4 = new JPanel();
    p4.setLayout(new GridLayout(3,1));
    p4.add(jbtName);
    p4.add(jbtType);
    p4.add(p3);

    //place p1 and p4 into jpContact
    JPanel jpContact = new JPanel(new BorderLayout());
    jpContact.add(p1, BorderLayout.WEST);
    jpContact.add(p4, BorderLayout.CENTER);

    //set panel with line border
    jpContact.setBorder(new BevelBorder(BevelBorder.RAISED));

    //add buttons to a panel
    JPanel jpButton = new JPanel();
    jpButton.add(jbtAdd);
    jpButton.add(jbtFirst);
    jpButton.add(jbtNext);
    jpButton.add(jbtPrevious);
    jpButton.add(jbtLast);

    //add jpContact and jpButton to the frame

    add(jpContact, BorderLayout.CENTER);
    add(jpButton, BorderLayout.SOUTH);

    jbtAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            writeContact();

        }
    });
    jbtFirst.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                if (raf.length() > 0) readContact(0);
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
    });
    jbtNext.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            try {
                long currentPosition = raf.getFilePointer();
                if (currentPosition < raf.length())
                    readContact(currentPosition);

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

    jbtPrevious.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                long currentPosition = raf.getFilePointer();
                if(currentPosition - 2 * RECORD_SIZE > 0)
                    //why 2 * 2 * RECORD_SIZE? see the the follow-up remarks
                    readContact(currentPosition - 2 * 2 * RECORD_SIZE);
                else
                    readContact(0);

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

    jbtLast.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                long lastPosition = raf.length();
                if(lastPosition > 0)
                    //why 2 * RECORD_SIZE? see the follow up remarks
                    readContact(lastPosition - 2 * RECORD_SIZE);

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

    try {
        if (raf.length() > 0) readContact(0);

    }

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

    }


}

//write a record at the end of file

public void writeContact() {

    try {
        raf.seek(raf.length());
        FixedLengthStringIO.writeFixedLengthString(
                jbtName.getText(), NAME_SIZE, raf);
        FixedLengthStringIO.writeFixedLengthString(
                jbtType.getText(), TYPE_SIZE, raf);
        FixedLengthStringIO.writeFixedLengthString(
                jbtCity.getText(), CITY_SIZE, raf);
        FixedLengthStringIO.writeFixedLengthString(
                jbtPhone.getText(), PHONE_SIZE, raf);
        FixedLengthStringIO.writeFixedLengthString(
                jbtEmail.getText(), EMAIL_SIZE, raf);

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

//read a contact at the specific position
public void readContact(long position) throws IOException{
    raf.seek(position);
    String name = FixedLengthStringIO.readFixedLengthString(
            NAME_SIZE, raf);
    String type = FixedLengthStringIO.readFixedLengthString(
            TYPE_SIZE, raf);
    String city = FixedLengthStringIO.readFixedLengthString(
            CITY_SIZE, raf);
    String phone = FixedLengthStringIO.readFixedLengthString(
            PHONE_SIZE, raf);
    String email = FixedLengthStringIO.readFixedLengthString(
            EMAIL_SIZE, raf);

    jbtName.setText(name);
    jbtType.setText(type);
    jbtCity.setText(city);
    jbtName.setText(phone);
    jbtName.setText(email);

}



/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    ContactSystem frame = new ContactSystem();
    frame.pack();
    frame.setTitle("Contact System");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

}

fixedlengthstringIO class

  import java.io.*;


  public class FixedLengthStringIO {

//read fixed number of characters from datainput stream
public static String readFixedLengthString(int size,
        DataInput in) throws IOException {
    //declare an array of characters
    char[] chars = new char[size];

    //read fixed number of characters to the array
    for(int i = 0; i < size; i++)
        chars[i] = in.readChar();
    return new String (chars);
}

//write fixed number of characters to a dataoutput stream

public static void writeFixedLengthString(String s, int size,
        DataOutput out) throws IOException {
    char[] chars = new char[size];

    //fill an array of characters from the string
    s.getChars(0, Math.min(s.length(), size), chars, 0);

    //fill in blank characters in the rest of the array
    for (int i = Math.min(s.length(), size); i < chars.length; i++)
        chars[i] = ' ';

    //create and write a new string padded with blank characters
    out.writeChars(new String(chars));
}
}

here is when I type in contact name, type, email etc into the list

enter image description here

and when I press next or previous, or first or last, it doesnt read the name..

enter image description here

  • 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-15T13:42:34+00:00Added an answer on June 15, 2026 at 1:42 pm

    Glitch is in your readContact method, change this

     public void readContact(long position) throws IOException {
            raf.seek(position);
            String name = FixedLengthStringIO.readFixedLengthString(
                    NAME_SIZE, raf);
            String type = FixedLengthStringIO.readFixedLengthString(
                    TYPE_SIZE, raf);
            String city = FixedLengthStringIO.readFixedLengthString(
                    CITY_SIZE, raf);
            String phone = FixedLengthStringIO.readFixedLengthString(
                    PHONE_SIZE, raf);
            String email = FixedLengthStringIO.readFixedLengthString(
                    EMAIL_SIZE, raf);
    
            jbtName.setText(name);
            jbtType.setText(type);
            jbtCity.setText(city);
            jbtPhone.setText(phone);
            jbtEmail.setText(email);
    
        }
    

    Also instead of adding separate ActionListner command button you can implement like

    public class ContactSystem extends JFrame implements ActionListener 
    {
      .....
      jbtAdd.addActionListener(this);
      jbtFirst.addActionListener(this);
      .....
    
      @Override
      public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if(actionCommand.equals("Add")) {
            //do add stuff
        } else if (actionCommand.equals("First")) {
            // move first
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making an Android app, and need to call the phone's contact list. I
I'm making a list that display Phone Contact. It works but does not looks
Hi i'm making an app which need to get user's gmail account contact list
I need to add a contact form to a website I'm making, and found
I am making a contact form that will send an email, but I'm having
I have a list and by making use of datatable plugin I sort them
I am making contact list kind view in my application. As contact application there
On my contact list application i try to use autocompletetextview when making search of
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
I'm getting an exception with this code; InnerException: System.InvalidOperationException Message=The specified type was not

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.