I am writing an escape room project for school which has messages such as a greeting, a win message, a loss message, etc. I stored these messages in a text file and then I read the file and store each line into one ArrayList and to access each line by their respective getter method and I use the .get function with their index value. I'm wondering if there is a way to avoid hardcoding the index numbers and on top of that is there a way I can just read the file when the program is run instead of having to make an instance of the class and then for example doing foo.readFile();
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
//package Stage;
public class EscapeRoom{
ArrayList<String> Messages;
String fileName;
private boolean win = false;
public void readFile() throws FileNotFoundException {
Messages = new ArrayList<String>();
fileName = "test.txt";
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine()){
Messages.add(scanner.nextLine());
}
scanner.close();
}
public void showGreeting(){
System.out.println(Messages.get(0));
}
public void showDirections(){
System.out.println(Messages.get(1));
}
public void showWin() {
System.out.println(Messages.get(2));
}
public void showLoss() {
System.out.println(Messages.get(3));
}
}
This is exactly what a properties file is for. Here is a file I named prompts.properties:
greeting = "hello, welcome to my game"
win = "you win, yay"
loss = "sorry bro, you lose"
directions = "escape with your life!"
Here is your modified program:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class EscapeRoom {
Properties prompts = null;
public void readFile() throws IOException {
prompts = new Properties();
prompts.load(new FileInputStream("prompts.properties"));
}
public void showGreeting() {
System.out.println(prompts.get("greeting"));
}
public void showDirections() {
System.out.println(prompts.get("directions"));
}
public void showWin() {
System.out.println(prompts.get("win"));
}
public void showLoss() {
System.out.println(prompts.get("loss"));
}
}
Basically to get a named key-value pair you want something like a map. Properties are really a special sort of map that understands a file of records that have the format <key> = <value>.
Here is the documentation on Properties and if you decide to roll your own you would implement the same basic thing with a Map.
Related
My bot is stuck printing the output. I check and there was no problem with the logical part as I used the sane logic to make a normal java program. Please help as it is stuck printing the output in discord and I do not know how to solve it.
I also added some unnecessary print functions to find out where the error was. To my surprise, it was just in printing the message which is unusual as I have made bots before and none of them had any errors to just "print" messages.
import javax.security.auth.login.LoginException;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
public class rpsidnfp {
public static JDA jda;
public static void main(String args[]) throws LoginException {
jda = JDABuilder.createDefault("(my token here)").build();
core2 core2obj = new core2();
jda.addEventListener(core2obj);
}
}
The former was my main class.
And below is the core class as it contains all the functions.
package pack.rpsidnfp;
import java.util.Random;
//import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class core2 extends ListenerAdapter {
public static String prefix = "!";
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
String input = event.getMessage().getContentRaw();
String[] options = {"rock", "paper", "scissors"};
Random robj = new Random();
int rnum = robj.nextInt(options.length);
System.out.println(rnum);
String conf = options[rnum];
event.getChannel().sendMessage(conf);
System.out.println(conf);
String same = prefix + conf;
String win = "congrats, you won!";
String lose = "lmao, you lost";
if(input.equals(same)) {
event.getChannel().sendMessage("we both kept the same thing");
}
else if(input.equals(prefix + options[0])) {
if(conf.equals(options[1])) {
event.getChannel().sendMessage(lose);
}
else if(conf.equals(prefix + options[2])) {
event.getChannel().sendMessage(win);
}
}
else if(input.equals(prefix + options[1])) {
if(conf.equals(options[0])) {
event.getChannel().sendMessage(win);
}
else if(conf.equals(options[2])) {
event.getChannel().sendMessage(lose);
}
}
else if(input.equals(prefix + options[2])) {
if(conf.equals(options[0])) {
event.getChannel().sendMessage(lose);
}
else if(conf.equals(options[2])) {
event.getChannel().sendMessage(win);
}
}
}
}
The sendMessage method returns a MessageAction. You need to call queue() on that RestAction instance.
Additionally, keep in mind that your bot receives its own messages so you should make sure it ignores those. You can add a if (event.getAuthor().isBot()) return; to the start of your listener method.
See Also:
Troubleshooting Guide: Nothing happens when using X
RestAction Guide
MessageListenerExample
JDA README
Basically, I am running a few tests for a simple CLI game I am making, and initially all of my variables/objects were declared at the beginning of the main() method. I now have to create a new method that uses some of the same objects/variables, so I decided to move all of my variables out of main() and make them static/global. In doing so, I've run into a problem: despite main() throwing FileNotFoundException, I was getting the following error message:
Tests.java:14: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
static Scanner boardReader = new Scanner(board);
Here was my code, up to the problematic line:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
class Tests {
// GAME OBJECTS AND VARIABLES
// The following is used for reading, displaying, and writing to the game board.
static File board = new File("Board.txt");
static Scanner boardReader = new Scanner(board);
static FileWriter boardWriter = null;
static StringBuilder boardBuilder = new StringBuilder((int)board.length());
static String boardDisplay = null; // This String allows boardContents to print in a more readable way.
static String boardContents = null; // This is Board.txt represented as a String.
// There are more variables here than shown, but these are the ones relevant to my problem.
// GAMEPLAY
public static void main(String[] args) throws FileNotFoundException, IOException {
boolean gameIsOngoing = true;
while(gameIsOngoing) {
// boardContents String from boardBuilder StringBuilder
while (boardReader.hasNextLine()) {
boardContents = (boardBuilder.append(boardReader.nextLine() + System.lineSeparator()).toString());
}
// More irrelevant code here.
}
}
}
In trying to fix the problem, I tried to put the code utilizing boardReader inside a proper try-catch as shown here:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
class Tests {
// GAME OBJECTS AND VARIABLES
// The following is used for reading, displaying, and writing to the game board.
static File board = new File("Board.txt");
static FileWriter boardWriter = null;
static StringBuilder boardBuilder = new StringBuilder((int)board.length());
static String boardDisplay = null; // This String allows boardContents to print in a more readable way.
static String boardContents = null; // This is Board.txt represented as a String.
// There are more variables here than shown, but these are the ones relevant to my problem.
// GAMEPLAY
public static void main(String[] args) throws IOException {
boolean gameIsOngoing = true;
while(gameIsOngoing) {
// boardContents String from boardBuilder StringBuilder
try (Scanner boardReader = new Scanner(board)) {
while (boardReader.hasNextLine()) {
boardContents = (boardBuilder.append(boardReader.nextLine() + System.lineSeparator()).toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// More irrelevant code here.
}
}
}
Which works, but it makes my code feel less readable, as the Scanner object is no longer listed with all of the other objects. I guess my question is, why didn't the first code work? Is it possible for something resembling it more to work?
I also want to mention quick that in researching for this question, I saw some things saying it is generally bad practice to have Scanner objects be global/static. I figured that because this particular Scanner isn't gathering user input (and me having no plans on maintaining this relatively simple project post-completion), it wouldn't be too problematic.
Edit: I don't know why but the editor isn't letting me put a "Hello" at the beginning of this post, so here it is... hello everyone :)
There is error because the exception is not caught. The Scanner instance is created in context of the Tests class, main method is something different, therefore catching it there won't solve the issue.
If you want a work-around, you can try something like this:
static {
try {
boardReader = new Scanner(board);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
I'm trying to Parse the static variable value from the JAVA file. But couldn't be able to parse the variable.
I've used JavaParser to Parse the code and fetch the value of variable. I got success in fetching all other class level variable and value but couldn't be able to parse the static field.
The Java File looks like ...
public class ABC {
public string variable1 = "Hello How are you?";
public boolean variable2 = false;
public static String variable3;
static{
variable3 = new String("Want to Fetch this...");
}
//Can't change this file, this is input.
public static void main(String args[]){
//....Other Code
}
}
I'm able to parse the all variables value except "variabl3". The Code of Java File looks like above Java Code and I need to Parse "variable3"'s value.
I've done below code to parse the class level variable...
import java.util.HashMap;
import java.util.List;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
public class StaticCollector extends
VoidVisitorAdapter<HashMap<String, String>> {
#Override
public void visit(FieldDeclaration n, HashMap<String, String> arg) {
// TODO Auto-generated method stub
List <VariableDeclarator> myVars = n.getVariables();
for (VariableDeclarator vars: myVars){
vars.getInitializer().ifPresent(initValue -> System.out.println(initValue.toString()));
//System.out.println("Variable Name: "+vars.getNameAsString());
}
}
}
Main Method ...
public class Test {
public static void main(String[] args) {
File file = new File("filePath");
CompilationUnit compilationUnit = null;
try {
compilationUnit = JavaParser.parse(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String, String> collector = new HashMap<String, String>();
compilationUnit.accept(new StaticCollector(), collector);
}
}
How could I parse the value of "variable3", which is static and value assigned inside static block? There might be other variable in the code but I need to find value of particular variable value (in this case Variable3).
Am I doing something wrong or i need to add some other way, please suggest.
Inspecting the AST as something that's easily readable, e.g., a DOT (GraphViz) image with PlantUML is a huge help to solve this kind of problem. See this blog on how to generate the DOT as well as other formats.
Here's the overview, with the "variable3" nodes highlighted (I just searched for it in the .dot output and put a fill color). You'll see that there are TWO spots where it occurs:
Zooming in on the node space on the right, we can see that the second sub-tree is under an InitializerDeclaration. Further down, it's part of an AssignExpr where the value is an ObjectCreationExpr:
So, I adapted your Visitor (it's an inner class to make the module self contained) and you need to override the visit(InitializerDeclaration n... method to get to where you want:
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.InitializerDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
public class Test {
public static void main(String[] args) {
File file = new File("src/main/java/ABC.java");
CompilationUnit compilationUnit = null;
try {
compilationUnit = StaticJavaParser.parse(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String, String> collector = new HashMap<String, String>();
compilationUnit.accept(new StaticCollector(), collector);
}
private static class StaticCollector extends
VoidVisitorAdapter<HashMap<String, String>> {
#Override
public void visit(FieldDeclaration n, HashMap<String, String> arg) {
List<VariableDeclarator> myVars = n.getVariables();
for (VariableDeclarator vars: myVars){
vars.getInitializer().ifPresent(initValue -> System.out.println(initValue.toString()));
//System.out.println("Variable Name: "+vars.getNameAsString());
}
}
#Override
public void visit(InitializerDeclaration n, HashMap<String, String> arg) {
List<Statement> myStatements = n.getBody().getStatements();
for (Statement s: myStatements) {
s.ifExpressionStmt(expressionStmt -> expressionStmt.getExpression()
.ifAssignExpr(assignExpr -> System.out.println(assignExpr.getValue())));
}
}
}
}
Here's the output showing additionally variable3's initialization in the static block:
"Hello How are you?"
false
new String("Want to Fetch this...")
I'm learning from Java for Dummies and I don't know why I get these errors. I Googled some information.
java.util.InputMismatchException means that I want to read wrong type of values. For example file looks like:
2543
Robert
and I force the program to take from first line string.
In my opinion everything in my file look right. I compared my code to sample code in the book and I can't find any mistakes.
I use Netbeans.
The File "EmployeeInfo" look like this:
Angela
nurse
2000.23
The main class:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class DoPayroll {
public static void main(String[] args) throws IOException{
Scanner diskScanner = new Scanner (new File("EmployeeInfo.txt"));
payOneEmployee(diskScanner);
}
static void payOneEmployee(Scanner aScanner)
{
Employee anEmployee = new Employee();
anEmployee.setName(aScanner.nextLine());
anEmployee.SetJobTitle(aScanner.nextLine());
anEmployee.cutCheck(aScanner.nextDouble());
aScanner.nextLine();
}
}
The class:
public class Employee {
private String name;
private String jobTitle;
public void setName(String mName)
{
name = mName;
}
public String GetName()
{
return name;
}
public void SetJobTitle(String mJobTitle)
{
jobTitle = mJobTitle;
}
public String GetJobTitle()
{
return jobTitle;
}
public void cutCheck(double amountPaid)
{
System.out.printf("Pay to the order of %s", name);
System.out.printf("%s ***€", jobTitle);
System.out.printf("%,.2f\n", amountPaid);
}
}
Your code is perfectly fine. I encountered the same problem with the Java for Dummies book.
For me, the problem lay within the formatting of the file. I am no expert (yet), so I am sorry to be unable to explain in further detail, but the InputMismatchException is thrown because I was using a . to separate the decimals while my system's standard decimals are separated by a ,
I propose you try formatting your file like this:
Angela
nurse
2000,15
you can write like this also
static void payOneEmployee(Scanner aScanner)
{
Employee anEmployee = new Employee();
List<String> employeeValueList = new ArrayList();
while (aScanner.hasNext())
{
employeeValueList.add(aScanner.next());
}
if (!employeeValueList.isEmpty())
{
anEmployee.setName(employeeValueList.get(0));
anEmployee.SetJobTitle(employeeValueList.get(1));
anEmployee.cutCheck(new Double(employeeValueList.get(2)));
}
}
if - EmployeeInfo.txt
Angela
nurse
2000.23
in the Code
DoPayroll - class
payOneEmployee -functions line no 4
anEmployee.setName(aScanner.nextLine()); // this line will take input - Angela
anEmployee.SetJobTitle(aScanner.nextLine()); // this will take zero input because second line dont have any data
anEmployee.cutCheck(aScanner.nextDouble()); // this line take input as - nurse
//and "nurse" is not double so after reading this line at time of casting
from nurse to duble it throw java.util.InputMismatchException
I have taken all of my classes from my intro to java college in highschool class, and put them into a package called gameChoices. I then made a class that would call these classes when the user asks for them, this is called whichGame. I've imported the classes I want called using import gameChoices."whatever game it is";. How do I call these classes in whichGame? I also have them all as public static main(string [] args), which ones shouldn't have that(I think it's just whichGame that should..)? And what would I put instead? Thanks for helping a newbie out :)
The simplest way to do it is probably to set up a big if/then statement.
if(input.equals("t"))
thisOne.start();
else if(input.equals("a"))
anotherOne.start();
else if(input.equals("y"))
yetAnotherOne.start();
And so on. Might be a pain if you have a lot of classes, or if they start with the same letter.
Not sure exactly what you want to achieve, but if you need to access a class by its name you can try Class.forName() and check for exceptions thrown (particularly, ClassNotFoundException).
Using case-insensitive String equality for the name check, if would allow you to access any existing class of your ClassLoader through reflection.
Edit
Here's your main class:
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Main {
// initializes your map of letter->game class
private static final Map<String, Class<?>> GAMES = new HashMap<String, Class<?>>();
// constant name of main method for your games
private static final String MAIN_METHOD_NAME = "main";
// add your games
static {
GAMES.put("c", Chess.class);
GAMES.put("d", Doom.class);
// TODO moar gamez
}
public static void main(String[] args) {
try {
// prompts the user
System.out.println("Enter the game's name or starting letter: ");
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
// gets the response
String input = br.readLine();
br.close();
// iterates over your games' first letters
for (String gameName : GAMES.keySet()) {
// the input starts with one game's first letter...
if (gameName.startsWith(input.toLowerCase())) {
// gets the class
GAMES.get(gameName)
// gets its main method (typical signature is String[] args)
.getMethod(MAIN_METHOD_NAME, String[].class)
// invokes its main method with no arguments
.invoke((Object) null, (Object) null);
}
}
// handles any disaster
} catch (Throwable t) {
t.printStackTrace();
}
}
}
Now here are two "game" classes:
package test;
public class Chess {
public static void main(String[] args) {
System.out.println("You've chosen Chess!");
}
}
... and...
package test;
public class Doom {
public static void main(String[] args) {
System.out.println("You've chosen Doom!");
}
}
Now set your "Main" class as your... main class.
When you launch the application, it will query you for an initial letter.
If you choose "c" or "d", it will print out: "You've chosen [Chess/Doom]!"
I hope this helps you getting started.