#arraylist first how to get first letter(begginer)

1 messages Β· Page 1 of 1 (latest)

jovial jackalBOT
#

<@&987246746478460948> please have a look, thanks.

sleek saddle
#

ur syntax is all over

#

sth like friends.add()0 makes no sense to java

#

u probably meant just friends.add("John")

#

also not sure what that if is supoosed to do. it doesnt make too much sense like that

glossy stream
#

friends is an ArrayList.
friends.get(..) method wants an index (Integer), not a String.

sleek saddle
#

do u want to show all friends that start with M?

#

please be a bit more detailed. perhapa give an example

delicate marsh
#

yes iam trying to show all friends that start with M can u help me how to do that

sleek saddle
#

u should step back. first write code that shows all friends

#

execute it, test it

#

only after uve confirmed that it works, proceed to filter them

#

then modify it to print the firsr character of their name

#

run it, test it

#

after that works, add an if to filter

delicate marsh
#

please be more detailed how to do that iam just a begginer

sleek saddle
#

which part?

#

printing all friends?

delicate marsh
#

can u run me threw all the steps because iam finding this hard to understand please and thank u

sleek saddle
#
for (String friend : friends) {
    System.out.println(friend);
}
#

this will iterate all friends and print them

delicate marsh
#

thank u

sleek saddle
#

now. how to get the first character of a string?

delicate marsh
#

yes how do i do that

sleek saddle
#

no idea?

glossy stream
#

Try πŸ™‚

sleek saddle
#

do u have the docs of String open? skim through it and see if there's any method that looks useful

tacit jewelBOT
#
public final class String
  implements Serializable,
             Comparable<String>,
             CharSequence,
             Constable,
             ConstantDesc

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

String str = "abc";
sleek saddle
#

click that link and it takes u to its doc

#

which lists all methods available for strings

#

including one that allows u to access its characters

delicate marsh
#

yes i done that and i ran the system and it shows all my friends in the brackets but how do i program to show me a person only by the first letter of the name

glossy stream
#

You have done

for (String friend : friends) {
    System.out.println(friend);
}

Which prints out all the friends right?
Next step is to print the first char in a friend.

delicate marsh
sleek saddle
#

please do what ive said

#

open the docs for String that i just shared

#

skrim through the list of available methods

#

see if u can find sth that looks useful

#

u kinda need to learn how to help urself on these smaller issues, so we are showing u how to do that instead of just throwing the solution for copy pasta at u πŸ™‚

sleek saddle
#

soo...?

delicate marsh
#

so

sleek saddle
#

have u opened the docs to String and skimmed through the methods? yes or no?

delicate marsh
#

yes and seen all of it but iam a begginer so i wouldnt understand much and ran and test it and it showed me the names i put down but want i want to do is to program for it if i put down M to show me Martin,Marcus because they start with m

sleek saddle
#

so. please list me all methods shown in the doc that return char

#

one of them must be the one u need

#

πŸ™‚

delicate marsh
#

charcter

carmine seal
sleek saddle
# delicate marsh charcter

im sorry but u have to write full sentences and generally be more detailed. its very hard to help u if u dont communicate

#

we can help u write it urself. we wont be able to just give u a solution.
but that requires that u cooperate a bit and explain what ur doing and what exactly is unclear to you, as well as doing what people ask you to do

delicate marsh
#

Yes i tried and i didnt manage to do exactly what i want but i did true or false if there is in arraylist it runs just how it’s supposed but i will show u to make sure i would appreciate if u look at it

round glacier
#

CODE HERE

#

and if you want to take a screenshot use the snipping tool

#

not your phone

jovial jackalBOT
#

Please use this format for posting code:

```java
// Example java program
int value = 5;
System.out.println(value);
```

Which results in:

// Example java program
int value = 5;
System.out.println(value);

For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.

glossy stream
# delicate marsh

The following code is not operating on your ArrayList.

System.out.println(Character.isLetter('M'));
System.out.println(Character.isLetter('J'));

As it is now you have:

Created an ArrayList of name of friends

ArrayList<String> friends = new ArrayList<>();
friends.add("martin");
friends.add("john");
friends.add("Thomas");
friends.add("Marcus");

You print if character M is a letter:
System.out.println(Character.isLetter('M'));

You print if character J is a letter:
System.out.println(Character.isLetter('J'));

Now the loop we suggested previously operates on the ArrayList

for (String friend : friends) {
    System.out.println(friend);
}

It goes through each friend in your ArrayList and prints out the whole String.
Your next step is to expand this for-loop, instead of printing the whole friend, you want to operate on the String to print the first letter. The String library contains a lot of operations, you just have to find the right one.

sleek saddle
#

you are just ignoring it and do your own thing it seems

#

that way helpers will just leave your convo