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

Thread: Help with easy programming

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with easy programming

    Hi people! My name is Martin Olofsson, I've started learning Java today and I'm just having a few questions that I hope any of you can answer me on. I'm learning Java by a book called "Programmering 1 Java" (written in Swedish) and it's really helpful.

    What am I trying to do?
    Well, basically I am trying to make the application ask for a character (A-Z) in capital letters and the application should answer in a small letter.

    This is what I currently have:
    package uppgift.pkg2.pkg8;
    import java.util.Scanner;
     
    public class Uppgift28
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
     
            System.out.print("Insert a character: ");
            String alpha = input.nextLine();
     
            char tecken = alpha.charAt(0);
            int kod = (int)tecken + 32;
     
            int newKod = (int)kod;
            String newTecken = Integer.toString(newKod);
     
            System.out.println("A small " + alpha + " is a  " + newTecken + ".");
        }
    }

    What is going wrong?
    This happens:
    run:
    Insert a character: A
    A small A is a 97.
    BUILD SUCCESSFULL (total time: 2 seconds)
    I want it to answer in the character (small 'a'), not 97.

    Is there possibly any helpful genious out there?


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with easy programming

    Well, you are not converting your new code of lower case character to a real lower case character, you just pick a number and put it to string, you need to use "Character.toChars(97)".

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Help with easy programming

    In your application you typecast a char into an int add 32 then when you declare your newKod it's data type is an int you then assign it the value of kod. So as of now your application has 2 variables with the same value (kod = 97; newKod = 97 You need to convert your int number back into a char. Integer.toString(newKod) only turns the variables value into a string so newTecken = "97"

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with easy programming

    There is "built in" functionality to do just that
    See toUpperCase and Character (Java Platform SE 7 )
    Note:String toUpperCase and toLowerCase should be used to map characters to uppercase. String case mapping methods have several benefits over Character case mapping methods.
    Note: Using a hard coded value to do conversion math assumes all character maps are the same, which is not the case.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Help with easy programming

    Or you can just cast to a char.
    (char) 97;


    --- Update ---

    Or you can just cast to a char.
    (char) 97;
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] Easy error to solve in a minute. Help please, I don't want to fail Java Programming again.
    By ihatejava in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 15th, 2012, 03:30 PM
  2. Blackjack programming error (programmer is new to programming)
    By JSingh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 09:13 PM
  3. android programming vs game programming using java
    By vgoel38 in forum Android Development
    Replies: 4
    Last Post: September 8th, 2012, 05:48 PM
  4. Replies: 0
    Last Post: December 12th, 2011, 02:17 PM
  5. an easy clear java programming tutorial
    By zkil_jpf in forum The Cafe
    Replies: 2
    Last Post: April 22nd, 2010, 08:40 AM