Problem with adding strings to proper positions of an array (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 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.

Related

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.

How to print out a 2d array in a certain way? (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 6 years ago.
Improve this question
So here is an example text file of what I need to store into a 2d array, and print it out in certain ways.
the file contains two numbers followed by text, the numbers represent the number of rows and columns.
4 4
SOME
FILE
WITH
TEXT
So I already stored them into an appropriate 2d array, I am just not sure how to print it out to fit this pattern:
THEE
XTLM
EIIO
TWFS
so far to print out the 2d array: i have the code:
for(int i=0;i<row;i++)
{
for(int m=0;m<column;m++)
{
System.out.print(charArray[i][m])
}
System.out.println();
}
I know to flip it vertically, and horizontally, by manipulating the parameters of how it is printed, but how do I manipulate the parameters so that it will print like the above? THanks so much
The text file would look exactly like above, called txt1.txt.... and I am going to use 4 of them. IT looks exactly like the example listed above:
4 4
Some
File
With
Text
my goal is to just store them in a 2d array, and manipulate how I print them. I already know how to manipulate it in some ways but just not in the pattern also listed above. THanks guys! :)
You want to start with the last element in the last column, and go backwards by columns first. So all you need to do is change the direction and order of your loops:
for(int m=column - 1;m>=0;m--) {
for(int i=row - 1;i>=0;i--) {
System.out.print(charArray[i][m])
}
System.out.println();
}

Remove element from array, without reindexing [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 6 years ago.
Improve this question
I have the following issue:
I have got an "array" Like this:
[0] = This
[1] = is
[2] = my array
when "array.remove(0)" - the array is changing to
[0] = is
[1] = my array
but I don't want to lose their positions (index)...
what can I do here? they shall stay like:
[0] = null
[1] = is
[2] = my array
You can simply set array element of required position to null, it will do the job:
array[pos]=null;
Instead of calling remove(0), you can use arrayList.add(0, null). This will do the job for you.
You can't.
java arrays start at index zero and have consecutive indices (natural positive numbers).
There are two basic approaches:
replace the "hole" values by placeholder items.
A natural value with java would be null.
With strings you might also use an empty string.
Use a map
There you can apply any logic you like to the indices.

Digital (number) length in 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 7 years ago.
Improve this question
How to get the length of some number in Java?
Length of string is string.length(), but what is it for an integer?
I tried this:
int lengthNumber = (String.valueOf(maxNumber)).length();
But it always returns 1, for every number.
Try
Integer#toString().length();
For Example
Integer a=222;
int length=a.toString().length();
Output
3
When I ran this:
int Number = 100003;
int lengthNumber = (String.valueOf(Number)).length();
System.out.println(lengthNumber);
My output was 6, indicating that it works correctly.
Just make sure that your variables are declared properly.
This method will work if the above isn't working.
int x = 100003;
String y = "" + x;
Now you can use y.length(). Printing y gives 100003, and printing y.length() gives 6.
System.out.println(y);
System.out.println(y.length());

How to split spacing and | in 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 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"}

Categories