Uhh, hello. I just want to get a sample line of codes that displays the given string, insert a new element and deletes an old element. So far, this what I have done:

import java.io.*;


public class EmpLists {
    String employees[] = {"A", "B", "C", "D", "E" };
}

class CallEmpLists {

    public static InputStreamReader r = new InputStreamReader (System.in);
    public static BufferedReader inp = new BufferedReader(r);

        public static void main (String args[]) throws Exception {
            int task;
            System.out.print ("Lists of Employees\n");
            System.out.print ("What do you want to do?:\n");
            System.out.print ("1 - Display list. \n2 - Insert New Name. \n3 - Delete an item. \n4 - Nothing." + "\n ");
            task = Integer.parseInt (inp.readLine()); // I got a problem with this here. It's supposed to be a command that reads a char input. But I don't know how to do it too. 

            if (task==1) {
                for(int k=0; k<5; k++)
                System.out.println (employees[]);
            }
    }
}

I really can't seem to understand some sites on the internet that explains how to display string elements. Please, I really need help. @_@

[line 18]: // I got a problem with this here. It's supposed to be a command that reads a char input. But I don't know how to do it too.

You can pick-off the first character in the String. readLine() returns a String, and the String class has a method charAt().
So you can do: inp.readLine().charAt(0).

The line System.out.println(employees[]); has invalid syntax: the brackets aren't allowed there unless you specify an index.
From context I'd say that you probably intended: employees[k].

Another option is to make use of the Arrays utility class to get a String representation of your array.
Here's an example of how you can do that: Arrays.toString(employees), as you can see: you don't have to write a loop.

Do you want/have to write your own list class that is backed by an array and provide insert and delete operations for it? If not, then use ArrayList.

Don't hardcode the length of your array:

// DON'T do this
for(int k = 0; k < 5; k++)
    System.out.println(employees[k]);

instead use the array's length field:

// DO this instead
for(int k = 0; k < employees.length; k++)
    System.out.println(employees[k]);

so do I have to type in another public-thingy? o.o
++ our instructor told us to use j2sdk 1.4.2_14...
I figured out that some of the package files like.. java.util.Scanner were not included in it.

Do you need to write your own simple list implementation as an assignment?

If so, then you'll need to work on your EmpList class, because as it stands now it is no better than using an array directly.
You should encapsulate your implementation and mark your employees array reference in EmpList as private.
Furthermore you should provide a public interface (don't confuse this with a Java interface) that contains methods for:

  • querying the size,
  • adding an element,
  • removeing an element,
  • getting an element by index

Think of something you could use like this:

// Create an empty list
EmpList employees = new EmpList();

// Adding elements
employees.add("John");
employees.add("Tom");
employees.add("Abbey");

// Removing elements
employees.remove("Tom");

// Iterating
for (int i = 0; i < employees.size(); i++)
    System.out.println("Employee #" + i + " = " + employees.get(i));

// Expected output:

// Employee #0 = John
// Employee #1 = Abbey

our instructor told us to use j2sdk 1.4.2_14

Why would you stick with something old like that?

I figured out that some of the package files like.. java.util.Scanner were not included in it.

That is because the Scanner class was new in Java 5.

Yes, it was just a simple list.

I was thinking of that...

But, it was really an instruction to use that j2sdk 1.4.2_14

so I'll replace the letters into numbers..

your post sir gave me the idea of what should I do.

But, it should be the user's input that will determine if he/she will delete or insert a new employee..

I was thinking of user if (userschoice==1).. then it will do like displaying the entire list.. else if (userschoice==2).. it'll delete a certain entry, "A" employee for example then it will be shown in the output. else if (userschoice==3).. it'll insert a certain entry, it should show in the output that there are a new name..

Is it possible to do? o.o

Plus, the title should be displaying, inserting and deleting lists.. sorry for that. xD

I really thought it was string. I'm just a newbie in Java.. o.o

Let me just clarify myself.. Even I, can't get what I really am saying here. xD

First of all, I'm just a student and I am currently using Java for programming.. And I'm a beginner in Java. We have an assignment that goes like this, our program should prompt the user if he/she would like to display the list of employees, insert a new name for the employee list, and delete a name existing on the list..

Right now, we are just using that j2sdk 1.4.2.. and I know it's old. Your replies, sir mvmaldren gave me the idea on what to do. But I still don't understand some parts.. like "private". My understanding on the assignment goes like this.. I should use the 1,2,3,4 for the user's choice on what he/she would do. Because, I will just use Integer.parseInt for the input. Then, I'll use if-else statement to justify the user's choice. Display, delete and insert elements on the lists is what I don't know.. Plus, I don't know if it would work under if-else statement.. I do have a background in C programming..

That's what I just want to know and what I don't know..

But I still don't understand some parts.. like "private".

Take a look here.
Basically the idea is that the employees array reference should not be exposed.
Compare the following two approaches:

1) Marking it public or default:

// Marking it public
public class EmpList
{
    public String[] employees; // accessible from everywhere

    // ...
}

// Marking it default
public class EmpList
{
    // default access is what you get by "default" when you don't
    // specify 'public', 'private' or 'protected'
    String[] employees; // not accessible from everywhere, but still
                        // accessible from everywhere in the package of
                        // your EmpList class.

    // ...
}

Somewhere else in your code (outside the EmpList class) it is now possible to write this:

EmpList list = new EmpList();
employees.add("John");
employees.add("Tom");
employees.add("Abbey");
list.employees = null; // this would be VERY BAD, after executing,
                       // the elements in EmpList are "gone".

2) Marking it private:

public class EmpList
{
    private String[] employees; // only accessible from inside EmpList

    // ...
}

Somewhere else in your code (outside the EmpList class) you might try writing this:

EmpList list = new EmpList();
employees.add("John");
employees.add("Tom");
employees.add("Abbey");
list.employees = null; // will cause a compiler error because
                       // the employees field is marked as 'private',
                       // none of the employees is gone.

Because you've marked the employees field as private this is not possible - which is a good thing.
Imagine the bugs and abuses [1] that could arise from not controlling access to it using the private access modifier and an appropriate public interface.

[1] an example is shown above, under Marking it public or default.

In general you should mark every field private unless you have a very good reason not to do so.

Because, I will just use Integer.parseInt for the input.
Then, I'll use if-else statement to justify the user's choice.
Plus, I don't know if it would work under if-else statement..

Sounds workable to me. Alternatively you could use a switch statement.
Try it and when you encounter problems post what you tried so that we can take a look and suggest a way to fix it.

what should goes first? private String employees[] or public static void main (String args[]) throws Exception?

This is what my code looks like now..

import java.io.*;
import java.util.*;


public class Trial1 {
    public static InputStreamReader r = new InputStreamReader (System.in);
    public static BufferedReader inp = new BufferedReader(r);

    public static void main (String args[]) throws Exception {
        ArrayList employees = new ArrayList();
        employees.add("A");
        employees.add("B");
        employees.add("C");
        employees.add("D");
        employees.add("E");
        int task;

        System.out.println ("List");
        System.out.println ("What do you want to do?:");
        System.out.println ("1 - Display list. \n2 - Insert New Name. \n3 - Delete an item. \n4 - Nothing." + "\n ");
        task = Integer.parseInt (inp.readLine());)

        if (task==1) {
            System.out.println ("Contents of Employees:" + employees);
        }


    }

}

what should goes first? private String employees[] or public static void main (String args[]) throws Exception?

Seems like you chose to use ArrayList after all. In that case you don't need to keep a separate array reference.
Also it doesn't matter which one goes first, whether you first put your fields, then your methods, or first the methods and then your fields, or put fields between methods. It's all valid.

alright. I'll try it now. :D

should I do adding elements in the array in the private or inside public?

EDIT: It displays now the string employees.. but I put the ArrayList inside the main. When I tried to do either private String[] employees; { or public String[] employees; { , I can't call it properly on the output. How can I call the whole list in the output? My line in the output goes like this:
System.out.println ("Contents of Employees:" + String.employees[]); well, I just tried that String.employees.
is Char.parseChar(inp.readLine()); a right line to get the char of user's input? I forgot that we should get the new input of the user which is a char, not a int.. >.<

is Char.parseChar(inp.readLine()); a right line to get the char of user's input?

Nope, in fact, it doesn't even exist. Read my first reply.

When I tried to do either private String[] employees; { or public String[] employees; { , I can't call it properly on the output. How can I call the whole list in the output?

Read my first reply (the part about the Arrays utility class).
Note that if you're using an ArrayList you don't need to keep a separate array reference.
You should decide which approach to go with, either you write your own list implementation, or you reuse an existing one (such as ArrayList) from a class library (such as Java's class library).

It might be a good idea to check with your instructor which classes from the Java API you are allowed to use.
(e.g. are you allowed to use java.util.ArrayList? java.util.Arrays?)

I understand things better with a sample.. >.<

Can I have sample codes? If you really don't want to give it, maybe just a hint.

Other than that, since I can display properly the string elements..
How can I insert an new element and delete an existing element?

Just a sample, would do. @_@

I have the version 6 now.. But I can't seem to install it properly. >.<
I don't know how to select it when I'm about to make a new Workspace for my new codes.
How can I use the version 6 of JDK? >.<

Anyone out there? >.<

I already have the JDK ver. 6.. I can use the Scanner now..

I have problems with it. I can't display number and characters properly.
I have a code that goes like this:

    int task = scan.nextInt(); // this should be the number input

        if (task==1) {
            System.out.println ("Contents of Employees:" + employees);
        } else if (task==2) {
            System.out.println ("Name of Employee:");
            String name = scan.nextLine(); // this should be the character input
            System.out.println("Name of employee: " + name);

I already have the scanner like this: Scanner scan = new Scanner (System.in); declared already in under public static void main.

I really need help. Plus, I don't know the proper code for inserting a new element in a string and delete an existing element.. I'll have the process by the user, who will put the data, and my code will either insert or delete that element. Just a sample would do. >.<

I'm about to finish my code.

I have problems with it. I can't display number and characters properly.

How did you try displaying them?

// this should be the character input

Why do you want a name to be a single character anyway? A name usually consists of more than one character, right?
If you really want to have one single character, then read the whole line, and pick-off the first character using charAt(0), as suggested in my first reply.

I have a code that goes like this:

You're on the right track, your next step would be adding insert and delete operations.

I don't know the proper code for inserting a new element in a string and delete an existing element..

Don't forget that in Java Strings are immutable, you can't insert/delete "elements" in it.
What you probably meant is inserting and removing a new element in a list of Strings?
Take a look at the Java API documentation for the ArrayList class.
Notice that ArrayList has methods such as add and remove. They are even overloaded.
For the overloaded versions, I refer to the API docs for the ArrayList class, but you probably won't need those for this program.

okay. Here's my new code on the part of else if. I have only one error in it.. Please check what's wrong in it.

    } else if (task==2) {
             do {
            System.out.println("Current list is " + employees);
            System.out.println("Add more? (y/n) ");
            if (scan1.next().startsWith("y")) {
                System.out.println("Enter : ");
                employees.add(scan1.next());
            } else {
                break;
            }
        } while (true);

        System.out.println("List is " + employees);
        String[] arr = employees.toArray(new String[0]);
        System.out.println("Array is " + Arrays.toString(arr));

The next part would be the deletion. >.<

Could you post the error message?
Also, a readability tip: define three constants

final int SHOW   = 1;
final int INSERT = 2;
final int DELETE = 3;

Then you could (re)write your if statements like:

if (task == SHOW) {
    // ...
}
else if (task == INSERT) {
    // ...
}
else if (task == DELETE) {
    // ...
}

This will also work with the switch statement I referred to in an earlier post.

it's just "incompatability type" error on the line:

String[] arr = employees.toArray(new String[0]);

I don't know why.. >.<

Please post your whole code, so that I can reproduce the error.

I fixed it now.. by asking certain people around me.. (phew)
They helped me a lot.
Now what's left is the deleting an array element by user's input.
Here's the code:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;

class Trial5 {
    public static InputStreamReader r = new InputStreamReader (System.in);
    public static BufferedReader inp = new BufferedReader(r);

    public static void main (String args[]) throws Exception {
        ArrayList employees = new ArrayList();
        employees.add("A");
        employees.add("B");
        employees.add("C");
        employees.add("D");
        employees.add("E");

        Scanner scan1 = new Scanner (System.in);
        System.out.println ("Lists of Employees");
        System.out.println ("What do you want to do?:");
        System.out.println ("1 - Display list. \n2 - Insert New Name. \n3 - Delete an item. \n4 - Nothing." + "\n ");
        int task = scan1.nextInt();

        if (task==1) {
            System.out.println ("Contents of Employees:" + employees);
        } else if (task==2) {
             do {
            System.out.println("Current list is " + employees);
            System.out.println("Add more? (y/n)");
            if (scan1.next().startsWith("y")) {
                System.out.println("Enter : ");
                employees.add(scan1.next());
            } else {
                break;
            }
        } while (true);
        System.out.println("List is " + employees);
        String[] arr = (String[]) employees.toArray(new String[0]);
        System.out.println("Array is " + Arrays.toString(arr));
        } else if (task == 3) {  //** I'm still not finished here. (xD)  This should be the part of deleting line with user's input. 

        }

    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.