How to split spacing and | in java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm having trouble splitting both at once right now as I am trying to split the code below
RCMP|Colour Demon|Magenta Boxer
NUPI|Number Picker|Random Numburrs
QUFI|Quick Fingers|Digitology
and here is my method that involves it. It worked for the others but because this one has spacing I am now having trouble
while (input.hasNext()) {
str = input.nextLine();
temp = str.split("\\s|\\|")
System.out.println(temp[0]) // should print RCMP
// Array out of bounds error
System.out.println(temp[1]) // should print COLOUR
Game game = new Game(temp[0],(temp[1]+temp[2]),(temp[3] + temp[4]))
}
the intended output that I want is something like this or better
RCMP- temp[0]
Colour - temp[1]
Demon - temp[2]
Magenta - temp[3]
Boxer - temp[4]
It is so I can store them into the game
When i attempt to print temp[1] which should be Colour it gives an array out of bounds error instead

Use
scan.nextLine()
To get the string
And split using regex:
yourString.split("\\s|\\|");
for example the line: RCMP|Colour Demon|Magenta Boxer would make:
{"RCMP", "Colour", "Demon", "Magenta", "Boxer"}

Related

Java - Two-dimensional array string replace and formatting [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 days ago.
Improve this question
I am facing issues while attempting a task that involves converting 0s (avaible seat) to the letter "O" and 1s (occupied seat) to the letter "X" in 2-dimensional arrays of strings. I have three rows of varying lengths (12, 16, and 20) that are filled with 0s, but I can't seem to get the output to look like the following:
How it should print like
I have tried to understand by watching videos and reading documentation, but I am still unclear. Can someone please help?

Extracting values from String[] that has a pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I will use examples to explain this. I have two fields. Summary and Labels. An example of Labels would be Labels = " QWE,CD-YTO, YTOU" and Summary = "CD-YTO : Some random words".
Now my code first checks labels for any element that starts with "CD-" and if its present it, that's pretty much it. It takes that value, for example here it takes "CD-YTO". Now, in case labels does not have any element that starts with "CD-" then its supposed to check summary. That's where my problem lies.
The code i developed converts summary to an String[] by using .split(" ") and the just check for .contains("CD-"). but the code complexity increased and i was told to reduce it.
Summary can be like the following
"CD-YTO: Some random words"
"CD-RTY:Some random words"
"CD-RTUU Some random words"
"cd-GFY - Some random words"
Now, in all the above cases, im supposed to get the "CD-***" part only. Using if loop for all these cases just increases the code complexity. Is there any way i can reduce that?
If your only concern is for loop, you can try to do the following:
String[] result = Arrays.stream(yourString.split(" "))
.filter(str -> str.contains("CD-"))
.toArray();

Problem with adding strings to proper positions of an array (java) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am working to make an Instagram type project where you can follow up to five people. For my follow method I tried to loop through a string array (length 5) and say if 'user getting followed' isn't on the list of followers, add that person to the array. But when I do instead of displaying the array like this (when I call the Arrays.toString()):
follows: [user 1, user 2, user 3, user 4, user 5]
It displays the array like this:
follows: [user1user2user3user4user5, , , , ]
Here is my code I am talking about:
for (int i = 0; i < 5; i++) {
if(...) {
this.following[i] += gettingFollowed.getHandle();
/* getHandle() gets the name of the user getting followed and adds it to the string array, 'following' */
this.following[i] += gettingFollowed.getHandle();
The += instruction appends to a String - which is why you are seeing your output. You are appending all your users to the same index.
this.following[i] = gettingFollowed.getHandle();
Simply remove the '+' and you are now setting the value.
Still, without knowing what .getHandle() does - this might not completely fix your code.

why does the loop on this program time out? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
it works fine for some strings, but for some reason it does not work for the last three. I see no difference to the type of strings being entered and I expect indx to evaluate to -1, but for some reason it does not on the last three strings. I don't understand why.
edit: problem solved. As you guys said, I was taking the substring of str instead of news within the loop. Sorry for such a simple mistake guys, I'm just starting to code and these are the details I need to pay attention to more. Also as I am working within the codingbat website there is no debugger, but I also want to highly recommend that website for other beginners. It will give you many example problems to begin coding on. Thanks again.
enter image description here
code:
public String stringYak(String str) {
int indx = str.indexOf("yak");
String news =str;
for(;indx!=-1;)
{
news = (str.substring(0,indx) + str.substring(indx+3,str.length()));
indx = news.indexOf("yak");
}
return news;
}
Because loop never breaks.
You are taking substring from str index from news inside the loop.
May be you want to take both substring and index from news.

Star pyramid with while loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am currently puzzled on making a star pyramid.
My product needs to be able to create a pyramid with a user-inputted # of rows. So if I say 3, the pyramid would look like:
*
***
*****
When I enter 3, my pyramid (based on my code below) looks like:
*
***
*****
I'm having trouble making the spaces get deducted when going down a row. I seem to have other parts down accurately, so my question is, how do I deduct spaces for each following row by 1 after the initial amount of spaces? Could I get help on how to fix my code? (while using a while loop). Feel free to comment on any other parts of my code if it seems inaccurate though.
At the moment this is what my code looks like:
import java.util.Scanner;
public class AstPyramid{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("How tall do you want your pyramid to be? Rows: ");
int rowCounter = keyboard.nextInt();
int baseLength = 1;
int starsInRow = 1;
while (baseLength <= rowCounter){
int starCounter = 1;
int whiteSpace = rowCounter/2;
while (whiteSpace >= 0){
System.out.print(" ");
whiteSpace--;
}
while (starCounter <= starsInRow){
System.out.print("*");
starCounter++;
}
System.out.println();
baseLength++;
starsInRow=starsInRow+2;
}
}
}
Update: I updated this question many times and already have received an answer pertaining to it. Could this question be un-held now? I'm not sure if editing this post notifies anybody, but it is worth another try.
In your code, the first inner-loop that you are using to print the white space, always runs for rowCounter / 2 times. That is the problem. It should ideally start at that value, and keep decreasing for each iteration of the outer loop.
You could have a spacesPerRow variable and assign it an initial value of rowCounter / 2, outside the outer loop. Then on each outer-loop iteration, you can decrease it. And use this spacesPerRow in your first inner loop to print the spaces.
Your spacesPerRow should be decremented once for each iteration of the outer loop. An ideal place would be right after your first inner loop finishes.
To moderators & question down-voters:
I can't add comments, so I'll add my thoughts here. The first few lines of the question precisely mention the problem: "I want to do X, but my code is doing Y". And the OP is not able to figure out why.
I also believe the code doesn't include a lot of unnecessary parts either.
I am wondering why this is considered off-topic, put on hold, and has two down-votes.

Categories