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 am a beginner in Java programming.
I was a making a program to find if the entered word is a pallindrome or not can someone please tell me the logic i should use to make the given program?
boolean isPalindrome(String input) {
for (int i=0; i < input.length() / 2; ++i) {
if (input.charAt(i) != input.charAt(input.length() - i - 1)) {
return false;
}
}
return true;
}
This solution is self explanatory, the only edge case requiring explanation is what happens for words with an odd number of letters. For an input containing an odd number of letters, the middle element will not be touched by the loop, which is OK because it has no effect on whether the input is a palindrome.
Related
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 2 years ago.
Improve this question
I am currently practicing with my coding skills and one of the challenges I ran into was finding the first duplicate value produced in an array. I had solve the challenge, but not in a timely manner. I looked at the solution for the problem and ran into this solution and wanted help in understanding how it exactly works. I have the solution commented in the areas that I understand and what exactly is the purpose of that block of code, but I would like help in understanding the if-statement why it works the way it does.
int firstDuplicate(int[] a) {
for(int i=0;i<a.length;i++)
//Checks ???????????
if(a[Math.abs(a[i])-1]<0)
return Math.abs(a[i]);
//Make the checked value negative
else{
a[Math.abs(a[i])-1]=-a[Math.abs(a[i])-1];
}
//If there are no duplicates it returns -1
return -1;
}
Constraints:
1 ≤ a.length ≤ 105,
1 ≤ a[i] ≤ a.length.
Welcome to SO. I will not give you the exact answer but instead provide you with tools for you to figure it out.
In order for you to figure out this code, you need to debug it. Here are some ways you could go about it.
Set a breakpoint just prior to calling the function (look up how to do this for your IDE), thereafter step into your code line-by-line.
You could use a combination of temporary variables and System.out.println() statements. Looking into your code, break it down into modular bits that you can track. For instance you could re-write the expression inside the if statement as
int currentElementAbsolute = Math.abs(a[i]);
System.out.println(currentElementAbsolute);
int difference = currentElementAbsolute - 1;
System.out.println(difference);
int element = a[difference]
System.out.println(element);
if (element < 0)
{
return Math.abs(a[i]);
}
As you can see, for each operation/line, I print out the current value thus keeping track of events. Practice this and re-write the else part
PS: Upon completion you will realise this method fails to capture all duplicates when certain type of numbers are used. Happy Hunting :)
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
So I'm programming a Go Fish game for a project of mine, and I've been at this for a while. Currently I'm trying to check if the player/computer has a full book of a certain number. I've based the whole game off of arrays, and just need to check if the array contains four instances of a given element.
I'm not sure about the specifics but this should get you started.
int count = 0;
for(int i = 0; i < YOURARRAY.length; i++)
{
if( //check to see if the element is the type youre counting)
{
count++;
}
}
if(count == 4)
{
//there are 4 instances
}
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
I'm trying to make a program that takes the string "1d8" and make the program identify it as int i = (Int) ((Math.Random()*8)+1) one time. It would also be nice if I could make it identify "10d8" to do something like :
for(int i = 1; i <= 10; i++){
int j += (int) ((Math.Random()*8)+1);
}
Thus returning basically the roll of 10 eight sided dice. So my question is how do I get the code to recognize the numbers on either side of the character d and make this work with whatever roll I do.
You can use split method of the String class.
String s = "10d8";
String[] numbers = s.split("d");
numbers[0] will have 10 and numbers[1] will have 8
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
I'm a novice programmer is looking to get help with a program I've been working on for hours now.
Anyway my issue happens to be with initializing my "sum" variable in a way so that it is not equal a number or become an input. (It's set to equal 0 in my program)
I also seem to be struggling with my while loop statement as I can't think of a condition in which I don't cause the program to terminate or cause a infinite loop.
Any help at this point would highly appreciated.
I'm assuming you are trying to compute sum of integers from 1 to n, where n is input from the user.
One of the simpler ways to do that is to use a for loop as below
for(int i = 1; i <= n; i++) {
sum = sum + i;
}
or using while loop
while(input > 0) {
sum = sum + input;
input = input - 1;
}
Alternatively, sum of first N natural numbers is given by formula n*(n+1)/2, so you might as well do
int sum = (n * (n+1))/2;
Ensure that n is a positive number > 0 through an if conditional
your While loop is going infinity as your input is never 0 until you give it.
So you have to deal with your input.
Possibly use or declare input=0 so that when your while loop goes for the execution for 2nd Time...It finds 0.
Hope this helps...Please share doubts if any.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I couldn't figure out why my code is printing forever. I tried a block (will show where I had it originally) but I want it to continue after printing the "help". My code goes as follows. I think it's the for loop... but I haven't touched much on it.
boolean yes = true;
while(yes) {
if(str1.equalsIgnoreCase("Exit")) {
System.exit(0);
} else if (str1.equalsIgnoreCase("Help")) {
for(int g = 0; g < 2; g++) {
System.out.println(" Accepted commands:\n exit\n help\n load [filename]\n students\n search [partial name]\n assignments\n grades\n student [student name]\n assignment [assignment name]\n");
}
//I added the break here and it did print out once but I did not want it to end the program. With the break I did not need the for statement.
} else if (str1.equalsIgnoreCase("Load")) {
}
The value of str1 is never changed inside the loop. Perhaps you are missing some statement that takes new input from the user.
First off
boolean yes = true;
while(yes) {
You are never setting yes to false
Secondly
if(str1.equalsIgnoreCase("Exit")) {
System.exit(0);
str1 is never set inside the loop, even to "Exit"
yes is set to true and you never changed it in the loop
read str1 inside the loop:
DataInputStream in = new DataInputStream(System.in);
str1=in.readLine();
while(true) {
str1=in.readLine();