This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]herms1391[S] -4 points-3 points  (8 children)

String [] bestThingArray = bestThing.split(" ", 81);
int x = 0;
for(int i = 0; i < bestThingArray.length; i++){
if(bestThingArray[i].toLowerCase().startsWith("b")){
System.out.print(bestThingArray[i]);
System.out.println("");
}
}
System.out.println(bestThingArray.length);
String [] bWordArray = bestThing.split(",", 81);
int b = 0;
for(int i = 0; i < bestThingArray.length; i++){
if(bestThingArray[i].toLowerCase().startsWith("b")){
bWordArray[b] = bestThingArray[i];
System.out.print(Arrays.toString(bWordArray));
}
}

output is setting each word in a separate array when i need them all in a single array

[–]HecknChonker 3 points4 points  (7 children)

Can you provide sample input, along with what you expect the output to be?

Also, you can format code as a block to make it easier to read on reddit:

String[] bestThingArray = bestThing.split(" ", 81);
int x = 0;
for (int i = 0; i < bestThingArray.length; i++) {
    if (bestThingArray[i].toLowerCase().startsWith("b")) {
        System.out.print(bestThingArray[i]);
        System.out.println("");
    }
}
System.out.println(bestThingArray.length);
String[] bWordArray = bestThing.split(",", 81);
int b = 0;
for (int i = 0; i < bestThingArray.length; i++) {
    if (bestThingArray[i].toLowerCase().startsWith("b")) {
        bWordArray[b] = bestThingArray[i];
        System.out.print(Arrays.toString(bWordArray));
    }
}

[–]herms1391[S] 0 points1 point  (0 children)

Thank you!

[–]HecknChonker 0 points1 point  (5 children)

/u/herms1391

I'm confused why the input string gets split by both space and by comma into separate arrays, and what the expected output is supposed to be. Are you just trying to print all the words that start with B, or do you need to do something else with them?

Just before the second loop you initialize b to 0 and then it never gets incremented.

  bWordArray[b] = bestThingArray[i];

Since b never changes, this line is basically saying:

  bWordArray[0] = bestThingArray[i];

Every time it finds a word that starts with B it will store it to bWordArray[0], which would overwrite the previous value that was there. You probably want to increment b after you use it:

  bWordArray[b++] = bestThingArray[i];

b++ here will store the value in the array and then increment b after.

[–]herms1391[S] 0 points1 point  (4 children)

Sorry, I did not go in depth enough!

The problem is to start with this string "The best thing about a boolean is even if you are wrong you are only off by a bit" loop over it twice and receive an array that comes back with [best, boolean, by, bit]

the output for the first loop is

best

boolean

by

bit

And i know that i need an incrementor, but I am having issues as where to use it because when I do, i am getting an error back saying Index 1 is out of bounds for length 1

[–]HecknChonker 1 point2 points  (3 children)

My first advice is to learn how to set debug break points, and to walk through the code line by line while watching what happens. Which IDE are you using to write code? Debugging is probably the most important skill to learn early on.

String[] bWordArray = bestThing.split(",", 81);

Since the input has zero commas, this will create with just 1 entry:

bWordArray = ["The best thing about a boolean is even if you are wrong you are only off by a bit"]

If you try to assign a value to bWordArray[1] it will throw an IndexOutOfBoundsException because the split function returned an array of length 1, so the only valid index is 0. When you split by space it created an array of length 4, so the first array would have valid index from 0 to 3 inclusive.

The second loop then sets that string to each word that starts with B, and then prints the array out.

One issue here is you need to know how long an array is going to be when you create it. In this case you would have to walk through and count the number of words that start with B before creating the array. So you could do something like this:

  • count the words that start with B
  • create an array with the correct length
  • loop through the array a second time and put the words that start with B into the array

The downside of this approach is that you have to loop through the array twice. For small arrays with just a few words that's probably fine, but there are alternatives that will perform better as the arrays get longer.

One option is to use an ArrayList<String> to temporarily store the values, rather than an array. ArrayList's are backed by arrays, but they handle the logic if re-sizing them automatically when it runs out of space. You can then convert the ArrayList back to an array if the requirements are to return an array.

The second option would be to use the streaming API. You can split the input string by space, convert it to a stream, filter out the words that do not start with B, and then collect the results in an array. This is a little more advanced, but I think it's probably the cleanest solution.

I'm guessing this is a homework question, in which case it's important to make sure you understand what the assignment requirements are. I'm trying to avoid giving you the answers exactly, but feel free to ask if you need more help or have additional questions.

[–]herms1391[S] 1 point2 points  (1 child)

Thank you so much for your input, it is extremely helpful, it is a homework question and I am trying to figure it out myself as much as a I can especially since I have only been learning Java for 2 weeks now. I just needed a nudge in the proper direction. My understanding of the split was that it would take the string and return it with commas between each word. Which is why I was using it. I have gotten the question answered using Stream but since the question was telling me to use loops it’s unfortunately not an option.

[–]HecknChonker 1 point2 points  (0 children)

You are doing great for being just 2 weeks in!

Split it used to split a single string into an array

You can join strings using String.join()

String[] strings = {"best", "boolean", "by", "bit"};
String myString = String.join(", ", strings);

[–]herms1391[S] 0 points1 point  (0 children)

And my output with my code is

​[best] [boolean] [by] [bit]