public class UserList
{
public static void main(String[] args) throws IOException
{
String str1, str2 = "username";
int index;
int initialCapacity = 10;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
ArrayList users = new ArrayList();
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.");
}
}
IOException and Class error
Page 1 of 11 Replies - 1551 Views - Last Post: 13 May 2007 - 10:23 AM
#1
IOException and Class error
Posted 13 May 2007 - 08:20 AM
I have looked thru the code but I keep getting several errors. The errors i get are a IOException and class error when complied.
Replies To: IOException and Class error
#2
Re: IOException and Class error
Posted 13 May 2007 - 10:23 AM
Hi, I modified your code
It compiles, the problems were:
Yoy haven't imported the needed classes
And in the first if() I made changes because you can't compare Strings like that
import java.io.*;
import java.util.ArrayList;
public class UserList
{
public static void main(String[] args) throws IOException
{
String str1, str2 = "username";
int index;
int initialCapacity = 10;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
ArrayList users = new ArrayList();
System.out.print("Enter a user name: ");
str1 = dataIn.readLine();
while(str1.length() > 0)
{
if(str1.equals(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.");
}
}
It compiles, the problems were:
Yoy haven't imported the needed classes
And in the first if() I made changes because you can't compare Strings like that
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|