9 Replies - 1223 Views - Last Post: 19 December 2008 - 05:42 PM Rate Topic: -----

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Printing contents of array

Posted 19 December 2008 - 01:27 PM

Having a slight problem. I have a text interface. The user can choose the option 1 which will allow them to enter their details. These details will then be saved to an array. They are then taken back to the text menu, where if they choose option 3, the contents of the array should print. However, only the last element of the array is being printed. My problem will either lie with not putting items into array correctly, or somthing wrong with my print method.
This is where i put a user in the array
public void addUser(int choice)
	{
		if(choice==1)
		{
		System.out.println("Please enter your first name " );
		String fName = input.next();
		
		System.out.println("Please enter your last name " );
		String lName = input.next();
		
		System.out.println("Please enter your title " );
		String title = input.next();
		
		System.out.println("Please enter your date of birth " );
		String birthDate = input.next();
		
		System.out.println("Please enter your home address " );
		String homeAddress = input.next();
		
		System.out.println("Please enter your phone number " );
		String phoneNumber = input.next();
		
		System.out.println("Please enter your Library card barcode " );
		int barCode = input.nextInt();
		
		System.out.println("Please enter your Student ID " );
		String studentId = input.next();
		
		StudentBorrower studentBor = new StudentBorrower(fName, lName, title, birthDate, homeAddress, phoneNumber, barCode, studentId);
		int x=0;
		libraryUsers[x]=studentBor;
		x++;
		}


I was thinking that i might need a loop, but then wouldnt this limit the users choice when they want to enter their details? Like with a loop, they couldnt enter details, check contents, enter more details, check contents, etc
so i am not too sure if i am doing the incrementing ok at the bottom of that method.

The printing out method is like so
	public void getBorrowers()
	{
		for(int x=0; x<libraryUsers.length; x++)
		{
		 System.out.println(libraryUsers[x].toString());
		}
	}


I cant really see anything wrong with this, so i am thinking its the way i am adding items to my array in my first method. Any advise would be great

Is This A Good Question/Topic? 0
  • +

Replies To: Printing contents of array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Printing contents of array

Posted 19 December 2008 - 02:01 PM

Problem is here:

int x=0; //x always set to zero before handling array input, only libraryUsers[0] is ever being written to
libraryUsers[x]=studentBor;
x++;




Everytime the method is called x is reset to zero so only one index is being used. Initialize x somewhere else would be the quickest fix to this.

edited for code formatting

This post has been edited by KYA: 19 December 2008 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Printing contents of array

Posted 19 December 2008 - 03:28 PM

kool, kool, that got it going. If i was going to handle incorrect inputs in my addUser method, what would be the best way to do this? Just put it in a try and catch? would i just be catching an IOException?
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Printing contents of array

Posted 19 December 2008 - 04:11 PM

Depends on what you mean by incorrect? You mean anything that isn't String? A try/catch would be a good start and you can tailor it further as necessary.
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Printing contents of array

Posted 19 December 2008 - 04:25 PM

Yeah, say if the user enters a String when an int is required, the program in its currents state quites with an error. What i want it to do is print the error but still allow the user to carry on entering their details. I am going to try a try and catch block, is there anywhere i can see what sort of exceptions i can throw because i cant seem to find them i.e. IOException, SQLException etc
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Printing contents of array

Posted 19 December 2008 - 04:26 PM

They are all at the Java API
Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Printing contents of array

Posted 19 December 2008 - 04:41 PM

I know from the error shown on the command prompt, that it is an InputMismatchException thrown by the scanner. So i have tried
	public void addUser(int choice)
	{
		try{
		
		if(choice==1)
		{
			System.out.println("Please enter your first name " );
			String fName = input.next();
			
			System.out.println("Please enter your last name " );
			String lName = input.next();
			
			System.out.println("Please enter your title " );
			String title = input.next();
			
			System.out.println("Please enter your date of birth " );
			String birthDate = input.next();
			
			System.out.println("Please enter your home address " );
			String homeAddress = input.next();
			
			System.out.println("Please enter your phone number " );
			String phoneNumber = input.next();
			
			System.out.println("Please enter your Library card barcode " );
			int barCode = input.nextInt();
			
			System.out.println("Please enter your Student ID " );
			String studentId = input.next();
			
			StudentBorrower studentBor = new StudentBorrower(fName, lName, title, birthDate, homeAddress, phoneNumber, barCode, studentId);
			libraryUsers[x]=studentBor;
			x++;
		}
		
		else if(choice==2)
		{
			System.out.println("Please enter your first name " );
			String fName = input.next();
			
			System.out.println("Please enter your last name " );
			String lName = input.next();
			
			System.out.println("Please enter your title " );
			String title = input.next();
			
			System.out.println("Please enter your date of birth " );
			String birthDate = input.next();
			
			System.out.println("Please enter your home address " );
			String homeAddress = input.next();
			
			System.out.println("Please enter your phone number " );
			String phoneNumber = input.next();
			
			System.out.println("Please enter your Library card barcode " );
			int barCode = input.nextInt();
			
			System.out.println("Please enter your Staff ID " );
			int staffId = input.nextInt();
			
			StaffBorrower staffBor = new StaffBorrower(fName, lName, title, birthDate, homeAddress, phoneNumber, barCode, staffId);
			libraryUsers[x]=staffBor;
			x++;
		}
		}
		catch(java.util.InputMismatchException  e)
		{
			System.out.println("Must enter an integer here.");
			return;

		}
	}


Error still gets thrown. Can u see where i am not doing it correctly? Will i have to put the try in each if statement rather than do it all together?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Printing contents of array

Posted 19 December 2008 - 04:48 PM

You could do something like this:
private String inputString(String prompt) {
	System.out.println(prompt);
	return input.next();
}

private int inputInt(String prompt) {
	int val = -1;
	for(;;)/> {
		System.out.println(prompt);
		try {
			val = Integer.parseInteger(input.next());
			break;
		} catch (Exception ex) {
			System.out.println("Invalid value, please try again.");
		}
	}
	return val;
}

String fName = inputString("Please enter your first name ");
String lName = inputString("Please enter your last name " );
String title = inputString("Please enter your title ");
int studentId = inputInt("Please enter your Student ID ");



Any time you see repetition in your code, it's a cry for refactoring. ;)
Was This Post Helpful? 0
  • +
  • -

#9 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Printing contents of array

Posted 19 December 2008 - 05:03 PM

Interesting, was getting a bit worried about the code repetition. Just a couple of questions. What does for(;;) mean, does that just loop whenever it needs to? interesting thing. And secondly i am getting the error cannot find symbal method parseInteger(java.lang.String)
Cant work out why i would be getting this error. Does is need an import or anything?
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Printing contents of array

Posted 19 December 2008 - 05:42 PM

for(;;) is a shorthand way of writing while(true) :)

It means "FOREVER". :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1