why does the loop on this program time out? [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 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.

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?

Why have to use value.length? [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 5 years ago.
Improve this question
guys.....Please help!!!
This is the picture of a textbook page that I'm reading
So I'm reading this textbook which is totally horrible, they just give codes out without any explanation.....And this is my first class in Java, I've never coded in any language before......So like a textbook without explanation just totally....you know....give me a super hard time....
Back to topic, in the picture, there are 2 sets of codes, A and B.....I understand B......But I do not get why in A, it used value.length instead of inputs.length? Isn't the array name in this code is inputs??? Is there any specific reason has to use value.length instead of array name.length???
the book clearly has an error, don't worry, should be inputs instead values, I say it with confidence because no variable values was ever initiated

Struggling to fix infinite loop [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 6 years ago.
Improve this question
I'm new to JUnit testing and I'm struggling to test a method that will search for a specific vehicle registration and output it that it is found
System.out.println("getSpecificVehicle");
CVMQueue instance = new CVMQueue();
VehicleNode newVehicle = new VehicleNode("YBZ5484", "Car", "Ire", 3, 2.2);
instance.enqueue(newVehicle);
String expResult = "YBZ5484";
String result = instance.getSpecificVehicle("YBZ5484");
assertEquals(expResult, result);
This is my code, It keeps looping in the Console.
How can I fix this?
Check your code; if you improve the formatting you find
while (temp != null) {
...
if (reg.equalsIgnoreCase(temp.getRegNum())) {
...
}
}
Your loop is never changing temp. So, why should it ever stop looping upon being entered? So, obviously your problem is that you missed that loop-closing brace; as you put that assignment to temp after that brace.
But the real take-aways here:
Formatting matters. Maybe, if you had put more diligence in writing down your code, you would have spotted this yourself much earlier. And as Gaket points out correctly: any sane IDE (or coding editor) probably has some "auto format" functionality, that well, formats your source code automatically.
Leading to: code readability matters even more. For example, there is the "Single layer of abstraction" principle; which would have told you to not just put that complete if into that loop; instead you would have created a method to do that work. And again, it would have been so much easier to spot this simple problem.
Learn about using a debugger. You see, the real power of unit tests is that they make it also so easy to isolate bugs: you put a breakpoint somewhere; and run your test in the debugger; and you can directly observe what is going on.

stringoutofboundexception in java (due to long string) [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
I am getting a StringIndexOutOfBoundsException for a long string. Basically the program is reading string from text file and for some reason i am getting error when I use long strings. For e.g. if use a=b+c or a=b or a=b+c-d*a this all works but when i put long strings such as "programming" or "javatutorial" this gives me a StringIndexOutOfBoundsException. At first I thought this was due to the fact that I am not checking whether or not x is empty but that is not the case this is occurring due to the length of the string itself. I would appreciate if someone could help.
while (scan.hasNext()) {
String x = scan.nextLine();
try
{
if(!x.isEmpty())
{
char ch=x.charAt(0);
s=String.valueOf(ch);
}
}
catch(StringIndexOutOfBoundsException siobe)
{
System.out.println("invalid input");
}
}
With the partial code you provided, all we can tell you is that the exception is being thrown because you are using the method:
String.charAt(int index)
on a String that does not have a character at the 'index'-th position.
For example, if String word = "cat", then word.charAt(8) would throw an exception because 'cat' only has three characters.
Search through your code for all the places that you used the charAt(int index) method and test that 'index' is indeed less than String.length(). Your error shows an argument of '11', so you can narrow your search to the location where you called the charAt() method with an index of 11. (This may be inside a loop).

java boolean array and turning every third value to false [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 8 years ago.
Improve this question
The Boolean array is initialized to true. I need to know how to turn every third value to false and also going through the array over and over. It is essentially duck duck goose without the randomness.
This sounds like a beginners programming exercise, so I'm going to just give you a hint or two:
Go back to your textbook, lecture notes, tutorial and reread the stuff on for loops. Focus on the old style ones.
Think about how to write a for loop that steps through integer index values in the pattern that you require.
Re "... going through the array over and over" - not sure what you mean, but maybe the hint for this is to think about using nested loops; i.e. a loop inside another loop.
But the most important advice is to try and work this out for yourself.
Well I'm really not sure what you mean by going through the array over and over but the following code will turn every third value to false.
for (int i = 0; i < myVar.length; i++) {
if (i % 3 == 0) {
myVar[i] = false;
}
}
Edit: Oops someone beat me to it while I was typing lol.

Categories