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

Thread: Very basic coding question, I'd appreciate feedback.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Very basic coding question, I'd appreciate feedback.

    Hi guys, I'm brand-new to Java, self-teaching. If one of the wizards could tell me what I'm doing wrong here, or ideally give me a better way to solve this problem, I'd be eternally appreciative.
    The challenge is incredibly simple, but I only want to use functions, classes or objects that I've referenced in the code below - there's not point me using more advanced functions when I haven't learnt them yet.
    The idea is to ask for 3 names from the user, sort them and return them in alphabetical order to the user. I want to use the scanner class for input.
    My issue is that my code below has compilation errors which I don't understand.
    //Sorted names
    //Ask the user to enter three names
    //Return the names in alphabetic order
     
    import javax.swing.JOptionPane;
    import java.util.Scanner;
     
    public class Challenge007Prompt
    {
    	public static void main(String args[])
    	{
    		String name1, name2, name3;
    		char name1Char, name2Char, name3Char;
    		char name1CharStr, name2CharStr, name3CharStr;
    		String firstChar, secondChar, thirdChar;
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.println("Please enter the first name");
    		name1 = keyboard.nextLine();
    		System.out.println("First name is: " + name1);
     
    		System.out.println("Please enter the second name");
    		name2 = keyboard.nextLine();
    		System.out.println("Second name is: " + name2);
     
    		System.out.println("Please enter the third name");
    		name3 = keyboard.nextLine();
    		System.out.println("Third name is: " + name3);
     
    		name1Char = name1.charAt(0);
    		name2Char = name2.charAt(0);
    		name3Char = name3.charAt(0);
     
     
    		if(name1Char < name2Char && name1Char < name3Char)
    			firstChar=name1;
    		else
    			if(name2Char < name1Char && name2Char < name3Char)
    				firstChar=name2;
    		else
    			if(name3Char < name1Char && name3Char < name2Char)
    				firstChar=name3;
     
    		if(name1Char > name2Char && name1Char > name3Char)
    			thirdChar=name1;
    		else
    			if(name2Char > name1Char && name2Char > name3Char)
    				thirdChar=name2;
    		else
    			if(name3Char > name1Char && name3Char > name2Char)
    				thirdChar=name3;
     
    		name1CharStr = firstChar.charAt(0);
    		name2CharStr = secondChar.charAt(0);
    		name3CharStr = thirdChar.charAt(0);
     
    		if(name1Char==name1CharStr || name1Char==name3CharStr)
    			secondChar=name1;
    		else
    			if(name2Char==name1CharStr || name1Char==name3CharStr)
    			secondChar=name2;
    		else
    			secondChar=name3;
     
    		System.out.println(firstChar + secondChar + thirdChar);
     
    		System.exit(0);
    	}
    }
    Last edited by helloworld922; November 15th, 2012 at 01:40 PM. Reason: please use [code] tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Very basic coding question, I'd appreciate feedback.

    What are the compilation errors? Also, please use the highlight tags when posting code.

    You might also want to look at the compareTo() function of the String class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Basic Question Need Help
    By Graser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 21st, 2012, 10:27 AM
  2. [SOLVED] VERY basic question - if loop
    By kobi1988 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 12th, 2011, 06:34 PM
  3. Basic Java question
    By fred2028 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 13th, 2011, 03:54 PM
  4. Very new to Java basic question
    By loofy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 23rd, 2011, 06:21 PM
  5. [SOLVED] simple question, coding unrelated, how to delete multiple tabs?
    By Perd1t1on in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 07:44 PM