Java Strings Loops Arrays Null?

I assume you are reading less than 100 addresses. The remaining elements in the array address are null. This is the cause for the null value.

Up vote 0 down vote favorite share g+ share fb share tw.

So I was given a set of emails, and I am supposed to read them in, store them in an array, remove the duplicates, and print the "leftovers". I am almost able to do this, but after removing the duplicates, when I print the leftovers, it prints an extra null. Here is my code.

Can someone point me in the direction of fixing it? If it helps, here is a link to the data I was given: cs.colostate.edu/~cs160/assignments/addres... File addresses. Txt is read in as an argument.

I am soo close! Any pointers would help. Public class Duplicate { public static void main(String args){ Scanner keyboard = new Scanner(System.

In); System.out. Println("Enter file name: "); String fileName = keyboard.nextLine(); if(fileName. Equals("")){ System.out.

Println("Error: User did not specify a file name. "); } else{Scanner inputStream = null; try{inputStream = new Scanner(new File(fileName)); } catch(FileNotFoundException e){ System.out. Println("Error: "+ fileName + " does not exist.

"); System. Exit(0); } String address = new String100; for(int i=0;inputStream.hasNextLine();i++){ String email = inputStream.nextLine(); addressi=email.toLowerCase(); //System.out. Println(addressi); } Set mail = new HashSet(Arrays.

AsList(address)); for(String email:mail){ System.out. Println(email); } java arrays string null duplicates link|improve this question edited yesterdayLeigh5,150517 asked yesterdayBean Winz103.

Like earlier questions, this too sounds like homework. Please be sure to use the homework tag. – Leigh yesterday.

I assume you are reading less than 100 addresses. The remaining elements in the array address are null. This is the cause for the null value.

Replace the fixed size array with an ArrayList: List address = new ArrayList(); //... address. Add(email.toLowerCase()); // ... You must also replace the construction of the set: Set mail = new HashSet(address).

Because it will be tested with a random amount thing from this site. – Bean Winz yesterday.

You are trying to read fixed number 100 elements from the file. If there are more e-mails, you will miss some, if there are less - you will have some nulls left which the Set will collapse into a single null. Try using an ArrayList instead of an array, or using a Set from the first place..

1: There is no reason to not just us a Set from the start. – unholysampler yesterday.

String address = new String100; change on SortedSet address = new TreeSet(); and addressi=email.toLowerCase(); change on address. Add(email.toLowerCase()).

Just do this: Set addresses = new HashSet(); // use a Set while (inputStream.hasNextLine()) { addresses. Add(inputStream.nextLine().toLowerCase()); // in-line unused variable } for (String email : mail) { System.out. Println(email); }.

There are several issues with your code. You use a scanner to read from the file, which I've rarely seen and never done myself. The FileInputStream class is more optimised for that purpose.

You use a fixed size array to store an unknown number of Strings You use a for-loop for an unknown number of loop iterations. This is not wrong, but a while-loop is more appropriate. You create a new HashSet from an array.

Again not wrong, but why have you not used the HashSet in your loop? You would have avoided using indices alltogether. I'm assuming you are a beginning programmmer who just whipped up some code to automate something you didn't want to do manually.

Nothing wrong with that of course, and in that case, just use an ArrayList, as all String objects are guaranteed to be unique in your case anyway: public class Duplicate { public static void main(String args){ Scanner keyboard = new Scanner(System. In); System.out. Println("Enter file name: "); String fileName = keyboard.nextLine(); if(fileName.

Equals("")){ System.out. Println("Error: User did not specify a file name. "); } else{Scanner inputStream = null; try{inputStream = new Scanner(new File(fileName)); } catch(FileNotFoundException e){ System.out.

Println("Error: "+ fileName + " does not exist. "); System. Exit(0); } ArrayList addresses = new ArrayList(); for(int i=0;inputStream.hasNextLine();i++){ String email = inputStream.nextLine(); address.

Add(email.toLowerCase()); //System.out. Println(email); } for(String email:addresses){ System.out. Println(email); } With appropriate imports.

Note: that's with minimal changes, there are some things that can still be improved, see above. Good luck!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions