I am getting NoSuchElementException while executing my code on various online IDE. I am taking STDIN using Scanner object.
import java.util.*;
import java.util.Scanner;
class Maze{
public static int numberLength(int k){
int ln = (int) (Math.log10(k) + 1);
return ln;
}
public static void main(String [] args){
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int s = numberLength(n);
System.out.println(s);
}
}
You have to check Custom input and write your input, then compile
You have got to give inputs for your primitives which you have declared in the main.
Related
This question already has answers here:
Java Multiple Scanners
(3 answers)
Closed 3 years ago.
I am trying to read input in main function as well as different functions, for that, I am creating two different Scanner and it is giving me an error but if I take input only in main function and pass the value in the different function it does not gives error then, what I am missing here?. I am a beginner in java.
import java.util.Scanner;
public class abc{
public static void fun(){
Scanner read = new Scanner(System.in);
int a,b,c;
a=read.nextInt();
b=read.nextInt();
c=read.nextInt();
// some code
}
public static void main(String[] args){
Scanner read=new Scanner(System.in);
int t=read.nextInt();
while(t>0){
fun();
t--;
}
}
}
Why do you just don't do like this:-
import java.util.Scanner;
public class abc{
public static void fun(int a, int b, int c){
Scanner read = new Scanner(System.in);
// some code
}
public static void main(String[] args){
Scanner read=new Scanner(System.in);
int t=read.nextInt();
Object o = new Object();
int a, b, c;
while(t>0){
a=read.nextInt();
b=read.nextInt();
c=read.nextInt();
ob.fun(a, b, c);
t--;
}
}
}
This will probably give you no error. If you don't want to do this, you can simply declare the Scanner object in the class and not in a method and try to use it.
I do not think it correct to create two Scanner for the same System.in. Why don't use the single one:
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int t = scan.nextInt();
while (t > 0) {
fun(scan);
t--;
}
}
}
public static void fun(Scanner scan) {
int a, b, c;
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
// some code
}
How to console input data in java class(not main class). The input methods work in cpp but not in java. Is there a way to console from a class method. System.in.read and interger parse methods are not working.
Use Scanner with System.in
Scanner input = new Scanner(System.in);
int x = input.nextInt();
String t = input.next();
like wise
Try following code
import java.util.Scanner;
public class Demo {
public static void main (String [] args){
A a = new A();
a.getValue();
}
}
class A {
public void getValue(){
Scanner input = new Scanner(System.in);
System.out.println("Enter number : ");
int x = input.nextInt();
System.out.println(x);
}
}
So I'm learning Java in class and I'm really loving it so far but its really hard to understand sometimes. Right now I'm trying to understand how methods work. My question is why my code is not working. I am trying to read in an integer from user input then square it.
Here is my code:
package freetime;
import java.util.Scanner;
public class methods {
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.println( " enter a number ");
int number = input.nextInt();
square(number);
}
public static int square(int number){
int num;
num = number * number;
return (num);
}
}
Let's say I input 5 on the console, the program immediately terminates and I cannot figure out why.
As mentioned by others, you don't print the value and the console will close as soon as the program ends. So you could try something like this
public class ScannerTest {
public static void main(String []args){
while(true){
Scanner input = new Scanner(System.in);
System.out.println( " enter a number (-1 to stop)");
int number = input.nextInt();
if(number == -1){
break;
}
int output = square(number);
System.out.println(output);
}
}
public static int square(int number){
int num;
num = number * number;
return (num);
}
}
This will print the result and loop ask for new input as long as you don't stop the program.
In Java, when main method comes to end and if there aren't any non-deamon threads running, the JVM ends. Your program came to an end without printing out the result of the square() call.
/*here is your solution :*/
import java.util.*;
import java.lang.*;
import java.io.*;
/*in java everything has to be in a class */
class SquareNumber
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner input = new Scanner(System.in);
System.out.println( " enter a number ");
int number = input.nextInt();
System.out.println(square(number));
/*something to print the squared number*/
}
public static int square(int number){
int num;
num = number * number;
return (num);
}
}
Your program is terminated because there is no other statement after square(number); statement. So your program executes square(...) method and after then it found end of main function so the program is terminated. To see some output you must print result of square(...) method.
package freetime;
import java.util.Scanner;
public class methods {
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.println( " enter a number ");
int number = input.nextInt();
int result=square(number);//executing square(...) method and store the returned value of square method to result variable
System.out.println("Square of "+number+" is : "+ result);//printing result
}
public static int square(int number){
int num;
num = number * number;
return (num);
}
}
I'm trying to make a simple calculator and need help with somthing
import java.util.*;
import javax.swing.*;
public class HiWorld {
public static void main(String[] args) {
System.out.println("Type 2 Numbers");
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
answer = input1 + input2;
System.out.println(answer);
}
It says that the operator + is undefined for the argument types java.util.scanner.
What you are doing there is creating two objects that read from standard input (your keyboard) and then, you are trying to add those objects together.
For start, use:
import java.util.*;
public class HiWorld {
public static void main(String[] args) {
System.out.println("Type 2 Numbers");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
int answer = x + y;
System.out.println(answer);
}
}
In Java you can only use the + operator for adding two numbers or concatenating two Strings. Not for doing anything with scanners.
import java.util.Scanner;
public class CHP4Ex
{
Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("enter a n: ");
int n = scan.nextInt();
int i=10;
while (i<n)
{
System.out.println(i);
i = i + 10;
}
}
}
Why am I getting this error? I'm basically writing a while loop that prints all positive numbers that are divisible by 10 and less than n. For example, if n is 100, enter 10 ... 90.
Put the Scanner class object inside the main function. Basically the problem is that your code violates the static feature. You cannot use non-static members inside a static function, main being static in your case. So it should be :
import java.util.Scanner;
public class CHP4Ex
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("enter a n: ");
int n = scan.nextInt();
int i=10;
while (i<n)
{
System.out.println(i);
i = i + 10;
}
}
}
You can't refer to non static variable in static context, so change
Scanner scan = new Scanner(System.in);
to
private static Scanner scan = new Scanner(System.in); It should work