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);
}
}
Related
Trying to code a text game and when asking for GameSettings class input, the function gets called 3 times. I am trying to send the code back and forth between the classes, the reason why I am using different classes to make the code a bit more clean so that when I am sending the monsterHealth...etc it is readable.
Game.Java
package src;
import java.io.IOException;
public class Game {
public static void main(String[] args) throws IOException {
GameSettings GameSettings = new GameSettings();
GameSettings.init();
// GameSettings.Classes();
GameSettings.StartLogic();
if (src.GameSettings.Classes().equals("mage")) {
System.out.println("mage");
}
else if (src.GameSettings.Classes().equals("warrior")) {
System.out.println("warrior");
}
else if (src.GameSettings.Classes().equals("archer")) {
System.out.println("archer");
}
else {
System.out.println("Non valid");
}
}
}
GameSettings.Java
package src;
import java.util.Scanner;
public class GameSettings extends Game {
public interface classChoice {
}
public int playerHp;
private static Scanner scanner;
private static String nameInput;
private static String classChoice;
private String mage;
private String archer;
private String warrior;
public void init() {
scanner = new Scanner(System.in);
System.out.println("Welcome To Fizzle's Text Based RPG\nWhat Is Your
Name?");
nameInput = scanner.nextLine();
}
public static String Classes() {
System.out.println("Welcome " + nameInput + " What Class Would You Like
To Be?\n(mage)\n(warrior)\n(archer)");
classChoice = scanner.nextLine();
return classChoice;
}
public void StartLogic() {
playerHp = 10;
System.out.println(classChoice);
}
}
I see your problem. In the
GameSettings.StartLogic();
if (src.GameSettings.Classes().equals("mage")) {
System.out.println("mage");
}
else if (src.GameSettings.Classes().equals("warrior")) {
System.out.println("warrior");
}
else if (src.GameSettings.Classes().equals("archer")) {
System.out.println("archer");
}
else {
System.out.println("Non valid");
}
You're calling the GameSettings.Classes().equals() method three times. Instead of doing this, define a String variable before the if/else block like this:
GameSettings.StartLogic();
String input = src.GameSettings.Classes();
if (input.equals("mage")) {
System.out.println("mage");
}
else if (input.equals("warrior")) {
System.out.println("warrior");
}
else if (input.equals("archer")) {
System.out.println("archer");
}
else {
System.out.println("Non valid");
}
This is because when you use the if/else statement, you shouldn't call methods that rely on luck or user input inside of the statement, but define them as variables beforehand and pass those in as arguments to the if/else statement. Hope this helps!
Hello Fizzle! :)
Please clarify your question.
I added a few comments to your code:
Game.java
import java.io.IOException;
public class Game {
public static void main(String[] args) throws IOException {
GameSettings GameSettings = new GameSettings();
GameSettings.init();
GameSettings.StartLogic(); //returns null
if (GameSettings.Classes().equals("mage")) {
System.out.println("mage");
} else if (GameSettings.Classes().equals("warrior")) {
System.out.println("warrior");
} else if (GameSettings.Classes().equals("archer")) {
System.out.println("archer");
} else {
System.out.println("Non valid");
}
}
}
GameSettings.java
import java.util.Scanner;
public class GameSettings extends Game {
//why did you declare an Interface?
public interface classChoice {
}
public int playerHp;
private static Scanner scanner;
private static String nameInput;
private static String classChoice;
public void init() {
scanner = new Scanner(System.in);
System.out.println("Welcome To Fizzles Text Based RPG What Is Your Name?");
nameInput = scanner.nextLine();
}
public static String Classes() {
System.out.println("Welcome " + nameInput + " What Class Would You Like To Be?\n(mage)\n(warrior)\n(archer)");
classChoice = scanner.nextLine();
return classChoice;
}
//why are you calling this method beforehand?
public void StartLogic() {
playerHp = 10;
System.out.println("Your Class:" + classChoice);
}
}
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 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();
//.....
}
}
}
My issue is to create a method called getScanner, and each time it is called, another method can call getScanner to get the next line of input.
Code:
public static void getScanner() {
Scanner input = new Scanner(System.in);
}
public static String getInput() {
String userInput = getScanner().nextLine();
return userInput
}
Error
error: void cannot be dereferenced
String userInput = getScanner().nextLine();
1 error
Essentially, I have multiple methods that need to user Scanner as input, rather than declaring a global or local variable, I want to call one method that will create a Scanner as input each time it is called and assign a variable to this input.
You have set void return type which returns nothing. Since you require a scanner object, return a Scanner in the method.
public static Scanner getScanner() {
Scanner input = new Scanner(System.in);
return input }
public static String getInput() {
String userInput = getScanner().nextLine();
return userInput }
You are not returning an instance of Scanner. Use:
public static Scanner getScanner(){
return new Scanner(System.in);
}
Working Code. Tested and Compiled.
public class testj
{
public static void main(String[] args)
{
System.out.println(getInput());
}
private static String getInput()
{
String userInput = getScanner().nextLine();
return userInput;
}
private static Scanner getScanner()
{
return new Scanner(System.in);
}
}
We have to specify in the method header, what object is getting returned.
Shorter version for getInput()
You can also modify this method as below
private static String getInput()
{
return getScanner().nextLine();
}
I have a text file which contains the list of all public method names. I require a Java program which reads each method name from the text file and create a method(template) for each public method name.
Say,
My text file contains, 3 methods
public static void A()
public static void B()
public static void C()
I need a output like this.
public class class_name
{
public void A_test()
{
System.out.println("Method A");
}
public void B_test()
{
System.out.println("Method B");
}
public void C_test()
{
System.out.println("Method C");
}
}
Kindly give your suggestions.
Following your example above the code below will generate a similar output. Note there is no package. NOTE As your example the generator strips static
public class ClassBuilder
{
public static String buildClass(String className,ArrayList<String> methods)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("public class %s \n{", className)).append("\n");
for (String method: methods)
{
stringBuilder.append(String.format(" %s \n {", method.trim().replace("static ","").replace("()","_test()"))).append("\n }\n\n");
}
stringBuilder.append("}");
return stringBuilder.toString();
}
public static void main(String[] args)
{
Scanner scanner = null;
try
{
scanner = new Scanner(new File("d:\\testFile.txt"));
ArrayList<String> methods = new ArrayList<String>();
while (scanner.hasNext())
{
methods.add(scanner.nextLine());
}
scanner.close();
String javaClass = buildClass("className", methods);
System.out.println(javaClass);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
output
public class className
{
public void A_test()
{
}
public void B_test()
{
}
public void C_test()
{
}
}