11 Replies - 613 Views - Last Post: 10 November 2009 - 06:40 PM Rate Topic: -----

#1 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Question about ArrayLists

Posted 10 November 2009 - 05:02 PM

I was finally able to get a Combobox and arraylist to compile and run but for some reason when i run the JFrame, it says

java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at SkypeDialer.main(SkypeDialer.java:58)

 ArrayList<String> contactInfo = new ArrayList<String>(); // ArrayList to hold information
		try
		{
			File read = new File("contactList.txt");
			Scanner in = new Scanner(new File("contactList.txt")); // scanner to read in the txt file
			String name = "";
			String pNumber = "";
			//ArrayList<String> contactInfo = new ArrayList<String>(); // ArrayList to hold information
			while(in.hasNextLine()) // Loops through until the txt file is done
			{
			  name = in.next() + " " + in.next(); // Gets the first name then adds space and Last name
			  pNumber = in.next(); // gets the Phone Number
			  in.nextLine(); // clears buffer to next line
			  contactInfo.add(name); contactInfo.add(pNumber);
			}
		}
		catch(IOException e)
		{
			System.out.println("************** Error *****>>>>>>");
			System.out.println("* This File is unexisting or unreadable");
		}
	


Here is the code for the combobox which compiles

	
	JPanel p5 = new JPanel();
	//*************************************************************************
	for(String i : contactInfo)
		{
		JComboBox cb1;
		cb1 = new JComboBox();
		cb1.addItem(contactInfo);
		p5.add(cb1);
		}
	//*************************************************************************
	
//	cb1 = new JComboBox();
//	cb1.addItem("first name");
//	cb1.setEditable(true);
	p5.setLayout(new GridLayout(1,1));
//	p5.add(cb1);
	cp.add(p5, BorderLayout.SOUTH);


If I comment out the nextLine in the ArrayList and run the program, it prints out only the first line.

Here is the data in the txt file


Bart Simpson 1(416)123-4567
Homer Simpson 1(416)987-6543
Marge Simpson 1(905)546-5469
John Doe 1(905)845-4464

Is This A Good Question/Topic? 0
  • +

Replies To: Question about ArrayLists

#2 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: Question about ArrayLists

Posted 10 November 2009 - 05:16 PM

Could you please post all of your code?
Was This Post Helpful? 0
  • +
  • -

#3 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Question about ArrayLists

Posted 10 November 2009 - 05:22 PM

That is all the code that could be the issue.
If I posted the entire program, it'd be over 300 lines!

Nonetheless, those two snipits above are where the problem could be taking place because when I comment out the in.nextLine() and run the program, it runs but only adds the first entry to the combobox
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Question about ArrayLists

Posted 10 November 2009 - 05:23 PM

hi
this is the broblem:
//...
in.nextLine();


this command returns a value.
it is not supposed just to advance in line.
so when you check in your while loop while(in.hasNextLine())
when you get to the last line, you get the values out to name and pNumber.
then you add the code in.nextLine(), and your program looks for the nexxt line which doesnt exist.
Was This Post Helpful? 0
  • +
  • -

#5 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: Question about ArrayLists

Posted 10 November 2009 - 05:25 PM

I want to compile it locally and see everything in the program and also the errors compiler gives. Its much easier for me to fix something if i have the code.
besides 300 lines isnt that much.

This post has been edited by Atspulgs: 10 November 2009 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

#6 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Question about ArrayLists

Posted 10 November 2009 - 05:27 PM

How can in.nextLine() return a value?

This is from the Java API

public String nextLine()

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Returns:
the line that was skipped
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Question about ArrayLists

Posted 10 November 2009 - 05:34 PM

It returns the string it went over. You just answered your own question.
Was This Post Helpful? 0
  • +
  • -

#8 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Question about ArrayLists

Posted 10 November 2009 - 05:35 PM

So then how would I change it so that it returns the string of the next line until the end of the file
Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Question about ArrayLists

Posted 10 November 2009 - 05:39 PM

well you answered your question yourself..
methods which declared as void, such as:
public void doSomething(){
//code
}


dont return a value.
however, methods declared as:
public int doSomething(){
int a;
//code
return a;//returns an int value
}

public String doSomething(){
String a;
//code
return a;//returns a String object
}



those methods return a value.
so inextLine is declared as:
public String nextLine()



so you can see that it returns a String object.
Was This Post Helpful? 0
  • +
  • -

#10 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Question about ArrayLists

Posted 10 November 2009 - 05:41 PM

I understand the return type but I don't get how to change the file reading code so that it reads the next line until the end of the file
Was This Post Helpful? 0
  • +
  • -

#11 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Question about ArrayLists

Posted 10 November 2009 - 05:59 PM

first, read the file until you encounter a null line.
then use the nextLine() method to get a new line.
Then you could use the split method, since each field is seperated by space.
youll get a String[] object.
then assign each field to the appropriate variable.
as:
//globar vars:
private static final int FIRSTNAME = 0;
private static final int LASTNAME = 1;
private static final int PNUMBER = 2;

//your code
//then the while loop as:

while((line = in.readLine()) != null){
String[] tmp = line.split(" ");
name = tmp[FIRSTNAME] + " " + tmp[LASTNAME];
pNumber = tmp[PNUMBER];
contactInfo.add(name); contactInfo.add(pNumber);
}



I suggest that you create a ContactInfo class, with the constructor:
public ContactInfo(String name, String pNumber){....}



then your ArrayList would be:
ArrayList<ContactInfo> contacts = new ArrayList<ContactInfo>();


a better way to store your data ordered.
Was This Post Helpful? 0
  • +
  • -

#12 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Question about ArrayLists

Posted 10 November 2009 - 06:40 PM

I keep getting an error with:

while((line = in.readLine()) != null

saying that it can't find the symbol readLine(). Why is that?





	String line;
   			
   			while((line = in.readLine()) != null)
			  
			  {
			
			String[] tmp = line.split(" ");
			name = tmp[FIRSTNAME] + " " + tmp[LASTNAME];
			pNumber = tmp[PNUMBER];
			contactInfo.add(name); contactInfo.add(pNumber);
			  
			  }










View Postjapanir, on 10 Nov, 2009 - 04:59 PM, said:

first, read the file until you encounter a null line.
then use the nextLine() method to get a new line.
Then you could use the split method, since each field is seperated by space.
youll get a String[] object.
then assign each field to the appropriate variable.
as:
//globar vars:
private static final int FIRSTNAME = 0;
private static final int LASTNAME = 1;
private static final int PNUMBER = 2;

//your code
//then the while loop as:

while((line = in.readLine()) != null){
String[] tmp = line.split(" ");
name = tmp[FIRSTNAME] + " " + tmp[LASTNAME];
pNumber = tmp[PNUMBER];
contactInfo.add(name); contactInfo.add(pNumber);
}



I suggest that you create a ContactInfo class, with the constructor:
public ContactInfo(String name, String pNumber){....}



then your ArrayList would be:
ArrayList<ContactInfo> contacts = new ArrayList<ContactInfo>();


a better way to store your data ordered.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1