Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Question [SOLVED] input name of a number

    Who can give an idea how to do a Java program there one can input the name of a number from 1 to 9 (that is, "one" two" and so on) and give then the corresponding result in number. This is my very first program in Java, so :D "be gentle"

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    use a bunch of if-elseif-else statements. its tidious work.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Default

    Like this?
    Java Code:
    class Talnamn{
    
    String namn;
    
    
    void TestNamn(){
    	if (namn == "one" || namn == "One")
    		System.out.println("The number is 1");
    	if (namn == "two" || namn == "Two")
    		System.out.println("The number is 2");
    	if (namn == "three" || namn == "Three")
    		System.out.println("The number is 3");
    	if (namn == "four" || namn == "Four")
    		System.out.println("The number is 4");
    
    
    
    
    }

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    yeah. you can improve it further by using else if and string.toUpperCase() first before checking.
    Take a look at the String API for more info.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    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 AlejandroPe View Post
    Like this?
    Java Code:
    class Talnamn{
    
    String namn;
    
    
    void TestNamn(){
    	if (namn == "one" || namn == "One")
    		System.out.println("The number is 1");
    	if (namn == "two" || namn == "Two")
    		System.out.println("The number is 2");
    	if (namn == "three" || namn == "Three")
    		System.out.println("The number is 3");
    	if (namn == "four" || namn == "Four")
    		System.out.println("The number is 4");
    
    
    
    
    }
    No. You shouldn't compare Strings using == or !=. Instead compare Strings with the equals or equalsIgnoreCase method. For instance:
    Java Code:
      if (namn.equalsIgnoreCase("one"))
      {
         //... do something
      }

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    dang it, overlooked that. nice catch fubarable.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

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

    Default

    Your turn will come to catch my oversight in the next thread.

  8. #8
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Thumbs up

    Thanks Fub och Angry:D
    So I can not compare strings with == , only with .equal?

    So far this is what i've done (the input/output language is swedish). What should I do now?

    Java Code:
    class Talnamn{
    
    String namn;
    int tal;
    
    void TestNamn(){
    	if (namn.equalsIgnoreCase("ett"))
    		System.out.println("Nummret är 1");
    		else if (namn.equalsIgnoreCase("två")){
    			System.out.println("Nummret är 2");
    		}
    		else if (namn.equalsIgnoreCase("tre")){
    			System.out.println("Nummret är 3");
    		}
    		else if (namn.equalsIgnoreCase("fyra")){
    			System.out.println("Nummret är 4");
    		}
    		else if (namn.equalsIgnoreCase("fem")){
    			System.out.println("Nummret är 5");
    		}
    		else if (namn.equalsIgnoreCase("sex")){
    			System.out.println("Nummret är 6");
    		}
    		else if (namn.equalsIgnoreCase("sju")){
    			(namn.equalsIgnoreCase("one"));
    		}
    		else if (namn.equalsIgnoreCase("åtta")){
    			System.out.println("Nummret är 8");
    		}
    	 	else if (namn.equalsIgnoreCase("nio")){
    			System.out.println("Nummret är 9");
    		}	 
    }

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    how do you get user input for name?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    fonso gfx is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    You need to use the Scanner class to get input from an user.
    For more info do a simple google search for "Java Scanner Class examples"

  11. #11
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Question scanner with println()?

    Thanks! I just started reading the Java Scanner Class examples but I have stumbled upon something new that I don't understand, is the printf() method :eek: which I don't want to be involved with right now.

    Is it possible to rewrite the following code using the println() method instead of the printf() method? I guess it would represent a lot of more codes but it would be better for me in order to understand the Java scanner class without any other interference. ;)

    Java Code:
    import java.io.*;
    import java.util.*;
    
    /** Demonstrate the Scanner class for input of numbers.**/
    public class ScanConsoleApp
    {
      public static void main (String arg[]) {
    
        // Create a scanner to read from keyboard
        Scanner scanner = new Scanner (System.in);
    
        try {
          System.out.printf ("Input int (e.g. %4d): ",3501);
          int int_val = scanner.nextInt ();
          System.out.println (" You entered " + int_val +"\n");
    
          System.out.printf ("Input float (e.g. %5.2f): ", 2.43);
          float float_val = scanner.nextFloat ();
          System.out.println (" You entered " + float_val +"\n");
    
          System.out.printf ("Input double (e.g. %6.3e): ",4.943e15);
          double double_val = scanner.nextDouble ();
          System.out.println (" You entered " + double_val +"\n");
    
        }
        catch  (InputMismatchException e) {
          System.out.println ("Mismatch exception:" + e );
        }
      } // main
    
    } // class ScanConsoleApp
    :confused::confused:

  12. #12
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Default

    Where can I find the Java 2 API Specifications??

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Question

    Quote Originally Posted by AlejandroPe View Post
    Thanks! I just started reading the Java Scanner Class examples but I have stumbled upon something new that I don't understand, is the printf() method :eek: which I don't want to be involved with right now.

    Is it possible to rewrite the following code using the println() method instead of the printf() method? I guess it would represent a lot of more codes but it would be better for me in order to understand the Java scanner class without any other interference. ;)

    Java Code:
    import java.io.*;
    import java.util.*;
    
    /** Demonstrate the Scanner class for input of numbers.**/
    public class ScanConsoleApp
    {
      public static void main (String arg[]) {
    
        // Create a scanner to read from keyboard
        Scanner scanner = new Scanner (System.in);
    
        try {
          System.out.printf ("Input int (e.g. %4d): ",3501);
          int int_val = scanner.nextInt ();
          System.out.println (" You entered " + int_val +"\n");
    
          System.out.printf ("Input float (e.g. %5.2f): ", 2.43);
          float float_val = scanner.nextFloat ();
          System.out.println (" You entered " + float_val +"\n");
    
          System.out.printf ("Input double (e.g. %6.3e): ",4.943e15);
          double double_val = scanner.nextDouble ();
          System.out.println (" You entered " + double_val +"\n");
    
        }
        catch  (InputMismatchException e) {
          System.out.println ("Mismatch exception:" + e );
        }
      } // main
    
    } // class ScanConsoleApp
    :confused::confused:
    :confused:

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Not sure what the problem is with the printf(), but yes, you can use the println() method instead, but you won't have the same formating capabilities. For example:
    Java Code:
    Scanner scanner = new Scanner (System.in);
    System.out.println ("Please input a number word from one to nine: ");
    String numName = scanner.next();
    if (numName.equalsIgnoreCase("one"))
     {
      System.out.println("Your number is 1");
     }
    else if (numName.equalsIgnoreCase("two"))
           {
             System.out.println("Your number is 2");
           }
    else ...
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  16. #16
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Cool Error

    So far:
    Java Code:
    import java.io.*;
    import java.util.*;
    
    class Talnamn{
    
    void TestNamn(){
    Scanner scanner = new Scanner(System.in);
    System.out.println("Ange ett talnamn, från ett till nio:");
    String namn = scanner.next();
    	if (namn.equalsIgnoreCase("ett"))
    		System.out.println("Nummret är 1");
    		else if (namn.equalsIgnoreCase("två")){
    			System.out.println("Nummret är 2");
    		}
    		else if (namn.equalsIgnoreCase("tre")){
    			System.out.println("Nummret är 3");
    		}
    		else if (namn.equalsIgnoreCase("fyra")){
    			System.out.println("Nummret är 4");
    		}
    		else if (namn.equalsIgnoreCase("fem")){
    			System.out.println("Nummret är 5");
    		}
    		else if (namn.equalsIgnoreCase("sex")){
    			System.out.println("Nummret är 6");
    		}
    		else if (namn.equalsIgnoreCase("sju")){
    			System.out.println("Nummret är 7");
    		}
    		else if (namn.equalsIgnoreCase("åtta")){
    			System.out.println("Nummret är 8");
    		}
    	 	else if (namn.equalsIgnoreCase("nio")){
    			System.out.println("Nummret är 9");
    		}	 
    }
    }
    Now I get this message when I try to run the program, what does it mean??

    C:\MisJavas>java Talnamn
    Exception in thread "main" java.lang.NoSuchMethodError: main


  17. #17
    fhtwins is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default

    Hello.
    I am a new!

  18. #18
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    There is a function called parseInt that does what you need in Integer class. similar functions in double and float.

    Edit: does this work in Swedish?
    Last edited by rdtindsm; 04-03-2009 at 05:51 AM. Reason: posted in error, add

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

    Default

    rdtindsm: please read what I posted above about not comparing Strings with the == operator.

  20. #20
    Nisha18 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Control if it is a input
    By Torgero in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 12:14 AM
  2. Replies: 1
    Last Post: 11-03-2008, 02:38 PM
  3. Creating a Number Input Dialog in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 07:53 PM
  4. input placed in array
    By smilejava in forum New To Java
    Replies: 5
    Last Post: 11-12-2007, 07:29 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

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