This program is not running due to 2 errors could someone please assist me? Thanks

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UserList
{

public static void main(String[] args) throws Exception
{
String str1, str2 = "username";
int index;
int initialCapacity = 10;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
ArrayList(String) users = new ArrayList(String)();

System.out.print("Enter a user name: ");
str1 = dataIn.readLine();

while(str1.length() > 0)
{
if(str1 == str2);
System.out.println("That user name is NOT allowed!");
else
{
if(users.size() == initialCapacity)
{
System.out.println("List is full!");
System.out.println("Last entry is "+users.get(initialCapacity));
}
else
if(!users.contains(str1))
{
users.add(str1);
System.out.println("User \""+str1+"\" added to user list.");
}
else
System.out.println("User \""+str1+"\" already in user list.");
}
System.out.print("\nEnter a user name: ");
str1 = dataIn.readLine();
}

System.out.println("Program complete.");
}
}

Recommended Answers

All 9 Replies

It would help if you told us what the error messages are. Copy/paste the entire messages, complete with line numbers etc.

Errors: C:\Course Technology\85985-0d\Chapter09\v4.1\UserL… ';' expected
ArrayList(String) users = new ArrayList(String)();
^
C:\Course Technology\85985-0d\Chapter09\v4.1\UserL… 'else' without 'if'
else
^
2 errors

When you specify a type for an ArrayList you should use <>, not (), as in ArrayList<String>

else without if means you've probably messed up matching the { and } barackets. If you indent your code properly it will probably become obvious where the error is (but fix all ocurrences of mistake 1 below first)

Now, for a new user bonus, here are two errors tha the compiler won't tell you about...
21: if(str1 == str2);
1. the ; terminates the whole if block ie if(str1 == str2) // do nothing
it's a simple mistake and the ; shouldn't be there.
2. == compares two strings to see if they are exactly the same object, which they are not (one is a constant in your program, the other has been read in from the user). To see if two strings contain the same sequence of characters you use String's equals method (see the String API doc for details)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

// Inserted Line Below
import java.util.ArrayList;
// Inserted Line Above

public class UserList2
{

public static void main(String[] args) throws Exception
{
String str1, str2 = "username";
int index;
int initialCapacity = 10;

BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

//Replaced Line with below ArrayList(String) users = new ArrayList(String)();
ArrayList<String> users = new ArrayList<String>();
//

System.out.print("Enter a user name: ");
str1 = dataIn.readLine();

while(str1.length() > 0)
{
//Replaced this line with line below if(str1 == str2);
if(str1 == str2)
//
System.out.println("That user name is NOT allowed!");
else
{
if(users.size() == initialCapacity)
{
System.out.println("List is full!");
System.out.println("Last entry is "+users.get(initialCapacity));
}
else
if(!users.contains(str1))
{
users.add(str1);
System.out.println("User \""+str1+"\" added to user list.");
}
else
System.out.println("User \""+str1+"\" already in user list.");
}
System.out.print("\nEnter a user name: ");
str1 = dataIn.readLine();
}

System.out.println("Program complete.");

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

it seems you didnt import java.util.ArrayList. without doing so , it arraylist wont work.

System.out.println("Last entry is "+users.get(initialCapacity));

this should be System.out.println("Last entry is "+users.get(initialCapacity - 1)); as arraylists an arrays have the same convention regarding indices.

else
{
    if(users.size() == initialCapacity)
    {

why not just use else if(){} .. ?

also , your code waits for user input after the arraylist is full , maybe you should change it so that when size becomes intialCapacity , the loop exits and the program finishes.

if(str1.equalsIgnoreCase (str2))

I forgot to add, this will ensure when you compare the username entered by the user does not equal "username" but it will also ensure "USERNAME" also gets rejected.

So please replace this line (30) below with the one above

30.if(str1 == str2)
System.out.println("Last entry is "+users.get(initialCapacity-1));

You also have a logic error on line 38

38.System.out.println("Last entry is "+users.get(initialCapacity));

The list starts at 0 not 1 so if you reference initialcapacity you are going out of bounds and your app will crash.

So for an extra bonus mark swap line 38 for the top line.

//I have added the complete code list to help your learning

@ mostlydangerous

use the edit post button , consecutive posts look a bit cluttered... and odd.

@pvn29 : use indentation on your code , else will look cluttered.

i edited your code a little bit.
this one is indentated , and as soon as size equals initialCapacity , it exits the program.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class UserList {
    public static void main(String[] args) throws Exception {
        String str1, str2 = "username";
        int index;
        int initialCapacity = 3;

        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<String> users = new ArrayList<String>();
        System.out.print("Enter a user name: ");
        str1 = dataIn.readLine();

        while (str1.length() > 0) {
            boolean containsString = users.contains(str1);
            if (str1.equals(str2))
                System.out.println("That user name is NOT allowed!");
            else if (!containsString) {
                users.add(str1);
                System.out.println("User \"" + str1 + "\" added to user list.");
            }
            else if(containsString){
                System.out.println("User \"" + str1 + "\" already in user list.");
            }
            if (users.size() == initialCapacity) {
                    System.out.println("List is full!");
                    System.out.println("Last entry is " + users.get(initialCapacity - 1));
                    break;
            }
            System.out.print("\nEnter a user name: ");
            str1 = dataIn.readLine();
        }

        System.out.println("Program complete.");
    }
}
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.