Results 1 to 19 of 19
  1. #1
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Exclamation Missing return statement help and format method help

    hey guys, i have made my own class which i tohught would run fine after i compiled it, when i run my compiler its coming back with "missing return statement" line 22.. confused at the moment because, i thought i had put everything in the right place... another thing is i have to write a format() method for this class which ensures that columns are aligned, i am a newby to programming so any help would be great!! =D

    Java Code:
     public class PersonTest
    {
    	// Constructs a Person Test Class with the default value.
    	public PersonTest()
    	{
    			AHeight = 0;
    			AAge = 0;
    			Person = null;
    		}//Constructs a Person with a given initial value
    		public String Person(String NewPerson)
    		{
    			Person = NewPerson;
    		}
    				
    		// Gets the First Name of the Person in the PersonTest
    		public String getFName()
    		{
    			return Fname;
    		}
    			
    			// Gets the Last Name of the Person in the PersonTest
    		public String getLName()
    		{
    			return Sname;
    		}
    			
    		// Gets the Height of the Person in the PersonTest
    		public double getHeight()
    		{
    			return AHeight;
    		}
    			
    		// Gets the Age of the Person in the PersonTest
    		public double getAge ()
    		{
    			return AAge;
    		}
    			
    			private String Person;
    			private String Fname;
    			private String Sname;
    			private double AAge;
    			private double AHeight;
    		
    }

  2. #2
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Evening.

    You missed to add a return att line 22. or for the function that starts at line 22. You dont use an IDE ? like NetBeans or eclipse? Thats very helpful for this kind of things.

  3. #3
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey ocean many thanks for the speedy reply. im using notepad 2 at the moment any hints on making my programming easier always welcomed =) so on line 22 return Person;

  4. #4
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    look at this:
    public String Person(String NewPerson)
    {
    Person = NewPerson;
    }
    you tell there is a public string person.
    but never return the String that represents person.
    replace String for void and it will work

  5. #5
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey ocean.. do appreciate your help honestly i do. another thing is i have to write a format() method for this class which ensures that columns are aligned. ive never done format methods so bit stuck on this one

  6. #6
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey ocean i have only ammended my program with your advice in this way and i am now encountering no error messages =)
    Java Code:
    public String Person(String NewPerson)
    		{
    			Person = NewPerson;
    			return NewPerson;
    		}
    i just need to do the format method which i know nothing about at this stage of my programming career

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    you're missing a } bracket.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  8. #8
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Nice, but i cant take solo credit for it, 'dinosoep' is the one you should thank too. =)

    What should your format method do?

  9. #9
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    many thanks to you both i really appreciate it.

    I have to write a format() method for this program which ensures that columns are aligned.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you can't use the formatters that are already available in Java (String.format(...) or System.out.printf(...), ...), then you'll have to roll your own. This means estimating how big you want each column to be, calculating how big the word or phrase is that is being shown in the column (String has a length() method for this) and then padding your String with spaces. A for loop can be used to do the padding.

    So knowing this, why don't you try to create your own formatting method first and see how you do?

  11. #11
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    if i had some sample code to work off, it would be easier. is that cheating? i just cant think of how i can do it. thanks for the reply though :)

  12. #12
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Chewart View Post
    if i had some sample code to work off, it would be easier. is that cheating? i just cant think of how i can do it. thanks for the reply though :)
    Surely you can at least try. It's nothing more than basic math and calling a few methods (as I noted above). Why not give it a go? You've nothing to lose!

    Again, much luck!

  14. #14
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey guys many thanks again for your positive replys to my posts about my program i have attempted making a format class but i seem to be encountering error messages when i try and compile the program. could any one give me guidence as to how and what im doing wrong?
    Java Code:
    		
    		public String format()	 
    		{
    			
    			
    			System.out.printf(AAge);
    			System.out.printf(AHeight);
    			System.out.printf(Sname);
    			System.out.printf(Lname);
         
    		}

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  16. #16
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    basically i want something similar to this printed out

    Fname Lname Age Height
    joe bloggs 22 76 inches

    just cant get my head around it, i know i have to create another program to implement what i have done in my 1st program. i just need to get this format () method introduced first..

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please look at the tutorial that I linked to above. It will help you get your head around it, guaranteed!

  18. #18
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    thank you Fubarable =D

  19. #19
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    many thanks guys for ur guidence. ive completed that part of my program im now in a dilemma as my new program is not working fully. the aim is to have 5 users to input data into their program and to output 5 people that have entered their details respectively. any hints would be great =)

    Java Code:
    //A Program to test my Person Class Program
    import java.util.*;
    public class Person
    {
    	// Tests the methods of the Person File
    	public static void main(String [] args)
    	{
    		int [] i = new NewPerson[5];	
    		Scanner input = new Scanner(System.in);	
    		
    		for (int i = 0; i <= i.length ; i++)
    		{
    			
    		System.out.print("Please enter your Forename ");
    		String FName = input.next();
    
    		System.out.print("Please enter your Surname ");
    		String LName = input.next();
    
    		System.out.print("Please enter your Age ");
    		int AAge = input.nextInt();
    
    		System.out.print("Please enter your Height ");
    		int AHeight = input.nextInt();
    		
    		PersonTest person1 = new PersonTest(FName, LName, AAge, AHeight);
    		person1.format();
    		
    		System.out.println("The number of Persons entered are " + (i));
    		}
    
    			
    	}
    }
    Last edited by Chewart; 12-02-2009 at 12:02 PM. Reason: code brackets wrong

Similar Threads

  1. Help with Recursion and return statement
    By nicolek808 in forum New To Java
    Replies: 3
    Last Post: 09-10-2009, 10:02 AM
  2. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM
  3. Replies: 3
    Last Post: 05-25-2009, 08:50 PM
  4. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04:55 PM
  5. [SOLVED] return statement
    By Nakira in forum New To Java
    Replies: 8
    Last Post: 11-12-2008, 11:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •