Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: MORE java homework help.

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default MORE java homework help.

    The goal for this project was to allow a user to input the name and age of a person, as many times as they want, until they type "quit". After they type quit, the program is to print the name and age of the youngest person, and the oldest person. So far, I have a (probably not good) way for the user to input all the information and it moves to another method when the user types quit. I found a way to get all the ages to sort, but that's where I'm stuck. When I just print the array (which you see in my code) just to check how it sorts, it sorts a like 90 0's, then say 10 numbers in order.

    I'm having a pretty hard time explaining this but hopefully someone can understand. Here is my code, now any help would be appreciated.

    import java.util.*;
    public class age {
    public static void main(String[] args){
    	Scanner keyboard = new Scanner(System.in);
    	int[] personAge = new int[100];
    	String[] personName = new String[100];
    	int x = 0;
    	char responseChar;
    	responseChar = 'a';
     
    	while(responseChar != 'q'){
    		System.out.println("Please enter a name: ");
    		personName[x] = keyboard.nextLine();
    		responseChar = personName[x].charAt(0);
    		if(responseChar == 'q'){
    			sortAges(personAge);
    		} else{
    		System.out.println("Please enter their age: ");
    		personAge[x] = keyboard.nextInt();
    		keyboard.nextLine();
    		++x;
    		}
    	}
    }
    public static void sortAges(int[] personAge){
    	int sortAge[] = personAge;
    	Arrays.sort(sortAge);
    	System.out.println(Arrays.toString(sortAge));
    }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: MORE java homework help.

    You want the program to terminate when the user types "quit", not something that starts with 'q'. Use the equals method:

    String input ="";
    while(!"quit".equals(input))
    {
         //... stuff inside
    }

    Also, you're sorting the ages, but the names aren't getting moved around. The easiest solution to this is to create a new class called Person which contains the person's name and age. Then, implement the Comparable interface and compareTo method to use Array.sort

    public class Person implements Comparable
    {
         String name;
         int age;
         Public Person(String name, int age)
         {
              this.name = name;
              this.age = age;
         }
     
         public int compareTo(Object person)
         {
              if (!person instanceof Person)
              {
                   throw new RuntimeException("Error! Can only compare Person to Person");
              }
              else if (person == null)
              {
                   throw new NullPointerException();
              }
              return this.age - person.age;
         }
     
         public String toString()
         {
              return name;
         }
     
         public int getAge()
         {
              return age;
         }
    }

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    JFujy (October 13th, 2009), TommyFiz (October 12th, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: MORE java homework help.

    I tried to understand what you were saying, and this is where I'm at now. I haven't got as far as making another class, thats going to be a whole nother story.

    Anyways, why isn't the if statement inside my while loop working? And is that what I should be doing in that situation? But instead of going to sortAges() it should go to a method in another class?

  5. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: MORE java homework help.

    Sorry, I forgot to attach code

    	while(!"quit".equals(input)){
    		System.out.println("Please enter a name: ");
    		personName[x] = keyboard.nextLine();
    		input = personName[x];
    		if(input == "quit"){
    			sortAges(personAge);
    		}
    		System.out.println("Please enter their age: ");
    		personAge[x] = keyboard.nextInt();
    		keyboard.nextLine();
    		++x;
    		}
    	}

  6. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: MORE java homework help.

    you should use "quit".equals(input)

    Regards,
    Chris

  7. #6
    Junior Member
    Join Date
    Sep 2009
    Location
    Romania
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: MORE java homework help.

    I was just randomly browsing the forum, and look what I found ...
    A solve for one of my problems !

    I once tried to make some kind of command prompt , but it didn`t work, because I used if...then...else if statements and comparation operators between strings to get the command wrote by the user (can you understand what I`m saying ? ... o.O ... ), but I always got errors because of this, so I just quit.
    And now, I finally found out the source of my problems ( or issues, whatever you want to call them ).

    Thanks a lot helloworld922 !
    System.out.println(Signature.getSignature());

  8. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: MORE java homework help.

    youre using equality operator to compare Strings?

    if thats the case.. then im not so different with you regarding with your concern i also encountered that problem when comparing Strings until sir JSon told me and saw one of the posts here that states that

    never use '==' or '!=' to compare Strings, i dont know why is it like that.... i dont know if theres anything to do with the logical circuitry of the system when it comes in java compiler (hahahaha )
    always use '.equals()' method to compare Strings and use '!' (exclamation) to negate the value of it...

    also if you want the input of strings ignore the case (no matter what the case is) use
    '.equalsIgnoreCase'


    ill share you my little knowledge 'bout that

    if (input.equalsIgnoreCase("stop"));

    either you enter "STOP" or "stop" your condition will return a true value.

  9. #8
    Junior Member
    Join Date
    Sep 2009
    Location
    Romania
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: MORE java homework help.

    Ironic fact :

    After I posted :
    I was just randomly browsing the forum, and look what I found ...
    A solve for one of my problems !

    I once tried to make some kind of command prompt , but it didn`t work, because I used if...then...else if statements and comparation operators between strings to get the command wrote by the user (can you understand what I`m saying ? ... o.O ... ), but I always got errors because of this, so I just quit.
    And now, I finally found out the source of my problems ( or issues, whatever you want to call them ).

    Thanks a lot helloworld922 !
    I found a book with Java, I looked at the summary, and I found a lesson about the String Class.
    Which contained all that stuff...


    Ironic huh ?

    But thank you anyway !
    System.out.println(Signature.getSignature());

  10. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: MORE java homework help.

    Quote Originally Posted by JFujy View Post
    I was just randomly browsing the forum, and look what I found ...
    A solve for one of my problems !

    Thanks a lot helloworld922 !
    Always good to hear!!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  11. #10
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: MORE java homework help.

    Today in class I was wondering why he STILL wasn't talking about arrays and the assignment was due on Friday, and I found out I actually skipped a chapter and just assumed we were supposed to do the assignment with arrays. After finding that out, this program was simple. Since you guys are helping me, this probably isn't of use, but if anyone is interested here is how I figured it out.

    import java.util.*;
    public class age {
    	public static void main(String[] args){
    		Scanner stdin = new Scanner(System.in);
    		String tempName;
    		String oldName = "";
    		String youngName = "";
    		int tempAge;
    		int oldAge = 0;
    		int youngAge = 200;
    		String input = "";
     
    		while(!"quit".equalsIgnoreCase(input)){
    			System.out.println("Please enter a name: ");
    			tempName = stdin.nextLine();
    			input = tempName;
    			if("quit".equalsIgnoreCase(input)){
    				System.out.println("The oldest person is " + oldName + ", who is " + oldAge + " years old.");
    				System.out.println("The youngest person is " + youngName + ", who is " + youngAge + " years old.");
    			} else {
    				System.out.println("Please enter an age: ");
    				tempAge = stdin.nextInt();
    				stdin.nextLine();
    				if(tempAge > oldAge){
    					oldAge = tempAge;
    					oldName = tempName;
    			} else if(tempAge < youngAge){
    				youngAge = tempAge;
    				youngName = tempName;
    				}
    			}
    		}
    	}
    }

  12. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: MORE java homework help.

    glad to see you figured it out.

    Yes, you can't use the == comparison for strings because strings are objects. The == on objects compares the addresses of the strings. Just because the value of two strings are the same doesn't necessarily mean they have the same address. Ex: There are two perfectly identical bolts. On is used to hold the wheels on your car on. The other is used to hold some piping in place. While they are identical (have the same value) they are not the same bolt.

    The .equals method is implemented to find the same value rather than comparing if two things are the same thing.

    Note: this breaks down when you start talking about the primitive data types, this is because of the lower-level implementations of java. I won't get into the details here, though because that would require nasty talk about pointers

Similar Threads

  1. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 05:56 PM
  2. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 05:34 AM
  3. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 10:52 AM
  4. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM