i tried to declare a new scanner, it works fine but only at the main.
when i write methods (out of the main of course) it wont recognize the scanner.
import java.util.Scanner;
public class Exe1GenericSort {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
start();
int i = input.nextInt();
}//end main
here it works fine, but at the method "start" it wont let me use "input.next....
tried to write the "Scanner input = new Scanner.... above the main and still wont work...
You need to declare the Scanner as an object outside the main function and then you can use it in other functions.
import java.util.Scanner;
class ScannerTest {
private static Scanner scanner;
public static void main(String[] args){
scanner = new Scanner(System.in);
start();
}
private static void start(){
String input = scanner.nextLine();
System.out.println("Input: " + input);
}
}
NOTE: The scanner object as well as the start function need to be static in order for you to access them inside the main function.
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int x= start(input);
System.out.println("enter another number");
int i = input.nextInt();
System.out.println("a number:"+x);
System.out.println("another number"+i);
}
public static int start(Scanner scan)
{
System.out.println("Please enter a number");
int x = scan.nextInt();
return x;
}
to use Scanner in another method
accept a parameter in the method start() and then return x to test the value then print the value in the main method
solved:
import java.util.Scanner;
public class Exe1GenericSort {
static Scanner input = new Scanner (System.in);
public static void main(String[] args) {
this one works great !
thank for help ppl.
problem solved ! :)
Related
In the main method, I create an object cls and call its method test. This method will call two others methods (test1 and test2). Each one has its Scanner.
public static void main(String[] args) {
Class2 cls = new Class2();
cls.test();
}
the Class2 is:
public class Class2 {
public Class2() {
}
public void test()
{
test2();
test3();
}
public void test2() {
Scanner scanner = new Scanner(System.in);
System.out.println("give a String:");
String str = scanner.next();
scanner.close();
}
public void test3()
{
Scanner sc = new Scanner(System.in);
System.out.println("give another String:");
String str = sc.next();
sc.close();
}
}
After execution, I got an exception
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Class2.test3(Class2.java:25)
at Class2.test(Class2.java:11)
at Class1.main(Class1.java:12)
How can I handle this exception please ? by keeping in each method a different scanner !
Here Is your rectified code with appropriate comments.
Class2.java
import java.util.Scanner;
public class Class2 {
/*You dont have to create multiple scanner objects*/
Scanner scan = new Scanner(System.in);
public void test() {
/*In order to run the methods in this class itself
* you have to use static keyword or create object*/
Class2 obj = new Class2();
obj.test2();
obj.test3();
scan.close();
/* As this method is run, scan.close() should be placed when you want to close InputStream
* you will learn this in Java Streams*/
}
public void test2() {
System.out.println("give a String:");
String str = scan.nextLine();
}
public void test3() {
System.out.println("give another String:");
String str = scan.nextLine();
}
}
Main.java
public class Main {
public static void main(String[] args) {
Class2 cls = new Class2();
cls.test();
}
}
Why did the error occur?
Ans: When your code executes test2() method it closes the scanner InputStream in the ending by using scan.close(), hence when the test3() is executed it can no longer read data. The solution is that you either close scanner in the test3() method or in the test() method.
This question already has answers here:
Utilizing a Scanner inside a method
(2 answers)
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 2 years ago.
I'm trying to create a Java program that prompts the user to enter a number well guess the number with a void method called CheckNum (int num) and the decision statements in the void method CheckNum, checks if the number entered is 100, if that is true, the following message “Number is correct!"
So far this is what I have created:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
checkNum();
}
public static void checkNum()
{
System.out.println("Enter a number: ");
int guess= sc.nextInt();
int number=(int)(Math.random()*100)+1;
System.out.println("The number is "+number);
if(guess>=1 && guess<=100)
{
if(guess==number)
System.out.println("The number is right!");
else
System.out.println("Wrong!");
}
else
{
System.out.println("Make it a 1-100 please!");
checkNum();
}
}
}
You can't just use variable from other method. Here are options for you
Define the Scanner as static for the class
public class Main{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
checkNum();
}
public static void checkNum(){...}
}
Pass the Scanner as a parameter
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
checkNum(sc);
}
public static void checkNum(Scanner sc){...}
}
Define it only in the method
public class Main{
public static void main(String[] args){
checkNum();
}
public static void checkNum(){
Scanner sc = new Scanner(System.in)
...
}
}
I'm taking a coding class using BlueJ, and decided to surprise my teacher with a Text Adventure that uses a Class Runner and another class to have the methods to call. The problem is, I don't know how to use a variable that I established in the Runner, into a method in the Method Class, which I will then call into the Runner. Here is my code:
(This is the runner)
import java.util.*;
public class TextAdventureRunner
{
public static void main (String[]Args)
{
TextAdventureCode run = new TextAdventureCode();
Scanner kb = new Scanner(System.in);
String x = "";
System.out.print("Enter Your Name: : ");
x = kb.nextLine();
System.out.println(x);
run.Hi();
run.HiTwo();
}
}
(This is the code that contains the methods)
import java.util.*;
public class TextAdventureCode extends TextAdventureRunner
{
Scanner kb = new Scanner(System.in);
public static void Hi()
{
System.out.println("Hi" + x);
}
public static void HiTwo()
{
System.out.println("");
}
}
You see, In my method Hi(), there is an error where the x should be. the error reads "cannot find symbol - variable x" even though i extended the class and declared an object in the other class... any help?
You can declare your TextAdventureCode like this:
import java.util.*;
public class TextAdventureCode extends TextAdventureRunner
{
Scanner kb = new Scanner(System.in);
public static void Hi(String x) //Modification
{
System.out.println("Hi" + x);
}
public static void HiTwo()
{
System.out.println("");
}
}
And declare TextAdventureRunner like this:
import java.util.*;
public class TextAdventureRunner
{
public static void main (String[]Args)
{
TextAdventureCode run = new TextAdventureCode();
Scanner kb = new Scanner(System.in);
String x = "";
System.out.print("Enter Your Name: : ");
x = kb.nextLine();
System.out.println(x);
run.Hi(x); // Modification
run.HiTwo();
}
}
class Testing
{
public static void ischeck()
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
System.out.println("hello"+a);
//print value of aa that given by user
}
public static void main(String str[])
{
ischeck();
}
}
** My requirement is to get the scanner class value in user define function
import java.util.*;
class Testing
{
public static void someMethod()
{
Scanner sc = new Scanner(System.in);
int text = sc.nextInt();
System.out.println(text);
}
public static void main(String str[])
{
someMethod();
}
}
I made a simple java program that works on console and I have a error I never had before.
There are no errors in my code but for some reason I can't run the program cause of my 'public class serie' that is never used.
this my code:
import java.math.BigInteger;
import java.util.Scanner;
public class serie {
public final void main(String[] args) throws Exception {
final int BASE = 36;
final BigInteger MODULO = new BigInteger("ZV", BASE);;
Scanner keyboard = new Scanner(System.in);
String strChassisNummer;
String input = "y";
while (input == "y"){
try{
System.out.print("Geef een chasis nummer in:");
strChassisNummer = keyboard.nextLine();
BigInteger chassisNummer = new BigInteger(strChassisNummer,
BASE);
BigInteger remainder = chassisNummer.remainder(MODULO);
System.out.print(strChassisNummer);
System.out.print(";");
String paddedRemainder = remainder.toString(BASE);
if (paddedRemainder.length() == 1)
{
System.out.print("0" + paddedRemainder.toUpperCase());
}
else
{
System.out.print(paddedRemainder.toUpperCase());
}
System.out.println();
System.out.print("Wenst u nog een chasis nummer in te geven ? (y/n): ");
input =keyboard.nextLine();
if (input != "y"){
break;
}
}
catch (Throwable t){
t.printStackTrace();
}
}
}
}
Thanks in advance !
Declare your main method as static, not final. Why have you declared it final in the first place?
Instead of final use static modifier to the main method. The main method should be static with signature allows it to be the entry point of the runnable class.
Your main needs to be static.
Non-static methods require the class to be instansiated and Java doesn't instantiate your class by magic.
Java starts running a program with the specific
public static void main(String[] args)
signature.
So your main method should be
public static void main(String[] args)
and not
public final void main (String[] args)