1 Replies - 1136 Views - Last Post: 13 May 2007 - 10:23 AM Rate Topic: -----

#1 dbrine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 25-April 07

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.

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



Is This A Good Question/Topic? 0
  • +

Replies To: IOException and Class error

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: IOException and Class error

Posted 13 May 2007 - 10:23 AM

Hi, I modified your code
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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1