#arraylist first how to get first letter(begginer)
1 messages Β· Page 1 of 1 (latest)
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
friends is an ArrayList.
friends.get(..) method wants an index (Integer), not a String.
do u want to show all friends that start with M?
please be a bit more detailed. perhapa give an example
yes iam trying to show all friends that start with M can u help me how to do that
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
please be more detailed how to do that iam just a begginer
can u run me threw all the steps because iam finding this hard to understand please and thank u
for (String friend : friends) {
System.out.println(friend);
}
this will iterate all friends and print them
thank u
now. how to get the first character of a string?
yes how do i do that
no idea?
Try π
do u have the docs of String open? skim through it and see if there's any method that looks useful
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";
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
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
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.
yes it showed all my friends sonow i make a char
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 π
soo...?
so
have u opened the docs to String and skimmed through the methods? yes or no?
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
so. please list me all methods shown in the doc that return char
one of them must be the one u need
π
charcter
declaration: module: java.base, package: java.lang, class: String
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
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
Send code as text
CODE HERE
and if you want to take a screenshot use the snipping tool
not your phone
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.
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.
you are not doing what people are asking you to do at all
you are just ignoring it and do your own thing it seems
that way helpers will just leave your convo