How to read value in user Defind function in java - java

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();
}
}

Related

Please assist with my code keep getting an variable sc error [duplicate]

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)
...
}
}

How to auto generate responses for System.out.println

I have two classes. Class A, prompts the user to input a number 9 times using Scanner(System.in). Class B implements class A.
How can I automate responses for class A when running class B. For example, when I run class B, how would I get the computer to respond the number "3" every time class A prompts me to enter a number?
public class A {
private Scanner scan;
public A() throws FileNotFoundException{
scan = new Scanner(System.in);
for(int i=1;i<=9;i++){
System.out.println("Enter #");
int num = scan.nextInt();
}
}
}
public class B{
public B(){
A runA = new A();
}
public static void main(String[] args) {
B runB = new B();
}
}
I would recommend using a function in Class A to get the inputs one by one. Then, from Class B, print the line before the method is called. As such:
public class A {
private Scanner scan;
public A() throws FileNotFoundException {
scan = new Scanner(System.in);
}
public void getInput() {
System.out.println("Enter #");
int num = scan.nextInt();
}
}
public class B {
public B() {
A runA = new A();
for(int i=1;i<=9;i++) {
System.out.println("3");
runA.getInput();
}
}
public static void main(String[] args) {
B runB = new B();
}
}
This should be what you want.
Either that or you can print the line above where it says scan.nextInt(); in your code.

How can I use a Variable from a Class Runner to use in a Method in another Class? (BlueJ, Java)

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();
}
}

java Scanner declaring

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 ! :)

How to use method showMessage()?

In class we learned about methods, but I'm having a bit of trouble using them.
In a package called util, I wrote a class called IO.
public class IO {
public static float getFloat(){
String str = JOptionPane.showInputDialog("Enter a real number");
return Float.parseFloat(str);
}
public static void showMessage(Scanner s){
System.out.println(s);
JOptionPane.showMessageDialog(null, s);
}
public static Scanner getInput (String prompt){
String s = JOptionPane.showInputDialog(prompt);
return new Scanner(s);
}
}
Also in package util, I have my program, called Program 4.
public class Program4 {
public static void main(String[] args) {
IO.getInput("enter 2 integers");
IO.showMessage(Scanner(s));
}
}
What I don't understand is how do I display the 2 integers entered? One is a scanner object and one is string. How do I use the method getInput to show convert the scanner into a string? Am I going to have to write a new method and use parse?
You can get user input without using Scanner. Here is example:
IO Class
public class IO {
public static float getFloat() {
String str = JOptionPane.showInputDialog("Enter a real number");
return Float.parseFloat(str);
}
public static void showMessage(String s) {
System.out.println(s);
JOptionPane.showMessageDialog(null, s);
}
public static String getInput(String prompt) {
// JOptionPane.showInputDialog() return user input String
String input = JOptionPane.showInputDialog(prompt);
return input;
}
}
Program4 Class
public class Program4 {
public static void main(String[] args) {
// IO.getInput() return stored input String
String input = IO.getInput("enter 2 integers");
IO.showMessage(input);
}
}

Categories

Resources