If statement not running: any ideas? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I am trying to compare the third number to the last number and the fourth number to the fifth number. The if statement isn't running at all any help would be appreciated.
import java.util.*;
class Problem2{
public static void main (String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter a number between 100000 and 999996: ");
int m = s.nextInt();
if(m < 100000 || m > 999996){
System.out.println("Out of range!");
}
else{
String j = Integer.toString(m);
for(int i=2;i<6,i++){
System.out.println(j.charAt(i));
if(j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2)){
System.out.println("Works!");
}
}
}
}
}

You are incrementing the value, not the index of the character.
Change this:
if (j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2))
To this:
if (j.charAt(i) == j.charAt(i+3) && j.charAt(i+1) == j.charAt(i+2))
// increment indexes --^ --^ --^

Move the additions inside of the charAt() calls.
if(j.charAt(i) == (j.charAt(i+3))&& (j.charAt(i+1)) == (j.charAt(i+2)))

Related

Cant print the value of scan.nextIn(); [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
I'm trying to delete the user integer input from an array. However, I can't get the value of the input.
while(keepGoing)
{
while (!scan.hasNextInt() )
{
scan.next();
System.out.print('\n'+"Choose a valid number: ");
}
int unitA = scan.nextInt();
if (unitA < 1)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else if (unitA > 14)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else
lengthValue.remove(unitA);
scan.close();
keepGoing = false;
}
//lengthValue.remove(int unitA);
System.out.println(unitA);
In my opinion, you forget to press "Enter" after entering input. A scanner can read the input if only you press the "Enter" key. Your solution seems correct to me. I was able to properly run it on my PC.
You can find a similar question here:
How to use Scanner to accept only valid int as input

Don't know why my while loop doesn't work (Java) ..? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
The purpose of my code is to enter a pin, and it'll check if it's right or not. If it isn't, the question will loop.
For some reason, my code doesn't loop properly, and a lot of the code is underlined. Specifically the while loop itself and the second JOptionPane
// package loop;
import javax.swing.JOptionPane;
public class loop {
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin);{
maybePin = JOptionPane.showInputDialog("Please enter the PIN");
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}
}
while(correctPin != sMaybePin); <--
Look at that ; that terminates the loop right there. You need to remove that.
You never update sMabyPin which is the variable you are checking against. If you do what #John and #ANS suggested you'll be stuck in an infinite loop.
Remove the ; after the while statement and correct set the value of the variable sMaybePin to the input vlaue and ot works
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin){
sMaybePin = Integer.parseInt(JOptionPane.showInputDialog("Please enter the PIN"));
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}

Why isn't this recursion working in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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 am trying to calculate the factorial of the integer user inputs, but it does not return anything. Why?
Thanks.
import java.util.Scanner;
`class App {
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
int recurse = factorial(n - 1);
int result = n * recurse;
return result;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer to calculate its factorial: ");
int users = input.nextInt();
factorial(users);
}
}
The problem in your code is that you are have not giving a print statement for displaying factorial of the number entered. Just returning a value will not print it. If you are working in BlueJ environment, you can only use the code by directly executing the method factorial. Thank You.
The problem in your code is that , you are have not given a print statement for >displaying factorial of the number entered.

"letter finder" code to find the number of letters in a word [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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
import java.util.Scanner;
public class lettercounter {
public static void main(String[] args) {
Scanner lettercounter = new Scanner(System.in);
System.out.println("Enter your sentence > ");
String sentence = lettercounter.nextLine();
int length = sentence.length();
System.out.println("what letter do you seek? > ");
String letters = lettercounter.nextLine();
char letter = letters.charAt(0);
int counter = 0;
for (int i = 0; i < length + 1; i++) {
char digit = sentence.charAt(i);
if (Character.toString(digit).matches(Character.toString(letter))) {
counter++;
}
}
System.out.println("Number of '" + letter + "'s found >" + counter + "");
}
}
whats wrong with the code?? i tried doing this but there seems to be an error when i run it: it says
xception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(Unknown Source)
at lettercounter.main(lettercounter.java:15)
i<length+1 should be i<length.
This fixes the problem because there are only length characters in the string, accessible via sentence.charAt(0) up to sentence.charAt(length - 1). You are trying to access one more character - sentence.charAt(length).

Why is this while-loop not working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
The following code compiles, but when I ran it, it didn't enter the while-loop. Why ?
while (Str1.equals(Str2)); {
count = 1;
while (count <= maxCount); {
System.out.print(something1);
count = count + 1; } }
Remove the semi colon, semicolon is added only for do-while.Here is your code:
while (Str1.equals(Str2))
{
count = 1;
while (count <= maxCount)
{
System.out.print(something1);
count = count + 1;
}
}

Categories