Results 1 to 5 of 5
  1. #1
    cuellar14 is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default Help on Arrays...

    My project consists in an array that adds, finds, deletes lists all the contenst within the array.

    Here's the code for my menu but i don't know how to initiate the array or create the mthods to add, find or delete the contents...

    I also need to do it using two different classes

    Thank you



    import java.io.*;
    import java.text.*;
    import javax.swing.*;

    class CuellarProjectArray {

    public static void main(String[] args) throws IOException{

    String str, company;
    int choice, selection;
    boolean keepRunning = true;

    do {

    JOptionPane.showMessageDialog(null, "This program stores customer's information");
    str = JOptionPane.showInputDialog(null, "What do you like to do?\n" + "(to add enter 1)\n" + "(to find enter 2)\n" + "(to delete enter 3)");
    choice = Integer.parseInt(str);

    switch(choice) {

    case 1:
    company = JOptionPane.showInputDialog(null, "Enter customer information");
    break;

    case 2:
    company = JOptionPane.showInputDialog(null, "Enter customer information");
    break;

    case 3:
    company = JOptionPane.showInputDialog(null, "Enter customer information");
    break;

    default:
    JOptionPane.showMessageDialog(null, "Invalid choice");

    }

    selection = JOptionPane.showConfirmDialog(null, "Would you like to return to the main menu?", "Confirmation", JOptionPane.YES_NO_OPTION);

    keepRunning = (selection == JOptionPane.YES_OPTION);

    }while (keepRunning);
    }
    }

  2. #2
    anandguna is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    use the for loop and iterate the array values

    String a="arul";
    for (int i=0;i<arraystring.length();i++)
    {
    if(a==arraystring[i])
    { System.out.println("match found");

    }


    }

  3. #3
    cuellar14 is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default

    i don't understand how to use that... where would i put it?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,580
    Rep Power
    25

    Default

    how to initiate the array
    type[] name = new type[<size>]; // create an array
    name[i] = new type(); // add an element to array at index i


    You'll want to change the following to use a method for comparing the two strings in place of using ==
    Java Code:
     if(a==arraystring[i])
    Last edited by Norm; 07-25-2008 at 04:12 PM.

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hello anandguna
    Quote Originally Posted by anandguna View Post
    use the for loop and iterate the array values
    Java Code:
    String a="arul";
    for (int i = 0; i < arraystring.length(); i++)
        if ([COLOR="RoyalBlue"]a == arraystring[i][/COLOR])
            System.out.println("match found");
    As Norm pointed out, the equality in blue uses to == operator. In Java, if the == operator is used on two variables of object type, like a String for example, then the test will return true if the variables reference the same object. Otherwise false is obtained. Primitive types, like int byte boolean or double, are tested for equality by value, as they are not objects. To test if two strings are equal, use the boolean String.equals(String other) method. For example:
    Java Code:
    if ([COLOR="RoyalBlue"]a.equals(arraystring[i])[/COLOR]) {
        System.out.println("match found");
    }
    Please ask if you want me to explain more, anandguna. ;)
    Last edited by tim; 07-25-2008 at 08:18 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. need help with arrays
    By Jman in forum New To Java
    Replies: 17
    Last Post: 07-21-2008, 02:34 AM
  2. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 02:24 AM
  3. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 02:45 PM
  4. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 10:08 PM
  5. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •