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 6 of 6

Thread: please help me in my assignment :(

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default please help me in my assignment :(

    Here's the program: Write a class Sort. that allows you to input three string names and output at ascending order...
    here's my code...i dont know what's wrong with this..please help me guys..im just a beginner at all..im still learning...

    import java.util.*;
     
    public class Sort {
     
    	public static void main (String []args){
    	Scanner console = new Scanner(System.in);
     
    	String name1, name2, name3;
     
    	System.out.println("Enter name1: ");
    	name1= console.nextLine();
    	System.out.println("Enter name2: ");
    	name2= console.nextLine();
    	System.out.println("Enter name3: ");
    	name3= console.nextLine();
     
    	int x = name1.compareTo(name2);
    	int y = name2.compareTo(name3);
    	int z= name3.compareTo(name2);
     
     
    	if (x< 0)
     
    		System.out.println(name1);
    			{
    				if(x> 0)
    				System.out.println(name2);
    			}
     
    	if (y< 0)
    		System.out.println(name2);
    			{
    			if (y>0)
    			System.out.println(name3);
    			}	
    	if (z < 0 )
    		System.out.println(name3);
    			{
    			if (z< 0 )
    			System.out.println(name1);
     
    			}
     
    		}
    }
    Last edited by helloworld922; May 15th, 2010 at 10:29 AM.


  2. #2
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: please help me in my assignment :(

    whats the output for that piece of code?

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: please help me in my assignment :(

    Quote Originally Posted by asdfg View Post
    ..please help me guys..im just a beginner at all..im still learning...
    The best advice I can give you is to work out how to use the results of the compareTo methods by hand, on paper, in English (or your native language) before writing any code. Once you know exactly what you have to do, writing the code is just translating it into Java.

  4. #4
    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

    Default Re: please help me in my assignment :(

    You could always do it like this:

    import java.util.*;
     
    public class Sort {
     
    	public static void main(String[] args) {
    		Scanner console = new Scanner(System.in);
     
    		String[] names = new String[3];
     
    		System.out.println("Enter name1: ");
    		names[0] = console.nextLine();
    		System.out.println("Enter name2: ");
    		names[1] = console.nextLine();
    		System.out.println("Enter name3: ");
    		names[2] = console.nextLine();
     
    		Arrays.sort(names);
     
    		for (int a = 0; a < names.length; a++) {
    			System.out.println(names[a]);
    		}
     
    	}
    }
    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.

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: please help me in my assignment :(

    Quote Originally Posted by javanub:( View Post
    whats the output for that piece of code?
    output would be:
    if the user enter: anne, annie, ann
    then it will display:
    ann
    anne
    annie

  6. #6
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: please help me in my assignment :(

    Quote Originally Posted by JavaPF View Post
    You could always do it like this:

    import java.util.*;
     
    public class Sort {
     
    	public static void main(String[] args) {
    		Scanner console = new Scanner(System.in);
     
    		String[] names = new String[3];
     
    		System.out.println("Enter name1: ");
    		names[0] = console.nextLine();
    		System.out.println("Enter name2: ");
    		names[1] = console.nextLine();
    		System.out.println("Enter name3: ");
    		names[2] = console.nextLine();
     
    		Arrays.sort(names);
     
    		for (int a = 0; a < names.length; a++) {
    			System.out.println(names[a]);
    		}
     
    	}
    }

    thanks so much this is a great help

Similar Threads

  1. College Assignment please help
    By The Lost Plot in forum Collections and Generics
    Replies: 7
    Last Post: March 13th, 2012, 09:27 AM
  2. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 04:20 PM
  3. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  4. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 09:44 PM
  5. need help in my assignment guys :(
    By Mr.cool in forum Collections and Generics
    Replies: 7
    Last Post: December 28th, 2009, 08:30 AM