Jump to content

charAt function

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
hawil

hawil

    Newbie

  • Members
  • PipPip
  • 26 posts
i just started java programming weeks ago.. i'm wondering anyone can help me explain elaborately what does the red text mean?
and i just found another code similar to this but using bufferedreader somewhat like that and had the same output.. what are there difference? thanks.

import java.util.*;
import static java.lang.System.out;

public class charaAtdummy {
    
    public static void main (String how[]) {
        [COLOR=Red]try {[/COLOR]
                Scanner Input = new Scanner (System.in);
        
                int length;
                char Char;
        
                out.print("enter string: ");
                String string = Input.nextLine ();
        
                [COLOR=Red]length = string.length ();[/COLOR]
                out.println(length);
                [COLOR=Red]Char = string.charAt (4);[/COLOR]
                out.print(Char);
        }
            [COLOR=Red]catch(Exception e){}    [/COLOR]
    }
}


#2
kelvo_link

kelvo_link

    Newbie

  • Members
  • Pip
  • 4 posts
the try and catch
thingy
is for error handling

try researching error handling

length = string.length ();
* it means
the number of character used will be stored in integer length

for example your input is :
apple

length=5;

Char = string.charAt (4);
* it means that the character at the 4th place will be stored to Char

here is the order ;

for example

apple

a is the character at 0

p is the character at 1

and so on and so forth

0 1 2 3 4
a p p l e


so that means if your input is :
apple

String sample="apple";


sample.charAt(4)


is equal to 'e'


well hope you like my answer



Sorry for my bad English

^^ ,

#3
hawil

hawil

    Newbie

  • Members
  • PipPip
  • 26 posts
i understand it perfectly. thanks. do you know about the bufferedreader? 'cause i found a code same as like this but using bufferedreader..

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Scanner is easier because it has the predefined nextInt() nextChar() nextLine() stuff.
The bufferedreader does not. But it has its uses for inputSTREAMS.

#5
kelvo_link

kelvo_link

    Newbie

  • Members
  • Pip
  • 4 posts
I agree with oxano
Scanner is a lot easier

^^ ,

hope you enjoy programming

Java is the best

#6
hawil

hawil

    Newbie

  • Members
  • PipPip
  • 26 posts
@oxano:
i researched about input streams and now i understand. bufferedreader is somewhat more complex than the scanner.

@kelvo_link:
i researched error handling, it is somewhat like a checker of some sort.. i thought it was really part of the code, it was just for checking errors.

i really agree! java the best for me!

thanks to your replies guys.

btw, how do i covert this line Char = string.charAt (4); to manual input? i mean the program will prompt the user to enter the index he/she wants to search. i can't seem to code it right. the compiler always needs an integer within that parenthesis..

Edited by hawil, 01 July 2010 - 06:06 AM.


#7
kelvo_link

kelvo_link

    Newbie

  • Members
  • Pip
  • 4 posts
ah you mean like these

string.charAt(Integer.parseInt(Input.nextLine()));

correct me if Im wrong I think these is what you are asking for.

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
string.charAt(Input.nextInt()); would be better as you won't have to manually parse the string to integer yourself and the program won't crash if you enter a "non-integer" i believe.

#9
hawil

hawil

    Newbie

  • Members
  • PipPip
  • 26 posts
wow! thanks guys.. i'll be trying both of the suggestions.. great to be asking with pro's! ^^,

i'll just edit this so it will not be a double post.

i tried the given line and it worked well. i'm just wondering why do i still hit enter for the 2nd time to proceed after entering the index?

Edited by hawil, 05 July 2010 - 06:13 AM.


#10
marjie

marjie

    Newbie

  • Members
  • Pip
  • 6 posts
I agree with oxano
Scanner is a lot easier

,

hope you enjoy programming

Java is the best

Read more: charAt function