public static void buttonAdd(boolean[][] coord) {
for (int col = 0; col < SIZE; col++) {
for (int row = 0; row < SIZE; row++) {
Button square = new Button(coord[col][row]);
square.addActionListener((ActionEvent e) -> {
if (bombOrNot) { //if bomb is true
JOptionPane lose = new JOptionPane();
lose.setMessage("You Lose");
frame.add(lose);
System.exit(0);
} else { //if bomb is false
frame.remove(square);
}
frame.add(square);
}
}
}
This code is not compiling, it seems like there is something wrong with the lambda. It says that the closing bracket for the lambda is expected to be a ")".
indenting the way the compiler sees the indentation of your code and commenting where the error happens (obviously the compiler does not see your code in that way since it get converted to a stream of tokens - without any indentation)
// wrong code, just re-indented to clarify
public static void buttonAdd(boolean[][] coord) {
for (int col = 0; col < SIZE; col++) {
for (int row = 0; row < SIZE; row++) {
Button square = new Button(coord[col][row]);
square.addActionListener(
(ActionEvent e) -> {
if (bombOrNot) { //if bomb is true
JOptionPane lose = new JOptionPane();
lose.setMessage("You Lose");
frame.add(lose);
System.exit(0);
} else { //if bomb is false
frame.remove(square);
}
frame.add(square);
}
} // missing ) to close addActionListener(
}
since you probably don't want to add square inside the lambda, you should close it '}' and close the addActionListener with ');` before that line.
...
} else { //if bomb is false
frame.remove(square);
}
} ); // this line is somehow missing in your code
frame.add(square);
...
The Lambda is passed in a call to the method addActionListener((ActionEvent e) -> which has only one argument
meaning that after the Lambda you need ); So...
public static void buttonAdd(boolean[][] coord) {
for (int col = 0; col < SIZE; col++) {
for (int row = 0; row < SIZE; row++) {
Button square = new Button(coord[col][row]);
square.addActionListener((ActionEvent e) -> {
if (bombOrNot) { //if bomb is true
JOptionPane lose = new JOptionPane();
lose.setMessage("You Lose");
frame.add(lose);
System.exit(0);
} else { //if bomb is false
frame.remove(square);
}
}); // <<<<<<<<< here Note: added } in edit
frame.add(square);
}
}
}
Hi i'm trying to change the background of a panel to create something similar to the Simon color games.
Here's what I have for code. The problem I seem to be finding is that the only time the background physically changes only at the end of the button being pressed. I have tried using Repaint(); and it has had no effect.
private void ClickRandomColorActionPerformed(java.awt.event.ActionEvent evt) {
Random gen = new Random();
for(int i = 0; i < 5; i++){
int CaseNum = 0;
CaseNum = gen.nextInt(3) +1;
ChangeBackground(CaseNum);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException ex) {
Logger.getLogger(Testerino.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void ChangeBackground(int i){
System.out.println(i);
if(i == 1){
RandColor.setBackground(blue);
}
else if (i == 2){
RandColor.setBackground(black);
}
else if(i == 3){
RandColor.setBackground(magenta);
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to calculate the minimum number of jumps to reach the end of an Array with dice throw Array Value may be negative/positive value:
when positive----> Move Forward
when negative ------> go back
The Array may also contain R value, which means that the player have to throw the dice again
The start position is marked on our Array with S and End position with E The Start Position is not always the first element of the Array and End position is not always at the end, it can even be before S
Example: Array = {4, S, -2,1, R, 4,3,4,3,-5,2,-4, E}
the player start on S position the fastest way to reach E:
Throwing the dice to have 3 and reach the R case (first move)
throwing the dice again and having 6 to reach the 2 Case (second movement)
Jumping 2 cases to reach E (third move)
so the best solution for this example is: 3 moves
Lets give you a hint: the key thing to learn here: sometimes you have to transform your input data in order to find a good way to solve your problem.
In your case; consider turning your array into a graph:
Each array index is a node within that graph
The value of each array position tells you something about edges to other nodes. For example, if a(0) is R; then a(0) would be connected to a(1), a(2) .. a(6) - because you can reach the next 6 elements.
For starters; I would suggest to do that manually; just draw the graph for example array.
So, the steps to solve your problem:
Transform your array into a graph
Search the net for algorithms to find minimum length paths in graphs
Print out that path; resulting in the minimal list from S to E
Implementation is left as exercise to the reader.
I wrote this working solution i share it for anyone interested , but when it comes to deal with big Array (3000 for example) it throws a java heap space error as the code will consume huge amount of memory , any help or advice will be appreciated
public class Solution {
static int startPosition;
public static int compute(BufferedReader br) throws IOException {
final int totalNodeCount = getTotalNodeNumber(br);
final String caseArray[] = new String[totalNodeCount];
bufferToArray(br, caseArray);
startPosition = getStartPosition(caseArray);
final boolean visited[] = new boolean[caseArray.length];
int minimumNumberOfMove = 0;
final List<Integer> reachableList = new ArrayList<Integer>();
for (int i = 1; i <= 6; i++)
{
visitedInitilise(visited);
if (((startPosition + i) < totalNodeCount) && ((startPosition + i) > 0))
getMinimumNumberOfMoves(caseArray, visited, startPosition + i, 0, reachableList);
}
// Retriving Minimum number of move from all reachble route
if (reachableList.isEmpty())
minimumNumberOfMove = Constants.IMPOSSIBLE;
else
{
minimumNumberOfMove = reachableList.get(0);
for (int i = 0; i < reachableList.size(); i++)
if (reachableList.get(i) < minimumNumberOfMove)
minimumNumberOfMove = reachableList.get(i);
}
return minimumNumberOfMove;
}
static int getStartPosition(String[] plateau){
int startIndex = 0;
for (int i = 0; i <= (plateau.length - 1); i++)
if (plateau[i].equals("S"))
{
startIndex = i;
break;
}
return startIndex;
}
static void bufferToArray(BufferedReader br, String[] plateau) {
String line;
int i = 0;
try
{
while ((line = br.readLine()) != null)
{
plateau[i] = line;
i++;
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static int getTotalNodeNumber(BufferedReader br) {
int i = 0;
try
{
i = Integer.parseInt(br.readLine());
} catch (NumberFormatException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return i;
}
static List<Integer> getMinimumNumberOfMoves(String[] plateau, boolean[] visited, final int currentIndex,
int currentNumberOfMoves, List<Integer> list) {
Boolean endIsReached = false;
Boolean impossible = false;
visited[startPosition] = true;
// Checking if the current index index is negativ
if (currentIndex < 0)
impossible = true;
while ((endIsReached == false) && (impossible == false) && (visited[currentIndex] == false)
&& (currentIndex < plateau.length))
{
try
{
switch (plateau[currentIndex]) {
case "E": {
// if end is reached , pushing number of move into our list
endIsReached = true;
list.add(currentNumberOfMoves + 1);
break;
}
case "R": {
// Marking node as visited
visited[currentIndex] = true;
for (int i = 1; i <= 6; i++)
{
// Marking all case after R case as non visited
for (int j = currentIndex + 1; j < visited.length; j++)
visited[j] = false;
// Calculating number of move after R case
if (((currentIndex + i) < plateau.length) && (currentIndex > 0))
getMinimumNumberOfMoves(plateau, visited, currentIndex + i, currentNumberOfMoves + 1, list);
}
break;
}
default: {
// Cheking if node was already visited
if (visited[currentIndex] == true)
{
// Marking all node as non visited
visitedInitilise(visited);
impossible = true;
break;
}
else
{
// when the node was not visited before , catch the jump
// value
int jumpValue = Integer.parseInt(plateau[currentIndex]);
// cheking that the next node is not bigger than node
// number and not negativ
if (((currentIndex + jumpValue) > plateau.length) || (currentIndex < 0))
{
impossible = true;
break;
}
else
{
// Marking node as visited
visited[currentIndex] = true;
// calculating minimum number of move starting from
// this node
getMinimumNumberOfMoves(plateau, visited, currentIndex + jumpValue,
currentNumberOfMoves + 1, list);
break;
}
}
}
}
}
catch (NumberFormatException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
if (impossible == true)
currentNumberOfMoves = 0;
return list;
}
static void visitedInitilise(boolean visited[]) {
for (int i = 0; i <= (visited.length - 1); i++)
visited[i] = false;
}
public static void main(String args[]){
String testCaseID = "15"; // Write a test file number from 1 to 15, or
// ALL
TestCases.test(testCaseID);
}
}
I have coded a simple memory game. Card values are added to two arrays and after that, a compare function is called. But there is a problem with the logic of the compare function.
The specific problem seems related to the fact that the compare function is called on the third button click. So on first click it adds first value to first array , on second click second value to second array. But I must click for yet a third time to call the compare function to compare the match of two arrays.
The main problem is that after all cards are inverted (10 matches in 5x4 memory game), it does not show the result.
I have uploaded full code here : http://uloz.to/xcsJkYUK/memory-game-rar .
public class PEXESO5x4 extends JFrame implements ActionListener {
private JButton[] aHracieTlactika = new JButton[20];
private ArrayList<Integer> aHodnoty = new ArrayList<Integer>();
private int aPocitadlo = 1;
private int[] aTlacitkoIden = new int[2];
private int[] aHodnotaTlac = new int[2];
private JButton aTlacitkoExit;
private JButton aTlacitkoReplay;
private JButton[] aHracieTlacitko = new JButton[20];
private int aPocetTahov = 0;
public void vkladanieHodnot() {
for (int i = 0; i < 2; i++) {
for (int j = 1; j < (this.aHracieTlactika.length / 2) + 1; j++) {
this.aHodnoty.add(j);
}
}
Collections.shuffle(this.aHodnoty);
}
public boolean zhoda() {
if (this.aHodnotaTlac[0] == this.aHodnotaTlac[1]) {
return true;
}
return false;
}
public void zapisCislaDoSuboru() {
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Semestralka.txt", true)))) {
out.println("haha");
//more code
out.println("hahahahha");
//more code
}catch (IOException e) {
//exception handling left as an exercise for the reader
}
}
public void actionPerformed(ActionEvent e) {
int match = 0;
if (this.aTlacitkoExit == e.getSource()) {
System.exit(0);
}
if (this.aTlacitkoReplay == e.getSource()) {
}
for (int i = 0; i < this.aHracieTlactika.length; i++) {
if (this.aHracieTlactika[i] == e.getSource()) {
this.aHracieTlactika[i].setText("" + this.aHodnoty.get(i));
this.aHracieTlactika[i].setEnabled(false);
this.aPocitadlo++;
this.aPocetTahov += 1;
if (this.aPocitadlo == 3) {
if (this.zhoda()) {
match+=1;
if (match==10)
{
System.out.println("You win");
}
this.aHracieTlactika[this.aTlacitkoIden[0]].setEnabled(false);
this.aHracieTlactika[this.aTlacitkoIden[1]].setEnabled(false);
} else {
this.aHracieTlactika[this.aTlacitkoIden[0]].setEnabled(true);
this.aHracieTlactika[this.aTlacitkoIden[0]].setText("");
this.aHracieTlactika[this.aTlacitkoIden[1]].setEnabled(true);
this.aHracieTlactika[this.aTlacitkoIden[1]].setText("");
}
this.aPocitadlo = 1;
}
if (this.aPocitadlo == 1) {
this.aTlacitkoIden[0] = i;
this.aHodnotaTlac[0] = this.aHodnoty.get(i);
}
if (this.aPocitadlo == 2) {
this.aTlacitkoIden[1] = i;
this.aHodnotaTlac[1] = this.aHodnoty.get(i);
}
}
}
}
}
During execution the method is called and the integer i displays as 0 on screen. However, no output is forthcoming from inside the for loop, suggesting the for loop doesnt execute. I have also tested this with breakpoints, and have obtained the same results. Any help is appreciated.
private void decrypt_btnActionPerformed(java.awt.event.ActionEvent evt) {
int ciphertext_length = Ciphertext().length();
String decrypted_char = "";
int i = 0;
System.out.println("increment" + i);
try {
for (i = 0; i == ciphertext_length; i++) {
System.out.println("test" + i);
String cipher_current_char = getLetterAtIndex(Ciphertext(), i);
int pos_char_in_alphabet = getIndexAtLetter(Alphabet(), cipher_current_char);
decrypted_char = decrypted_char +
getLetterAtIndex(Alphabet(), pos_char_in_alphabet - 5);
status_label.setText(100 / i + "%");
}
} catch (Exception e) {
e.getMessage();
}
plain_ta.setText(decrypted_char);
}
for (i = 0; i==ciphertext_length; i++){
should in all likelihood be
for (i = 0; i<ciphertext_length; i++){