How to split string array? - java

It worked but it took the last song settext for all
for (int i = 1; i < items.length; i++) {
String SongsName = items[i];
String [] abcd = SongsName.split("_");
if(abcd[0] != null)
{
txtTitle.setText(abcd[0]);
txt2.setText(abcd[1]);
}
else
{
txtTitle.setText("Zero");
}
}
How does it show each line? Thanks in advance

After splitting the string, you end up with an array of song names. That array has an undefined number of items inside so you cannot just reference abcd[0] and abcd[1] and expect to see all songs. You need another loop that will iterate through all your song names in variable abcd.
Maybe I did not understand properly though, if variable abcd contains just one song name and you are just splitting to get the different pices of information for that song, then here is what is happening: you are looping through all the songs but you are overwriting txtTitle and txt2 everytime, so in the end, you end up with only the last one showing.

Related

Unable to obtain values if for loop placed in a different line of code

I'm tasked to take the words out of a txt file and then eliminate the duplicates and print out the rest. However there seems to be something weird going on when I place the for loop used to print out the array of words that are taken from the txt file.
When I do
for (String word:arr)
{
words = word.split(" ");
for (int i = 0; i < words.length; i++)
{
// Printing the elements of String array
System.out.print(words[i] + " ");
}
}
where, arr is an array of string; filled with sentences from the text file , and words is the individual words of the text file stored in array of strings, printing the array words will give what it is supposed to,
however when I do
for (String word:arr)
{
words = word.split(" ");
}// not nested so happens seperately
for (int i = 0; i < words.length; i++)
{
// Printing the elements of String array
System.out.print(words[i] + " ");
}
I only obtain 4 words out of the hundreds that are stored in the txt file.
Can someone help explain this? Thanks in advance!
In the second case you are processing outside of the first for, and you process just the last String that is in your array. Words variable gets overwritten in each itteration. My suggestion is to learn how to debug, it will greatly help you to learn.

Search For String in Multidimensional Array

I am trying to find a contact by searching the first two names of the array below and then update the phone number associated with the contact. In the coding I've provided, I can find the first name of the contact (strFirstName) in the outer loop but can't verify that it is associated with the appropriate last name (strLastName). Even tho in the array provided there are no duplicates of first or last name, I want my coding to be able to match the exact record.
After I find the appropriate record, I the need to prompt the user for the new phone number. I believe I can figure this part, but I'm open to ideas to accomplish this.
numContacts = the numbers of rows in the array
String [][] contactsArray = {
{"Emily","Watson","913-555-0001"},
{"Madison","Jacobs","913-555-0002"},
{"Joshua","Cooper","913-555-0003"},
{"Brandon","Alexander","913-555-0004"},
{"Emma","Miller","913-555-0005"},
{"Daniel","Ward","913-555-0006"},
{"Olivia","Davis","913-555-0007"},
{"Isaac","Torres","913-555-0008"},
{"Austin","Morris","913-555-0009"}
public static void updateContact(Scanner scanner, String[][] contactsArray, int numContacts) {
System.out.println("Updating contact");
System.out.print("Enter first and last name: ");
String strFirstName = scanner.next();
String strLastName = scanner.next();
for (int i=0; i < numContacts; i++){
System.out.println(i);
if (contactsArray[i][0].equals(strFirstName) ) {
for (int j = 0; j < 3;j++) {
System.out.println(j);
if (contactsArray[1][j].equals(strLastName) ) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
}
}
Appreciate all the help resolving this in advance.
I feel you are close to the solution. The string comparison with the last name is unfortunately incorrect.
In fact, you are doing contactsArray[i][0] for firstname, which is correct. However, you are doing contactsArray[1][j] for the lastname, which is incorrect. Maybe contactsArray[i][1] is more correct.
Then you could ask yourself if you really need your second loop? You actually just want to find a record given the first and lastname. Therefore, you only need one loop to iterate over your "records".
Finally, you should break out of your loop if the record was actually found, and print "yes". If none was found after the loop, you should print "no".

Getting only the first name in an array

I have an array list like this: ArrayList names = new ArrayList<>(); that stores people's first and last names when they are entered in different textbooks.
So when prompted to Joe Biden would be element number 1 then Barack Obama would be element number 2 in the array list. My question is that if it is possible to only get their first names like Joe from the array without getting Biden also?
Yes you could just do
names.get(0).split("\\s+")[0] //this would get you "Joe"
to get last name possibly do
names.get(0).split("\\s+")[1] //this would get you "Biden"
This way completely relies on the fact that you have a space in between their first and last name. and obviously edit the 0 to whatever index you would like.
Each element will be sitting in the ArrayList as a String object.
You could use the Str.split() to split it into a array and get the last name.
Let's say your ArrayList
String str = names.get(0); //Joe Biden
String[] arr = str.split(" "); //this will create Array of String objects
System.out.println(arr[1]); will print Biden
However be careful using this method, it'll not work with people with 3 names or one name. People with one names will cause an ArrayIndexOutOfBoundsException. People with more than one name will print their last name incorrectly.
However you can overcome this by doing,
int arrLength = arr.length;
if(arrLength > 0) {
System.out.println(arr[arrLength - 1]); //this will always print the last name, if the name isn't empty
}

I want to print an element from an array

in my program I want the user to be able to print an element from an array. This is how far I've and I can't think of what to put next?
public void viewClub() {
System.out.println("Please enter the name of the country whose details you would like to see");
String Name = input.next();
for (int i = 0; i < countryList.size(); i++) {
Country x = countryList.get(i);
if (Name.equalsIgnoreCase(x.getName())) {
}
You anyways have x.getName in which x points to ith element of the array. So even if you just do a sys out for
x.getName()
you will get the value.
Hope this be of some help
Happy Learning :)
You want to output the details of a country that the user inputs. To do this, you will need a function in your country class that returns a string containing the details of that country:
System.out.println(x.getCountryDetails());
You don't want to output the name, because the user already knows that.
After you find the country, you should break out of your loop, to stop further processing:
for (int i = 0; i < countryList.size(); i++)
{
if (name.equalsIgnoreCase(countryList.get(i).getname()))
{
System.out.println(countryList.get(i).getCountryDetails());
break; //Exit the for loop because you found the specified country
}
}
firstly you take a array in your program, then by using buffered input streams or any other take the input from the user, store it into the array and then print the array index which contain the values

Remove Null elements from a (String) Array in Java

Hey guys, I'm new to Java (well, 3/4 of a year spent on it).
So I don't know much about it, I can do basic things, but the advanced concepts have not been explained to me, and there is so much to learn! So please go a little but easy on me...
Ok, so I have this project where I need to read lines of text from a file into an array but only those which meet specific conditions. Now, I read the lines into the array, and then skip out on all of those which don't meet the criteria. I use a for loop for this. This is fine, but then when I print out my array (required) null values crop up all over the place where I skipped out on the words.
How would I remove the null elements specifically? I have tried looking everywhere, but the explanations have gone way over my head!
Here is the code that I have to deal with the arrays specifically: (scanf is the scanner, created a few lines ago):
//create string array and re-open file
scanf = new Scanner(new File ("3letterWords.txt"));//re-open file
String words [] = new String [countLines];//word array
String read = "";//to read file
int consonant=0;//count consonants
int vowel=0;//count vowels
//scan words into array
for (int i=0; i<countLines; i++)
{
read=scanf.nextLine();
if (read.length()!=0)//skip blank lines
{
//add vowels
if (read.charAt(0)=='a'||read.charAt(0)=='e'||read.charAt(0)=='i'||read.charAt(0)=='o'||read.charAt(0)=='u')
{
if (read.charAt(2)=='a'||read.charAt(2)=='e'||read.charAt(2)=='i'||read.charAt(2)=='o'||read.charAt(2)=='u')
{
words[i]=read;
vowel++;
}
}
//add consonants
if (read.charAt(0)!='a'&&read.charAt(0)!='e'&&read.charAt(0)!='i'&&read.charAt(0)!='o'&&read.charAt(0)!='u')
{
if (read.charAt(2)!='a'&&read.charAt(2)!='e'&&read.charAt(2)!='i'&&read.charAt(2)!='o'&&read.charAt(2)!='u')
{
words[i]=read;
consonant++;
}
}
}//end if
//break out of loop when reached EOF
if (scanf.hasNext()==false)
break;
}//end for
//print data
System.out.println("There are "+vowel+" vowel words\nThere are "+consonant+" consonant words\nList of words: ");
for (int i=0; i<words.length; i++)
System.out.println(words[i]);
Thanks so much for any help received!
Just have a different counter for the words array and increment it only when you add a word:
int count = 0;
for (int i=0; i<countLines; i++) {
...
// in place of: words[i] = read;
words[count++] = read;
...
}
When printing the words, just loop from 0 to count.
Also, here's a simpler way of checking for a vowel/consonant. Instead of:
if (read.charAt(0)=='a'||read.charAt(0)=='e'||read.charAt(0)=='i'||read.charAt(0)=='o'||read.charAt(0)=='u')
you can do:
if ("aeiou".indexOf(read.charAt(0)) > -1)
Update: Say read.charAt(0) is some character x. The above line says look for that character in the string "aeiou". indexOf returns the position of the character if found or -1 otherwise. So anything > -1 means that x was one of the characters in "aeiou", in other words, x is a vowel.
public static String[] removeElements(String[] allElements) {
String[] _localAllElements = new String[allElements.length];
for(int i = 0; i < allElements.length; i++)
if(allElements[i] != null)
_localAllElements[i] = allElements[i];
return _localAllElements;
}

Categories

Resources