Results 1 to 5 of 5
Thread: Help on Arrays...
- 07-25-2008, 01:39 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 2
- Rep Power
- 0
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);
}
}
- 07-25-2008, 01:53 PM #2
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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");
}
}
- 07-25-2008, 02:39 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 2
- Rep Power
- 0
i don't understand how to use that... where would i put it?
- 07-25-2008, 05:10 PM #4how to initiate the 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 05:12 PM.
- 07-25-2008, 09:16 PM #5
Hello anandguna
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"); }
Last edited by tim; 07-25-2008 at 09:18 PM.
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
Similar Threads
-
need help with arrays
By Jman in forum New To JavaReplies: 17Last Post: 07-21-2008, 03:34 AM -
Arrays
By bunbun in forum New To JavaReplies: 1Last Post: 04-09-2008, 03:24 AM -
new to arrays
By jimJohnson in forum New To JavaReplies: 1Last Post: 04-08-2008, 03:45 PM -
2D-Arrays
By kbyrne in forum New To JavaReplies: 1Last Post: 02-07-2008, 11:08 PM -
arrays help
By Warren in forum New To JavaReplies: 6Last Post: 11-23-2007, 08:23 PM
Bookmarks