Problem with storing strings into object arrays

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 729 Views - Last Post: 12 April 2012 - 09:04 AM Rate Topic: -----

#1 Riprickwright  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 12

Problem with storing strings into object arrays

Posted 11 April 2012 - 06:03 PM

My Project for my java class is to create a phone book with certain keys having a certain function. My problem is with the entry function because my instructor wants us to put the inputs into an object array that can hold no more than 100 values. The last fix I made on this ran with gave me something along the lines of "Entry@14318bb
Entry@ca0b6
Entry@10b30a7" for a hundred lines instead of what I put in.
import java.io.*;
import java.util.*;

class Entry {
	public String name, number, note;	
	
	
	
	Entry()
	{
		name = "";
		number = "";
		note = "";
	}
}





public class Phonebook {
	public static Entry[] entrylist = new Entry[100];
	


	public static void main(String[] args) throws Exception {
		for (int i=0; i<entrylist.length; i++){
			entrylist[i] = new Entry();
		}
		Scanner input = new Scanner(System.in);
		File file = new File("contactlist.txt");
		BufferedWriter output = new BufferedWriter(new FileWriter(file));
		System.out.println("Codes are entered as 1 to 8 characters. \nUse \"e\" for enter, \"f\" for find, \"l\" to list, \"q\" to quit.");
		int i = 0;
		int j = 0;
		while (i == 0) {
			System.out.println("Command: ");
			String g = input.next();
			String[] token = g.split(" ");
			String k = token[0];
			if (k.equals("q")) {
				break;
			}
			if (k.equals("e")) {
				if (j >= entrylist.length){
					System.out.println("You have already used all of your entries...");
				}
				else{
				entrylist[j].name = input.next();
				System.out.println("Enter number: ");
				entrylist[j].number = input.next();
				System.out.println("Note: ");
				entrylist[j].note = input.next();
				j++;
				}
			}
			if (k.equals("f")) {
				System.out.println("Ha, im not going to find shit!");
			}
			if (k.equals("l")) {
				for (int l = 0; l < entrylist.length; l++){
					if (entrylist == null){
						System.out.println("");
					}
					else
					System.out.println(entrylist[l]);
				}
			}
		}
		}


}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with storing strings into object arrays

#2 Riprickwright  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 12

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 06:09 PM

* I used the function "e" to enter the name number and note a few times then used "l" to display to get the hundred lines of "Entry@14318bb
Entry@ca0b6
Entry@10b30a7"
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 06:17 PM

System.out.println(entrylist[l]);

your Entry class will need a toString() method something like

public String toString() {
   return name + " " + number + " " + note;
}


Was This Post Helpful? 1
  • +
  • -

#4 oha055  Icon User is online

  • D.I.C Regular

Reputation: 49
  • View blog
  • Posts: 271
  • Joined: 02-February 09

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 06:21 PM

You should override the Entry class' toString() method.

public String toString() {
return name + " " + number + " " + note;
}

System.out.println(entrylist[l].toString());


Was This Post Helpful? 1
  • +
  • -

#5 Silent Ace  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 07-January 09

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 06:49 PM

Also break is not terminating the console it only breaks the code.
if (k.equals("q")) {
     System.exit(0);
}


Was This Post Helpful? 1
  • +
  • -

#6 Riprickwright  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 12

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 06:50 PM

Thank you guys so much. I have one more question, how would I go about alphabetizing entrylist by the first letter so that entrylist[0] would be the first name in alphabetical order?
I have a rough idea:
if (k.equals("l")) {
        for (int a = 0; a < entrylist.length; a++){
	String[] t = entrylist[a].name.split("");
	String kd = t[0];
	}

Would this be how I split the first letter from the list and how would I arrange them using it?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 07:00 PM

Simply implements Comparable in your Entry class


class Entry implements Comparable<Entry>{
	public String name, number, note;	
		
	Entry()
	{
		name = "";
		number = "";
		note = "";
	}

        public String toString() {
           return name + " " + number + " " + note;
        }

        public int compareTo(Entry other) {
           return name.compareTo(other.name);
        }
}



Then in your code you can simply: Arrays.sort(entryList);

Happy coding


*Edited: oups forgot to remove extra code

This post has been edited by pbl: 11 April 2012 - 07:25 PM

Was This Post Helpful? 1
  • +
  • -

#8 Riprickwright  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 12

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 07:15 PM

Where should I put that at? I put it the if "l" statement:
if (k.equals("l")) {
				for (int a = 0; a < entrylist.length; a++){
					Arrays.sort(entrylist);						
					}
				for (int l = 0; l < entrylist.length; l++){
					if (entrylist == null){
						System.out.println("");
					}
					else
					System.out.println(entrylist[l].toString());
				}
			}


and when I entered "l" I got:
Exception in thread "main" java.lang.ClassCastException: Entry cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at Phonebook.main(Phonebook.java:69)
And line 69 is where I put the statement at
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 07:29 PM

Arrays.sort(entrylist);

just once do need to put it in a loop

Do you still have null entries in your array[] or they are all filled ?
Did you add the implements Comparable<Entry> in your Entry class declaration ?
Was This Post Helpful? 1
  • +
  • -

#10 Riprickwright  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 12

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 07:37 PM

Entry()
	{
		name = "";
		number = "";
		note = "" ;
	}
	public String toString() {		
		return name + " " + number + " " + note;
	}
	public int compareTo(Entry other) {
		return name.compareTo(other.name);
	}
	}



I did and I changed the "l" so there is no loop but I still get the same result.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 07:45 PM

Do you still have null entries in your array[] or they are all filled ?
Did you add the implements Comparable<Entry> in your Entry class declaration ?

class Entry implements Comparable<Entry> {
Was This Post Helpful? 1
  • +
  • -

#12 Riprickwright  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 12

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 08:02 PM

Sorry about that. Yes all of the statements are no longer null and this works out now, but when I use the "l" statement I get all of the other blank entries:
"











name a
name b"
how would I get rid of them in the print statement?
Thank you so much for your patience with me so far.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 08:06 PM

Yes because you filled you array with new Entry() where name and num are ""

you can always
Arrays.sort(entries);
for(int i = 0; i < entries.length; i++) {
   String str = entries[i].trim();
   if(str.length() > 0)
      System.out.println(str);
}


Was This Post Helpful? 1
  • +
  • -

#14 Riprickwright  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 12

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 08:15 PM

When I tried it, I got "trim is an unidentified method for the type Entry"
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Problem with storing strings into object arrays

Posted 11 April 2012 - 08:22 PM

sorry

String str = entries[i].toString().trim();
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2