Why do-while dosen't respond to .equals [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 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'm setting up this do- while loop . Where do i need to correct this code
so when 000000 is given , the loop ends.
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
String am ;
{
do
{ System.out.println("give am number");
am = kb.next();
if (am.matches("[0-9]+") && am.length() <= 6)
{System.out.println("am = "+am);
{break;}}
else
{System.out.println("wrong try again");
am = kb.next();
}
} while(!"000000".equals(am));

Right now, you are checking for equality, not matching. "000000" is not equal to "[000000]", so the loop keeps going.
It's not completely clear what you want, but I think just ditching the square braces is probably the answer.

Related

Split function giving incorrect result in 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 1 year ago.
Improve this question
import java.util.*;
class Solution {
public static void main(String d[]) {
Scanner sc=new Scanner(System.in);
String s0=sc.next();
String p[]=s0.split(" ");
System.out.println(p.length);
}
}
I am using jdk version 8 and when i am giving input as "hello world java", it is printing length as 1 ,whereas it should print the length as 3,please help me in resolving this
Scanner.next() will get the input from the user till a space is encountered. For the input "hello world java" it only assign "hello"
If you want to include spaces also use Scanner.nextLine()

IndexOutOfBounds error, hangman game [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I'm making a quick hangman game, and came across an IndexOutOfBoundsException and was wondering why. I don't see the problem/how this error would come about.
It happens at this line:
array[index]+=c;
Any feedback is appreciated.
import java.io.IOException;
import java.util.Scanner;
public class Driver {
public static void main(String[]args) throws IOException {
Scanner console = new Scanner (System.in);
String[] phrase={"television"};
String[] array= new String[phrase.length];
int body =6;
while(array!=phrase) {
char c=(char)System.in.read();
int index= console.nextInt();
array[index]+=c;
if(array[index].charAt(index)==phrase[index].charAt(index)){
System.out.println("the new array");
}
}
}
}
There are many Issues with the code. few of them are below.
you are creating array of size "phrase.length" which will be of size 1, when i enter 2 for "console.nextInt();" it will throw index out of bound.
Array Equality check is wrong, you need to do something like
if( Arrays.equals(array1, array2) )

Cannot find symbol .hasNextInt [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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
if(args.length != 1){
System.out.println("Incorrect number of arguments");
return;
}
String inString = "";
Scanner s = new Scanner(new FileReader(args[0]));
while(s.hasNextInt){
int temp = s.nextInt();
inString += temp;
}
This is an excerpt of code from my main method. Code won't compile; the two errors are cannot find symbol (s.hasNextInt) and illegal start of type (while(s)). The other question I found here about .hasNextInt and cannot find symbol was someone trying to call .hasNextInt on a string instead of a scanner, and I can't figure out why my scanner can't run hasNextInt. I imported .util and .io.
Can someone help me figure out what I'm doing wrong?
You're missing the brackets () for the method call hasNextInt().
while(s.hasNextInt()){
int temp = s.nextInt();
inString += temp;
}

Can't create two int in for statement [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
Can you please help me make the following code work?
for(int a=0, b=0; a<101; b<102; a++; b++;) {
stuff
}
You got the initialization (first) part of the loop right.
The termination or condition (second) part of the loop should be evaluated to a boolean, so assuming you require an AND relation between the conditions on a and b, it becomes a<101 && b<102. You might want || (OR) instead, depending on your logic.
The increment (third) part of the loop should contain comma separated expressions (same as the initialization part which you already got right).
I also removed an extra ';' from the end.
for(int a=0, b=0; a<101 && b<102; a++, b++) { stuff }

Why assertions in Java is 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 8 years ago.
Improve this question
I tried to use assertions in java from an example put forth from a site but it is not throwing an Assertion Error even if it does not conform to the requirements.
Where am I doing wrong?
package Sources;
import java.util.Scanner;
public class MyAssertion
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age");
int age = scanner.nextInt();
assert age>=18:"Not Valid !!";
System.out.println("Age is -- "+age);
}
}
I have also used the following command :
java -ea -cp ./classes Sources.MyAssertion
But even if I enter the age as 33 it is not throwing an error . Why?
Any help is much appreciated :)
Assertions fire when the condition is not true. If you enter 33 then that is greater than equal to 18, so all is well.

Categories