Last Updated
Viewed 16 Times
     
for(int u = 0; u < 9; u++) {
        /*int i2 = i;
        int finalI = i;*/
        int u2 = u;
        button[u2].setOnMouseClicked(event -> {
            MouseButton mbutton = event.getButton();
            if (mbutton == MouseButton.PRIMARY) {
                //grid_b[i[0]] = false;
                //grid[u[0]] = 0;
                //arraylist.add(u2, 0);
                button[u2].setText("O");
                int qdq;
            } else if (mbutton == MouseButton.SECONDARY) {
                //grid_b[i[0]] = true;
                //arraylist.add(u2, 1);
                //grid[u[0]] = 1;
                button[u2].setText("X");
            } else if (mbutton == MouseButton.MIDDLE) {
                primaryStage.setTitle(String.valueOf(grid[u2]));
                e[0] = false;

            }
        });

This is my code, I want to make a TicTacToe game from scratch in JavaFX. As you can see I tried everything, but I can't get those arrays updated

I run into a problem that I do not understand. In the method of adding in the Table class I would like to use:

Arrays.binarySearch(asso, (a,b) -> a.cle.compareTo(b.cle));

Where asso is my array of Association objects containing the key and the value.

By executing this code with the type String, I get an error "cannot find symbol" on the method compareTo(Object).

There my code:

test:

Table<String,String> table0 = new Table<String,String>(10);
table0.add("1","int");

method:

import java.util.Arrays;

public class Table<C,V>{

  Association[] asso;

  public Table(int n){
    asso = new Association[n];
  }

  public void add(C cle, V valeur){
    asso[0] = new Association<C,V>(cle,valeur);
    Arrays.sort(asso, (a,b) -> a.cle.compareTo(b.cle));
  }

  public class Association<C,V>{

    public C cle;
    public V valeur;

    public Association(C cle,V valeur){
      this.cle = cle;
      this.valeur = valeur;
    }
  }
}

And the error:

../src/Table.java:15: error: cannot find symbol
Arrays.sort(asso, (a,b) -> a.cle.compareTo(b.cle));
                                ^
symbol:   method compareTo(Object)
location: variable cle of type C
where C is a type-variable:
C extends Object declared in class Table.Association

Thank you for your help.

I tried to check score[0] :

if score[0] is 1 , pos+1

if score[0] is 0 ,neu+1

if score[0] is -1, neg+1

final int[] score = { 0 };
final  int post = 0;
final int neu = 0;
final int neg = 0;
stream.forEach(line -> {

    ps.println("Code: " + analysis.parse(line).getCode()); 
    score[0] = score[0] + analysis.parse(line).getCode(); 

    if(score[0]==1){ post ++;}   //problem line
    else if (score[0]==0) {neu++;}  //problem line 
    else{ neg++; }  //problem line 
});

update

I tried to remove final in the variable initiation

local variable referenced from a lambda expression must be final or effectively final

sample code

public static void main (String args[]) throws UnsupportedEncodingException, IOException {

    final int[] score = { 0 };
    int post = 0;

    try (Stream < String > stream = Files.lines(Paths.get("e:\\in.txt")).sequential()) { //retrieve txt file
        File file = new File("e:\\out.txt"); //output file
        FileOutputStream fos = new FileOutputStream(file);
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);

        stream.forEach(line -> {
            if(score[0]==1){ post ++;}
        });

        System.out.println("postive=" + post); 
    }
}

purpose

I tried to read txt file by line.

Then, I use an external library to translate every line into code (-1,0,1)

The codes in every lines store in score[0]

I create pos neu neg to count how many -1,0,1 respectively.

More importantly, the problem I m facing is the part of checking -1/0/1. Other parts should be fine.

Lastly, the translated codes will output to a new txt, including the value of pos,neu,neg.

Originally code :

public static void main (String args[]) throws UnsupportedEncodingException, IOException {

    final int[] score = { 0 };
    int post = 0;
    final int neu = 0;
    final int neg =    0;

    try (Stream < String > stream = Files.lines(Paths.get("e:\\ust.txt")).sequential()) { //retrieve txt file
        File file = new File("e:\\gg.txt"); //output file
        FileOutputStream fos = new FileOutputStream(file);
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);
        final Analysis analysis = new Analysis();
        stream.forEach(line -> {

            ps.println("Code: " + analysis.parse(line).getCode()); //show current line's code
            score[0] = score[0] + analysis.parse(line).getCode();  //add up total codes
            if(analysis.parse(line).getCode()==1){ post ++;}  // check current codes=>line with problems
            else if (analysis.parse(line).getCode()==0) {neu++;}// check current codes=>line with problems
            else{ neg++; } //check current codes=> line with problems

        });

        System.out.println("score=" + score[0]); //print out the total score
        System.out.println("postive=" + post); 
        System.out.println("neu=" + neu); 
        System.out.println("neg=" + neg); 
    }
}

I am trying to use lambda expressions to convert a String array to an Integer array.

I have provided my code below, along with a brief description of what I have tried so far:

String [] inputData = userInput.split("\\s+");
Integer [] toSort = new Integer [] {};
try {
    toSort = Arrays.asList(inputData).stream().mapToInt(Integer::parseInt).toArray();
}catch(NumberFormatException e) {
    System.out.println("Error. Invalid input!\n" + e.getMessage());
}

The lamba expression I have above is one which maps to an int array, which is not what I want, upon compiling this code I get the following error message:

BubbleSort.java:13: error: incompatible types: int[] cannot be converted to Integer[]
            toSort = Arrays.asList(inputData).stream().mapToInt(Integer::parseIn
t).toArray();

          ^
1 error

Is there a simple way which allows me to use lambdas, or other means, to get from a String array to an Integer array?

Similar Question 5 (5 solutions) : generic array of inner class initialization in java

Similar Question 6 (3 solutions) : Cannot push an Integer object into an generic array

Similar Question 8 (2 solutions) : convert a integer to array of class type java

Similar Question 9 (4 solutions) : pass integer array from one class to another

cc