Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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 :-)

2 Answers

Relevance
  • SAM
    Lv 4
    9 years ago
    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

  • Anonymous
    9 years ago

    Why don't you send this program to the tutors on tutorsonnet website

    they provide email based homework help on your java assignment

Still have questions? Get your answers by asking now.