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: Answer won't print!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Seattle, Washington
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Answer won't print!

    Assignment was to compare two strings that have one letter from the alphabet. the output should be which of them has the greatest value(which letter is closer towards z). for example, if the user entered "a" for the first string and "b" for the second string, the output should say that "a" is less than "b." (a=1, b=2, c=3 and so on until z=26). So far, the user can only enter the two strings, but there is no output and i have no idea why. Can someone give me a pointer?
    import java.util.Scanner;
    public class SmallestChar {
     
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		String char1, char2;
    		int value1 = 0, value2 = 0;
    		System.out.println("Enter first char: ");
    		char1 = scan.next();
    		System.out.println("Enter second char: ");
    		char2 = scan.next();
    		if(char1 == "a")
    			value1 = 1;
    		else if(char1 == "b")
    			value1 = 2;
    		else if(char1 == "c")
    			value1 = 3;
    		else if(char1 == "d")
    			value1 = 4;
    		else if(char1 == "e")
    			value1 = 5;
    		else if(char1 == "f")
    			value1 = 6;
    		else if(char1 == "g")
    			value1 = 7;
    		else if(char1 == "h")
    			value1 = 8;
    		else if(char1 == "i")
    			value1 = 9;
    		else if(char1 == "j")
    			value1 = 10;
    		else if(char1 == "k")
    			value1 = 11;
    		else if(char1 == "l")
    			value1 = 12;
    		else if(char1 == "m")
    			value1 = 13;
    		else if(char1 == "n")
    			value1 = 14;
    		else if(char1 == "o")
    			value1 = 15;
    		else if(char1 == "p")
    			value1 = 16;
    		else if(char1 == "q")
    			value1 = 17;
    		else if(char1 == "r")
    			value1 = 18;
    		else if(char1 == "s")
    			value1 = 19;
    		else if(char1 == "t")
    			value1 = 20;
    		else if(char1 == "u")
    			value1 = 21;
    		else if(char1 == "v")
    			value1 = 22;
    		else if(char1 == "w")
    			value1 = 23;
    		else if(char1 == "x")
    			value1 = 24;
    		else if(char1 == "y")
    			value1 = 25;
    		else if(char1 == "z")
    			value1 = 26;
     
    		if(char2 == "a")
    			value2 = 1;
    		else if(char2 == "b")
    			value2 = 2;
    		else if(char2 == "c")
    			value2 = 3;
    		else if(char2 == "d")
    			value2 = 4;
    		else if(char2 == "e")
    			value2 = 5;
    		else if(char2 == "f")
    			value2 = 6;
    		else if(char2 == "g")
    			value2 = 7;
    		else if(char2 == "h")
    			value2 = 8;
    		else if(char2 == "i")
    			value2 = 9;
    		else if(char2 == "j")
    			value2 = 10;
    		else if(char2 == "k")
    			value2 = 11;
    		else if(char2 == "l")
    			value2 = 12;
    		else if(char2 == "m")
    			value2 = 13;
    		else if(char2 == "n")
    			value2 = 14;
    		else if(char2 == "o")
    			value2 = 15;
    		else if(char2 == "p")
    			value2 = 16;
    		else if(char2 == "q")
    			value2 = 17;
    		else if(char2 == "r")
    			value2 = 18;
    		else if(char2 == "s")
    			value2 = 19;
    		else if(char2 == "t")
    			value2 = 20;
    		else if(char2 == "u")
    			value2 = 21;
    		else if(char2 == "v")
    			value2 = 22;
    		else if(char2 == "w")
    			value2 = 23;
    		else if(char2 == "x")
    			value2 = 24;
    		else if(char2 == "y")
    			value2 = 25;
    		else if(char2 == "z")
    			value2 = 26;
     
    		if (value1 > value2)
    			System.out.println("The smaller value of " + char1 + " and " + char2 + " is " + char2 + ".");
    		if(value1 < value2)
    			System.out.println("The smaller value of " + char1 + " and " + char2 + " is " + char1 + ".");
    		if(char1 == char2)
    				System.out.println("The values are both \'" + char1 + "\'.");
     
    	}
     
    }
    Thanks in advance!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Answer won't print!

    Asked and answered numerous times. Search for equals method.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    gotdatdough (October 30th, 2013)

Similar Threads

  1. Can any One answer it !!!!!!!!!!!!!!!!!!!!!
    By Srinivas_Java_Developer in forum Java Servlet
    Replies: 1
    Last Post: October 23rd, 2013, 12:42 PM
  2. Replies: 3
    Last Post: March 9th, 2013, 07:22 PM
  3. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  4. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM
  5. No errors but just won't print out the first element of an array list
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2011, 06:30 PM