Question
CONSOLE: Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) at sun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource) atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource) at java.lang.reflect.Method.invoke(UnknownSource) atcom.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) atcom.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) at sun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource) atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource) at java.lang.reflect.Method.invoke(UnknownSource) at sun.launcher.LauncherHelper$FXHelper.main(UnknownSource)
CONSOLE:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
at sun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)
at java.lang.reflect.Method.invoke(UnknownSource)
atcom.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
atcom.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
at sun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)
at java.lang.reflect.Method.invoke(UnknownSource)
at sun.launcher.LauncherHelper$FXHelper.main(UnknownSource)
Caused by: java.lang.RuntimeException: Exception inApplication start method
atcom.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: InvalidURL: Invalid URL or resource not found
atjavafx.scene.image.Image.validateUrl(Image.java:1118)
atjavafx.scene.image.Image.
atCardGame24Point.setCardsAtRandom(CardGame24Point.java:86)
atCardGame24Point.start(CardGame24Point.java:36)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
atcom.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
atcom.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(NativeMethod)
atcom.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
atcom.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(NativeMethod)
atcom.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.IllegalArgumentException: InvalidURL or resource not found
atjavafx.scene.image.Image.validateUrl(Image.java:1110)
... 12 more
Exception running applicationCardGame24Point
CardGame24Point.java
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
public class CardGame24Point extends Application{
int[] vNum = new int[4];
//overriden start method of
//Application class
@Override
public void start(Stage pStage){
Button refreshbtn = newButton("Shuffle");
Label statusLabel = new Label("");
HBox paneTop = new HBox(statusLabel,refreshbtn);
paneTop.setAlignment(Pos.BASELINE_RIGHT);
paneTop.setSpacing(10);
HBox paneCenter = new HBox();
paneCenter.setAlignment(Pos.CENTER);
paneCenter.setSpacing(10);
paneCenter.setPadding(newInsets(10));
setCardsAtRandom(paneCenter);
TextField expressionInp = newTextField();
Label expressionLabel = newLabel("Input " + "an expression:");
Button verifyBtn = newButton("Verify");
HBox btmPane = new HBox(10,expressionLabel, expressionInp, verifyBtn);
BorderPane paneBorder = newBorderPane();
paneBorder.setPadding(newInsets(10));
paneBorder.setTop(paneTop);
paneBorder.setCenter(paneCenter);
paneBorder.setBottom(btmPane);
refreshbtn.setOnAction(e->{
statusLabel.setText("");
setCardsAtRandom(paneCenter);
});
verifyBtn.setOnAction(e ->{
String expStr =expressionInp.getText();
if(!isValidOperand(expStr)){
statusLabel.setText("The numbers in the " + "expression dont matchthe numbers " + "in the set");
}
else if(isValid(expStr) && isCorrect(expStr)) {
statusLabel.setText("Correct");
}
else{
statusLabel.setText("Incorrect result");
}
});
Scene scene = newScene(paneBorder);
pStage.setTitle("Exercise20_13");
pStage.setScene(scene);
pStage.show();
}
private void setCardsAtRandom(HBoxhboxPane){
boolean[] usedInfo = newboolean[52];
int count = 0;
hboxPane.getChildren().clear();
while(count < 4){
int card =(int)(Math.random() * 52);
if(!usedInfo[card]){
usedInfo[card] = true;
hboxPane.getChildren().add(new ImageView(new Image("image/cards" +(++card) + ".png")));
intvalue = card % 13;
vNum[count]= (value == 0) ? 13 : value;
count++;
}
}
}
private static boolean isOperator(char ch){
return (ch == '(' || ch == ')' ||isArithmeticOperator(ch));
}
private static boolean isArithmeticOperator(charch){
return (ch == '/' || ch == '+' || ch =='-' || ch == '*');
}
private static String[] separateExp(Strings){
ArrayList
char[] charArray =s.toCharArray();
String numberBuffer = "";
for(char ch : charArray){
if(isOperator(ch)){
if(numberBuffer.length() > 0){
tokensList.add(numberBuffer);
numberBuffer = "";
}
tokensList.add(ch + "");
}
else{
if(ch != ''){
numberBuffer += ch;
}
}
}
if(numberBuffer.length() >0){
tokensList.add(numberBuffer);
}
return tokensList.toArray(newString[tokensList.size()]);
}
private boolean isCorrect(String infixExp){
return (24 ==PostfixConv.evalInfix(infixExp));
}
private boolean isValidOperand(StringinfixExp){
String[] tokensArray =separateExp(infixExp);
for(String s : tokensArray){
for(char ch :s.toCharArray()){
if(Character.isAlphabetic(ch)){
return false;
}
}
}
Stack
for(String token : tokensArray){
if(!isOperator(token.charAt(0))){
operandStack.push(Integer.parseInt(token));
}
}
if(operandStack.size() !=vNum.length){
return false;
}
int flag = 1;
int[] intArr = new int[4];
int[] operArr = new int[4];
for(int i = 0; i < 4; i++){
intArr[i] =vNum[i];
operArr[i] =operandStack.pop();
}
Arrays.sort(intArr);
Arrays.sort(operArr);
System.out.println(intArr + " " +operArr);
for(int i = 0; i < 4; i++){
if(intArr[i] ==operArr[i]){
flag++;
}
}
if(flag > 3){
return true;
}
else
return false;
}
private boolean isValid(String infixExp){
String[] tokensArray =separateExp(infixExp);
for(String s : tokensArray){
for(char ch :s.toCharArray()){
if(Character.isAlphabetic(ch)){
return false;
}
}
}
Stack
for(String token : tokensArray){
if(!isOperator(token.charAt(0))){
operandStack.push(Integer.parseInt(token));
}
}
if(operandStack.size() !=vNum.length){
return false;
}
ArrayList
for (int num : vNum){
validOper.add(num);
}
for(int i = 0; i < validOper.size();i++){
int number =operandStack.pop();
int index =validOper.indexOf(number);
if(index != -1){
validOper.remove(index);
}
else{
returnfalse;
}
}
return true;
}
public static void main(String[] args){
Application.launch(args);
}
}
PostfixConv.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import java.util.Collections;
class PostfixConv{
public static String postfixGen(double r, int thresh,Double... reqNum){
double ans;
int opLim = reqNum.length - 1;
int len = reqNum.length + opLim;
String expr;
Stack
int attemptCount = 0;
do{
expr = "";
shufNum.clear();
shufNum.addAll(Arrays.asList(reqNum));
Collections.shuffle(shufNum);
int tracker = 0;
int operatorCount =0;
int count = 0;
while(count
expr += getRanOperator() + " ";
tracker -= 2;
}
else if(operatorCount == opLim || tracker - 2 < 0){
expr += shufNum.pop() + " ";
tracker++;
}
else if((int)(Math.random() * 2) == 1){
expr += shufNum.pop() + " ";
tracker++;
}
else{
expr += getRanOperator() + " ";
tracker -= 1;
}
count++;
}
ans =evaluate(expr);
attemptCount++;
}
while(ans != r &&attemptCount < thresh);
if(ans == r){
return expr;
}
else{
return"Solution NotFound";
}
}
public static double safeInfixEval(StringinfixExp) throws RuntimeException{
StringBuilder strBuilder = newStringBuilder();
String oper = "()-+*/";
for(char ch :infixExp.toCharArray()){
if(ch == ' '){
continue;
}
if(Character.isAlphabetic(ch)){
throw newRuntimeException("No " + "characters are allowed");
}
if(oper.indexOf(ch) >=0){
strBuilder.append(" ").append(ch).append(" ");
}
elseif(Character.isDigit(ch) || ch == '.'){
strBuilder.append(ch);
}
else{
throw newRuntimeException("Invalid" + "character detected! " + ch);
}
}
Stack
String[] symbols =strBuilder.toString().trim().split("s+");
for(String symbol : symbols){
if(symbol.equals("(")){
opStack.push(symbol);
}
else if(symbol.equals(")")){
if(opStack.empty()){
throw new RuntimeException("ERROR: " + "Expected a" +"'('before')'");
}
else{
opStack.pop();
}
}
}
if(opStack.size() > 0){
throw newRuntimeException("Invalid number of" + "parenthesis");
}
returnevalInfix(strBuilder.toString().trim());
}
public static double evaluate(StringpostfixExpression){
String[] expression =postfixExpression.split("s+");
Stack
try{
for(String token :expression){
if(isArithmeticOperator(token)){
evaluate(postfix, token);
}
else{
postfix.push(Double.parseDouble(token));
}
}
}
catch(NumberFormatException e){
System.out.println("Please " + "provide space between " +"expressions and numbers");
}
return postfix.pop();
}
public static double evalInfix(StringinfixExpression){
String postfix =convertToPostfix(infixExpression);
return evaluate(postfix);
}
public static String convertToInfix(StringpostfixExpression){
String[] symbols =postfixExpression.split("s+");
Stack
stack.addAll(Arrays.asList(symbols));
for(String symbol : symbols){
if(isArithmeticOperator(symbol)){
if(stack.size() < 2){
System.out.print("ERROR");
break;
}
String num2= stack.pop();
String num1= stack.pop();
stack.push("(" + num1 + " " + symbol + num2 + ")");
}
else{
stack.push(symbol);
}
}
String expression = stack.pop();
return expression.substring(1,expression.length() - 1);
}
public static String convertToPostfix(StringinfixExpression){
String[] tokens =infixExpression.split("s+");
Stack
String infixString = "";
for(String token : tokens){
if(isOperator(token)){
if(stackOperators.empty() || token.equals("(")){
stackOperators.push(token);
}
elseif(token.equals(")")){
while(!stackOperators.peek().equals("(")){
infixString += stackOperators.pop() + "";
}
stackOperators.pop();
}
else{
int topValue = opValue(stackOperators.peek());
int current = opValue(token);
if(current > topValue){
stackOperators.push(token);
}
else if(current == topValue){
infixString += stackOperators.pop() + "";
stackOperators.push(token);
}
else{
while(!stackOperators.empty() &&!stackOperators.peek().equals("(") && current <=opValue(stackOperators.peek())){
infixString +=stackOperators.pop() + " ";
}
stackOperators.push(token);
}
}
}
else{
infixString+= token + " ";
}
}
while(!stackOperators.empty()){
infixString +=stackOperators.pop() + " ";
}
return infixString;
}
public static Double[] getOperands(Stringexpression, boolean isInfix){
String operators;
if(isInfix){
operators ="()+-*/";
}
else{
operators = "+-*/";
}
ArrayList
StringBuilder sb = newStringBuilder(10);
boolean containsPeriod = false;
for(char ch :expression.toCharArray()){
if(ch == ' '){
continue;
}
if(operators.indexOf(ch)> -1 && sb.length() > 0){
operands.add(Double.parseDouble(sb.toString()));
sb = newStringBuilder(10);
containsPeriod = false;
}
else if(ch == '.'){
if(sb.length() == 0 || containsPeriod){
return null;
}
else{
sb.append(ch);
}
containsPeriod = true;
}
else if(Character.isDigit(ch)){
sb.append(ch);
}
}
if(sb.length() > 0){
operands.add(Double.parseDouble(sb.toString()));
}
return operands.toArray(newDouble[operands.size()]);
}
private static String getRanOperator(){
String[] operators = new String[]{"*","/", "+", "-"};
return operators[(int)(Math.random() * operators.length)];
}
private static int opValue(char operator){
switch(operator){
case '+':
case '-':
return1;
case '*':
case '/':
return2;
default:
return3;
}
}
private static int opValue(String operator){
returnopValue(operator.trim().charAt(0));
}
private static void evaluate(Stack
double num2 = postfix.pop();
double num1 = postfix.pop();
switch(operator){
case '+':
postfix.push(num1 + num2);
break;
case '-':
postfix.push(num1 - num2);
break;
case '/':
postfix.push(num1 / num2);
break;
case '*':
postfix.push(num1 * num2);
break;
default:System.out.println("ERROR " + "INVALID OPERATOR: " +operator);
}
}
private static void evaluate(Stack
evaluate(postfix,operator.trim().charAt(0));
}
private static boolean isOperator(char ch){
return isArithmeticOperator(ch) || ch== '(' || ch == ')';
}
private static boolean isArithmeticOperator(charch){
return(ch == '/' || ch == '+' || ch =='-' || ch == '*');
}
private static boolean isArithmeticOperator(Stringoperator){
returnisArithmeticOperator(operator.trim().charAt(0));
}
private static boolean isOperator(Stringoperator){
returnisOperator(operator.trim().charAt(0));
}
}
I'm lost on how to fix the error. Any input would be appreciatedit.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started