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.
Related
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()
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.
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
I started learning core java from youtube but while writing same code in eclipse i am getting syntax error .
package Javatut;
public class ifelse {
public static void main(String[] args) {
int test = 10;
if (test == 10);{
System.out.println("yes");
}
else{
System.out.println("No");
}
}
}
I suggest you use an IDE as this will help you find such issues. After using the auto-formatter, I can see this warning in IntelliJ
You can see that the formatting is not what you think it should be as you have an extra ; in your code.
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
When my application compiles, it will not accept the input for roomNum on the same line in which it asks, "Please enter a room to search for:"
System.out.println();
if(roomNum < 0);
{
System.out.println("Please enter a room to search for: ");
roomNum = input.nextInt();
}
If I just use next instead of nextInt, it doesn't compile correctly.
The code above works, but will not accept the input on the same line which is the functionality I need.
Two things: Remove semi-colon after if condition and use System.out.print() instead if you want input on the same line.
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 9 years ago.
Improve this question
My issue is when I run the code in eclipse it just never seems to do anything at the bottom. The other day I made some almost identical programs and they worked fine. The only thing I can assume is going on is it never gets the input from scanner so it never prints the line so the program just keeps on running.
import java.util.Scanner;
public class testrun {
public static void main(String args[]){
String name;
Scanner get = new Scanner(System.in);
name = get.nextLine();
System.out.println(name);
}
}
You may need to type something and hit that ENTER key. The Scanner's nextLine() is waiting for input.
:)
It happens to us all.
The program works good and asks for input.
Keep some sop to print some helping statements and make it user friendly.
import java.util.Scanner;
public class Testrun {
public static void main(String args[]){
String name;
System.out.println("Please Enter : ");
Scanner get = new Scanner(System.in);
name = get.nextLine();
System.out.println(name);
}
}