Results 1 to 7 of 7
  1. #1
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Exclamation How To Determine Each Character When Written In A String?

    Here is what i need to get working:

    - Create a program that accept string value. If the letter is odd then converts it to upper case and put it insidde <> (angle brackets). If not, disply its ASCII value.
    - Must loop (ask the user if he/she wants to enter another string)
    - Use try and catch to handle error reporting

    Sample output
    Enter String: AaBb
    <A><A>9866

    I'm a newbie in Java and i have this program codes:

    Java Code:
    import java.lang.*;
    import java.io.*;
    public class LongQuiz
    {
    	public static void main (String Args[]) throws IOException
    	{
    		String name;
    		char choice;
    		int x=0;
    		BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    			System.out.print("\nDo You Want To Enter A String [Y/N]: ");
    			choice=(char)System.in.read();
    			if(choice=='N'||choice=='n')
    			System.out.print("Thank You For Using The Program :))");
    			else if(choice=='y'||choice=='y')
    			{
    				System.out.print("\nEnter String: ");
    				int len = name.length();
    				name=br.readLine();
    				for(x=0; x<30; x++)
        				{
        					if(name=='a')
        					System.out.print("<A>");
        					else if(name=='A')
        					System.out.print("<A>");
        					else if(name[x]=='b')
        					System.out.print("98");
       					else if(name[x]=='B')
        					System.out.print("66");
        					else if(name[x]=='c')
        					System.out.print("<C>");
        					else if(name[x]=='C')
        					System.out.print("<C>");
        					else if(name[x]=='d')
        					System.out.print("100");
        					else if(name[x]=='D')
        					System.out.print("68");
    				}
        			}
        			else
    			System.out.print("\n \nINVALID INPUT");
    	}
    }
    THANKS IN ADVANCE FOR YOUR HELP
    Last edited by JanAlbertLam; 02-07-2013 at 02:26 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: How To Determine Each Character When Written In A String?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How To Determine Each Character When Written In A String?

    Hi Jan.

    you're trying to do all at once. Break it down into smaller bits.
    1) read user input in a loop until user wants to quit.
    2) break down user input into single chars, input might be more than one char
    3) think about non-chars in the user input, e.g. 23,.-
    4) find a solution for converting the chars, the ascii value is the key.

    So, implement number one and ask specific questions when you get stuck. If it works continue with the next step. Try not to stuff all into the main method.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: How To Determine Each Character When Written In A String?

    Quote Originally Posted by PhHein View Post
    Hi Jan.

    you're trying to do all at once. Break it down into smaller bits.
    1) read user input in a loop until user wants to quit.
    2) break down user input into single chars, input might be more than one char
    3) think about non-chars in the user input, e.g. 23,.-
    4) find a solution for converting the chars, the ascii value is the key.

    So, implement number one and ask specific questions when you get stuck. If it works continue with the next step. Try not to stuff all into the main method.
    1) read user input in a loop until user wants to quit.
    - i can make the looping work now.
    2) break down user input into single chars, input might be more than one char
    - this is mainly my problem:
    say for instance, if i input 'AaB', java will read it as a whole string. what i want to do is for the java to read it per character so i can determine that, like my example,
    'A' is capital letter a, 'a' is small letter a and 'B' is capital letter b. from there runs my 'if/else' code
    3) think about non-chars in the user input, e.g. 23,.-
    - i know this can work with the 'else' statement at the end of the 'if/else' statement, right?
    4) find a solution for converting the chars, the ascii value is the key
    - how do i convert this? (cprog basically do it by reading it as integer as in the code '%i')

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How To Determine Each Character When Written In A String?

    Quote Originally Posted by JanAlbertLam View Post
    1) read user input in a loop until user wants to quit.
    - i can make the looping work now.
    OK, do it.
    Quote Originally Posted by JanAlbertLam View Post
    2) break down user input into single chars, input might be more than one char
    - this is mainly my problem:
    say for instance, if i input 'AaB', java will read it as a whole string. what i want to do is for the java to read it per character so i can determine that, like my example,
    'A' is capital letter a, 'a' is small letter a and 'B' is capital letter b. from there runs my 'if/else' code
    Look at the String API. Loop over the String. Your if / else is overkill.
    Quote Originally Posted by JanAlbertLam View Post
    3) think about non-chars in the user input, e.g. 23,.-
    - i know this can work with the 'else' statement at the end of the 'if/else' statement, right?
    Your if / else is overkill. You only need a small method to identify whether the char is odd or even.
    Quote Originally Posted by JanAlbertLam View Post
    4) find a solution for converting the chars, the ascii value is the key
    - how do i convert this? (cprog basically do it by reading it as integer as in the code '%i')
    Cast the char to int.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    JanAlbertLam is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: How To Determine Each Character When Written In A String?

    Look at the String API. Loop over the String. Your if / else is overkill.
    - how should i do it. i beg of you to give me a sample code

    Your if / else is overkill. You only need a small method to identify whether the char is odd or even.
    - what's the code for this in java?

    Cast the char to int.
    - also, how can i do this. code pls, if you may :))

    I really need help since i just a newbie., and we this due 6pm (Beijing time).
    This is a graded quiz and the prof hasn't even discussed about this. :(((

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How To Determine Each Character When Written In A String?

    Quote Originally Posted by JanAlbertLam View Post
    Look at the String API. Loop over the String. Your if / else is overkill.
    - how should i do it. i beg of you to give me a sample code
    String.charAt(index)
    Quote Originally Posted by JanAlbertLam View Post
    Your if / else is overkill. You only need a small method to identify whether the char is odd or even.
    - what's the code for this in java?
    int odd = 'A' -64;
    Quote Originally Posted by JanAlbertLam View Post
    Cast the char to int.
    - also, how can i do this. code pls, if you may :))
    int i = 'c';
    Quote Originally Posted by JanAlbertLam View Post
    I really need help since i just a newbie., and we this due 6pm (Beijing time).
    This is a graded quiz and the prof hasn't even discussed about this. :(((
    Talk to your prof. We won't do it for you as it's your grade.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Replies: 1
    Last Post: 02-26-2012, 07:10 AM
  2. Replies: 3
    Last Post: 06-03-2011, 02:57 PM
  3. Convert Character to String
    By andy9507 in forum New To Java
    Replies: 3
    Last Post: 05-21-2011, 03:29 PM
  4. Removing last character from a String
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 12:43 AM
  5. how to get next character/string
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-28-2009, 04:04 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
  •