How to auto generate responses for System.out.println - java

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.

Related

Exception when I insert a different Scanner in different methods

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.

How do I change a variable in one java class from another, then access that changed variable from a third java class?

So say I have 3 java classes, A, B, and C. Class A has an int variable named num that equals 3. I want to be able to change num from class B to another number (for example, 45), then access that variable from class C so that it equals 45. How would I do this?
//First Class
public class A
{
public int num;
public A() {
num = 3;
}
public void setNum(int newNum) {
num = newNum;
}
public int getNum() {
return num;
}
}
//Second Class
public class B
{
public static void main(String[] args) {
A a = new A();
C c = new C();
System.out.println(a.getNum());
a.setNum(45);
System.out.println(a.getNum());
c.printStuff();
}
}
//Third Class
public class C
{
public void printStuff() {
A a = new A();
System.out.println(a.getNum());
}
}
The first two print statements output the expected 3 and 45, but the print statement in the C class still outputs a 3, even though I changed num's value to 45 in class B.
The reason you cann't get the result you want, is that you have created a new instance of class A in class C, and it has its own value, which is 3. Changing the value of the instance of class A which is defined in class B will not effect this new instance.
Note that you called the new method on class A two times so you have two seperate instances.
You can try this code to get the result you want:
//First Class
class A
{
public int num;
public A() {
num = 3;
}
public void setNum(int newNum) {
num = newNum;
}
public int getNum() {
return num;
}
}
//Second Class
public class B
{
public static void main(String[] args) {
A a = new A();
C c = new C();
System.out.println(a.getNum());
a.setNum(45);
System.out.println(a.getNum());
c.printStuff(a);
}
}
//Third Class
class C
{
public void printStuff(A a) {
//A a = new A();
System.out.println(a.getNum());
}
}
Thats happens because in C class you are creating a new instance of A again. So the value would be 3. You can change your constructor and call it like this,
public A(int value) {
num = value;
}
new Post(45);
By this when you create a instance you can assign a value to num as you wish from any class.

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

How to read value in user Defind function in 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();
}
}

How to execute different classes in one class

I want to create a program that can do all the stuff from another code, depending on user input. Something like this:
import java.util.Scanner;
public class Main_Programm1 {
public static void main(String args[]) {
String something = "something";
String something2 = "something2";
Scanner userInput = new Scanner(System.in);
String action = userInput.next();
if (action.equals(something)) {
//here i want to execute all the code from class Main_Programm2
} else if (action.equals(something2)) {
//here i want to execute all the code from class Main_Programm3 and so on
}
}
}
How do i do it?
Actually, you've got it all done, only creates the Objects that you need ;-)
import java.util.Scanner;
// imports classes;
public class Main_Programm1
{
public static void main(String args[])
{
String something = "something";
String something2 = "something2";
Main_Programm main_prog;
Main_Programm2 main_prog2;
Scanner userInput = new Scanner(System.in);
String action = userInput.next();
if (action.equals(something))
{
main_prog = new Main_Programm();
//.....
}
else if (action.equals(something2))
{
main_prog2 = new Main_Programm2();
//.....
}
}
}

Categories

Resources