2 Errors in Java debugging Assignment, please help :-)?

Hi everyone

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

Code: 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.");
}
}

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

Thank You :-)

SAM2012-02-13T02:14:47Z

Favorite Answer

I found 3 mistakes in your code.....

1. Arraylist declaration: It should be...

ArrayList<String> users = new ArrayList<String>();

2. Missing parenthesis after else...

else
{// You missed this...
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.");
}// You missed this...


3. Semicolon after if statement...
while(str1.length() > 0)
{
if(str1 == str2); // Remove this semi-colon.
System.out.println("That user name is NOT allowed!");


Hope this helps!!!

Regards
SAM

Anonymous2012-02-13T06:33:08Z

Why don't you send this program to the tutors on tutorsonnet website
they provide email based homework help on your java assignment