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);
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
How do you write a Java program that accepts number as input and based on the value entered, displays a message?
Like
"1" displays "Yes"
"2" displays "No"
The main thing you will want to look into is the Java Scanner. You can import it via import java.util.Scanner; To make a new Scanner which reads from the console you will write:
Scanner userInput = new Scanner(System.in);
Scanners are very versatile, so they are built to read from any input stream. This could be a file, a String, or many other things. Just like System.out is an output stream to the console, System.in is an input stream from the console.
For more information you can read the scanner documentation here.
Now with your scanner created to get a number input we can use the nextInt() method.
int input = userInput.nextInt();
Here is a simple program similar to what you described.
import java.util.Scanner;
public class ScannerInputTest{
public static void main(String[] args){
Scanner userInput = new Scanner(System.in);
System.out.print("Enter Input > ");
int input = userInput.nextInt();
if(input == 1){
System.out.println("Yes");
}else if(input == 2){
System.out.println("No");
}else{
System.out.println("I don't Know");
}
}
}
Since you are just starting out please try your best to read through documentation, and don't just copy and paste answers from here. Try to tinker and change things to get a better understanding.
If you are going to be making a lot of console programs, I highly recommend also looking into switch statements, as if else statements will eventually become very bloated the more branches you need.
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 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) )
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.