I have this method which should output a toast at the end but it is coming up with the following error
Cannot resolve method Toast.makeText()
public Guess makeGuess() {
PegEnum[] data1 = new PegEnum[4];
PegEnum[] codeCpy = code.clone();
PegEnum[] guessCpy = guess.clone();
int correctCount = 0;
int correctColourCount =0;
for (int i = 0; i < codeCpy.length; i++) {
if(codeCpy[i]==guessCpy[i]) {
++correctCount;
codeCpy[i]=null;
guessCpy[i]=null;
}
}
for(int x =0; x<codeCpy.length; x++) {
if (codeCpy[x]!=null) {
for (int y = 0; y < guessCpy.length; y++) {
if (codeCpy[x]==guessCpy[y]) {
++correctColourCount;
guessCpy[y]=null;
}
}
}
}
return new Guess(guess.clone(), correctCount, correctColourCount);
Toast.makeText(this, "You have guessed: " + correctCount + " Correct Pegs" + " and "
+ correctColourCount + "Correct Colours", Toast.LENGTH_LONG).show();
}
There are some errors in your code -
1) Did you import Toast?2) Your toast is after the return statement, and that makes the code unreachable.
Related
I'm making a sorting algorithm visualizer and i want the UI to update in real time to show how it works, step by step.
But the UI only updates when the sort() method is done, I want it to update when updateHeight() is done
the sort() method is initiated by an onAction
#FXML
public void sort() throws InterruptedException {
minHeight = RectHeight[0];
counter = 0;
minIndex = 0;
temp = 0;
for(int i=1;i<RectList.size();i++){
System.out.println(RectList.size());
System.out.println(counter);
sort2();
}
}
public void sort2() throws InterruptedException {
System.out.println("start sort");
System.out.println(minHeight);
System.out.println("find min");
for (int i = counter; i < (RectList.size()); i++) {
System.out.println("i= " + i);
if (RectHeight[i] < minHeight) {
minHeight = RectHeight[i];
System.out.println("minHeight= " + minHeight);
System.out.println("minIndex= " + i);
minIndex = i;
}
}
updateHeight();
Thread.sleep(500);
}
public void updateHeight() {
temp = RectHeight[counter];
RectHeight[counter] = RectHeight[minIndex];
RectList.get(counter).setHeight(RectHeight[counter]);
RectHeight[minIndex] = temp;
RectList.get(minIndex).setHeight(temp);
counter++;
minHeight = RectHeight[counter];
minIndex = counter;
}
Never sleep on the FX Application thread. That thread is responsible for rendering the UI, so by blocking it with Thread.sleep() you prevent the UI from being rendered, as you have observed.
Instead, use the Animation API. A simple Timeline should work here:
#FXML
public void sort() {
Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(500), e -> sort2()));
timeline.setCycleCount(rectList.size());
minHeight = rectHeight[0];
counter = 0;
minIndex = 0;
temp = 0;
timeline.play();
}
public void sort2() {
System.out.println("start sort");
System.out.println(minHeight);
System.out.println("find min");
for (int i = counter; i < (rectList.size()); i++) {
System.out.println("i= " + i);
if (rectHeight[i] < minHeight) {
minHeight = rectHeight[i];
System.out.println("minHeight= " + minHeight);
System.out.println("minIndex= " + i);
minIndex = i;
}
}
updateHeight();
}
public void updateHeight() {
temp = rectHeight[counter];
rectHeight[counter] = rectHeight[minIndex];
rectList.get(counter).setHeight(rectHeight[counter]);
rectHeight[minIndex] = temp;
rectList.get(minIndex).setHeight(temp);
counter++;
minHeight = rectHeight[counter];
minIndex = counter;
}
Here's the question -
https://www.codechef.com/DEC17/problems/CPLAY
And here's my solution -
import java.util.Scanner;
class Penalty_Shoot_Out
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//System.out.println("T: ");
int T = sc.nextInt();
sc.nextLine();
while(T-->0)
{
String s = sc.nextLine();
int l = s.length();
s = " " + s;
int A_score = 0, B_score = 0, A_shots = 0, B_shots = 0, flag = 0, A_shots_left = 5, B_shots_left = 5, shots_left = 0;
Outer:for(int i=1; i<=20; i++)
{
char c = s.charAt(i);
if(i%2==1)
{
flag = 0;
A_shots++;
A_shots_left--;
if(c=='1')
A_score++;
}
else
{
flag = 1;
B_shots++;
B_shots_left--;
if(c=='1')
B_score++;
}
if(i<=10)
{
if(A_score<B_score)
{
if(B_score-A_score>A_shots_left)
{
System.out.println("TEAM-B " + i);
break Outer;
}
}
else
{
if(A_score-B_score>B_shots_left)
{
System.out.println("TEAM-A " + i);
break Outer;
}
}
}
else if(i>10 && i<=20)
{
if(i%2==0)
{
if(A_score>B_score)
System.out.println("TEAM-A " + i);
else if(B_score>A_score)
System.out.println("TEAM-B " + i);
else
System.out.println("TIE");
break Outer;
}
}
}
}
}
}
These are the exceptions -
Exception
in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Penalty_Shoot_Out.main(Main.java:8)
I'm getting the correct answer on my computer, but when I submit it online, I get the NZEC Runtime Error.
I tried Googling for solutions and most people said the error might have to do with the main function returning a wrong number or not returning a number. Some said it may be due to the use of a particular function or due to an exception that i am not handling during I/O. However, I haven't been able to figure it out.
I'd really appreciate it if someone could fix my code.
You should put your solution in a try catch.
public static void main(String args[])
{
try {
Scanner sc = new Scanner(System.in);
//System.out.println("T: ");
int T = sc.nextInt();
sc.nextLine();
while(T-->0)
{
String s = sc.nextLine();
int l = s.length();
s = " " + s;
int A_score = 0, B_score = 0, A_shots = 0, B_shots = 0, flag = 0,
A_shots_left = 5, B_shots_left = 5, shots_left = 0;
Outer:for(int i=1; i<=20; i++)
{
char c = s.charAt(i);
if(i%2==1)
{
flag = 0;
A_shots++;
A_shots_left--;
if(c=='1')
A_score++;
}
else
{
flag = 1;
B_shots++;
B_shots_left--;
if(c=='1')
B_score++;
}
if(i<=10)
{
if(A_score<B_score)
{
if(B_score-A_score>A_shots_left)
{
System.out.println("TEAM-B " + i);
break Outer;
}
}
else
{
if(A_score-B_score>B_shots_left)
{
System.out.println("TEAM-A " + i);
break Outer;
}
}
}
else if(i>10 && i<=20)
{
if(i%2==0)
{
if(A_score>B_score)
System.out.println("TEAM-A " + i);
else if(B_score>A_score)
System.out.println("TEAM-B " + i);
else
System.out.println("TIE");
break Outer;
}
}
}
}
} catch(Exception e) {
return;
}
}
}
Right now i'm struggeling with a basic algorithm, that shall sort a linked list. I have two additional linked lists (in the beginning empty), in which i can copy the Integer Objects of the first linkedlist.
My problem is, that all of my tries simply doesn't work. In the copied example at the bottom, it goes through both of the while loops, but i don't know how to loop everything, until everything is sorted in the third linked list (zug3.zug3). Also i shall compare the actual smallest value of zug1 to the smallest of zug2 and then continue sorting in the list where the value is smaller. That is not possible at the start of sorting, because if i wanna getSmallest() of an empty List, it will get a null pointer exception.
I'm tryin this now since three days with different, for-loops, while-loops, if-else sentences but i don't find out, how to make it work accurate.
Please help!
Example of the Programm:
public class Abstellgleis {
LinkedList<Integer> zug1 = new LinkedList<Integer>();
void initialize() {
for (int i = 0; i <15;i++) {
Random zahl = new Random();
int integer = zahl.nextInt(15);
zug1.add(integer);
}
}
public void wagenAnkoppeln(int i) {
zug1.addFirst(i);
}
int wagenAbkoppeln() {
int waggonNummer = zug1.getFirst();
zug1.removeFirst();
return waggonNummer;
}
int getSmallest() {
int smallest = zug1.size();
for( int i =1; i <zug1.size()-1; i++)
{
if(zug1.get(i) < smallest )
{
//int smallest = integers.get(Oedipus);
smallest = zug1.get(i);
}
}
return smallest;
}
}
public class Rangiergleis {
LinkedList<Integer> zug2 = new LinkedList<Integer>();
void waggonAnkoppeln(int i) {
zug2.addFirst(i);
}
int waggonAbkoppeln() {
int waggonNummer = zug2.getFirst();
zug2.removeFirst();
return waggonNummer;
}
int getSmallest() {
int smallest = 100;
for (int i = 0; i < zug2.size() - 1; i++) {
if (zug2.get(i) < smallest) {
smallest=zug2.get(i);
}
}
return smallest;
}
}
public class Zuggleis {
LinkedList<Integer> zug3 = new LinkedList<Integer>();
void waggonAnkoppeln(int i) {
zug3.addLast(i);
}
}
public class Steuerung {
public static void main(String[] args) {
Abstellgleis zug1 = new Abstellgleis();
zug1.initialize();
Rangiergleis zug2 = new Rangiergleis();
Zuggleis zug3 = new Zuggleis();
System.out.println("Abstellgleis:" + zug1.zug1);
System.out.println("Rangiergleis: " + zug2.zug2);
System.out.println("Abstellgleis: " + zug3.zug3);
while (!zug1.zug1.isEmpty()) {
if (zug1.zug1.getFirst() != zug1.getSmallest()) {
zug2.waggonAnkoppeln(zug1.zug1.getFirst());
System.out.println("Vom Abstellgleis wurde Wagen " +
zug1.zug1.getFirst() + " aufs Rangiergleis bewegt");
zug1.zug1.removeFirst();
}
else if (zug1.zug1.getFirst() == zug1.getSmallest()) {
zug3.waggonAnkoppeln(zug1.zug1.getFirst());
System.out.println(zug1.zug1.getFirst() + "wurde aufs Zuggleis bewegt");
zug1.zug1.removeFirst();
}
System.out.println("Abstellgleis:" + zug1.zug1);
System.out.println("Rangiergleis: " + zug2.zug2);
System.out.println("Zuggleis: " + zug3.zug3);
}
while (!zug2.zug2.isEmpty()) {
if (zug2.zug2.getFirst() != zug2.getSmallest()) {
zug1.wagenAnkoppeln(zug2.zug2.getFirst());
System.out.println("Vom Rangiergleis wurde Wagen " +
zug2.zug2.getFirst() + " aufs Abstellgleis bewegt");
zug2.zug2.removeFirst();
}
else if (zug2.zug2.getFirst() == zug2.getSmallest()) {
zug3.waggonAnkoppeln(zug2.zug2.getFirst());
System.out.println(zug2.zug2.getFirst() + " wurde vom Rangiergleis aufs Zuggleis bewegt");
zug2.zug2.removeFirst();
}
System.out.println("Abstellgleis:" + zug1.zug1);
System.out.println("Rangiergleis: " + zug2.zug2);
System.out.println("Zuggleis: " + zug3.zug3);
}
if (zug1.zug1.isEmpty()) {
while (!zug2.zug2.isEmpty())
if (zug2.zug2.getFirst() != zug2.getSmallest()) {
zug1.wagenAnkoppeln(zug2.zug2.getFirst());
System.out.println("Vom Abstellgleis wurde Wagen " +
zug2.zug2.getFirst() + " aufs Rangiergleis bewegt");
zug2.zug2.removeFirst();
}
else if (zug2.zug2.getFirst() == zug2.getSmallest()) {
zug3.waggonAnkoppeln(zug2.zug2.getFirst());
System.out.println(zug2.zug2.getFirst() + "wurde aufs Zuggleis bewegt");
zug2.zug2.removeFirst();
}
System.out.println("Abstellgleis:" + zug1.zug1);
System.out.println("Rangiergleis: " + zug2.zug2);
System.out.println("Zuggleis: " + zug3.zug3);
}
}
}
Your getSmallest methods in Abstellgleis and Rangiergleis don’t look right. In the first you start by setting smallest to zug1.size(). First time when the size is 15 this is probably fine, but as the Zug grows shorter, there may come a point when the size is smaller than the smallest element, and then your method will give the wrong result. In Rangiergleis you are initializing to 100, that’s sounder. In both methods you are missing the last element. For example in Abstellgleis.getSmallest():
for( int i =1; i <zug1.size()-1; i++)
This is in fact missing both the first and the last element. Elements are indexed 0 through zug1.size() - 1, so it should be one of the two following:
for (int i = 0; i < zug1.size(); i++) {
for (int i = 0; i <= zug1.size() - 1; i++) {
The former would be conventional. If you are sure there is at least one wagon in the train, you may of course initialize smallest to zug1.get(0) and the have the loop run from 1 (this could have been what you intended).
In Rangiergleis.getSmallest() your loop runs from 0 as it should, but is missing the last element in the same way as in Abstellgleis.
I have some problems in implementing the hill cipher algorithm in Java Android. It runs well on certain condition but it comes lack on other condition.
Here is the running app.
1. It runs OK for below condition
2. It comes with unexpected result for below condition
I'm just figuring that the problem comes from the negative results. I used the determinant and multiplicative invers to find the matrix invers.
here is the code of my project.
MainActivity.java
package com.andri.hilltest;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText txtPlain, txtKey, txtCipher, txtDecrypt;
Button btnEncrypt, btnCheck, btnDecrypt;
String char_db = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ?!.,";
//String char_db = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc";
int[] array_angka, array_angka2;
int[] array_angka_cipher, array_angka_decrypted;
int[][] M_key = new int[2][2];
int[][] M_inverse = new int [2][2];
int i;
String key_input, ciphertext, plaintext;
int db_length = char_db.length();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtPlain = (EditText) findViewById(R.id.txtPlain);
txtKey = (EditText) findViewById(R.id.txtKey);
txtCipher = (EditText) findViewById(R.id.txtCipher);
txtDecrypt = (EditText) findViewById(R.id.txtDecrypt);
btnEncrypt = (Button) findViewById(R.id.btnEncrypt);
btnDecrypt = (Button) findViewById(R.id.btnDecrypt);
btnCheck = (Button) findViewById(R.id.btnCheck);
btnEncrypt.setOnClickListener(this);
btnCheck.setOnClickListener(this);
btnDecrypt.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v == btnEncrypt){
String plainTextInput = txtPlain.getText().toString();
char[] array_huruf = plainTextInput.toCharArray();
if(array_huruf.length%2!=0)
array_angka = new int[array_huruf.length+1];
else
array_angka = new int[array_huruf.length];
for (i=0 ; i < array_huruf.length ; i++){
int posisi_huruf = char_db.indexOf(array_huruf[i]);
//Toast.makeText(this, "Tombol ditekan " + posisi_huruf , Toast.LENGTH_SHORT).show();
array_angka[i] = posisi_huruf; //if I disable this line the code should run well
}
//jika panjang array ganjil letakkan spasi diakhir array
if(array_huruf.length % 2 != 0 ){
array_angka[array_huruf.length] = 62;
}
array_angka_cipher = new int[array_angka.length];
array_angka_cipher = encrypt_hill(M_key, array_angka);
ciphertext = to_char(array_angka_cipher);
txtCipher.setText(Arrays.toString(array_angka) +"\n\n" + Arrays.toString(array_angka_cipher) + "\n\n" + to_char(array_angka_cipher) );
}
else if (v == btnCheck){
key_input = txtKey.getText().toString();
if (key_input.length() < 4)
Toast.makeText(this, "panjang kunci harus 4 karakter!" , Toast.LENGTH_SHORT).show();
else{
check_kunci(key_input);
}
}
else if(v == btnDecrypt){
char[] array_cipher = ciphertext.toCharArray();
array_angka2 = new int[array_cipher.length];
for (i=0 ; i < array_cipher.length ; i++){
int posisi_huruf = char_db.indexOf(array_cipher[i]);
array_angka2[i] = posisi_huruf;
}
array_angka_decrypted = new int[array_cipher.length];
array_angka_decrypted = decrypt_hill(M_inverse, array_angka2);
plaintext = to_char(array_angka_decrypted);
txtDecrypt.setText(Arrays.toString(array_angka2) + "\n\n" + Arrays.toString(array_angka_decrypted) + "\n\n" + plaintext);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void check_kunci(String kunci){
Toast.makeText(this, kunci , Toast.LENGTH_SHORT).show();
char[] array_kunci = kunci.toCharArray();
int j = 0;
while ( j < array_kunci.length){
//masukkan masing-masing nilai ke matriks kunci
for (int x=0; x < 2; x++){
for (int y=0; y<2; y++){
M_key[x][y] = char_db.indexOf(array_kunci[j]);
j = j+1;
}
}
}
Toast.makeText(this, "hello " + M_key[0][0] + " " + M_key[0][1] + " " + M_key[1][0] + " " + M_key[1][1] , Toast.LENGTH_SHORT).show();
int det = determinant(M_key);
Toast.makeText(this, " determinant = " + det , Toast.LENGTH_SHORT).show();
if (det == 0){
Toast.makeText(this, "Kunci invalid karena determinant = 0" , Toast.LENGTH_SHORT).show();
} else if (det%67==0){
Toast.makeText(this, "Kunci invalid karena determinan memiliki persamaan faktor dgn 67" , Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Kunci Valid, multiplication inverse " + mi(det) , Toast.LENGTH_SHORT).show();
M_inverse = getInverse(M_key, mi(det));
Toast.makeText(this, "Inverse berhasil" + M_inverse[0][0] + " " + M_inverse[0][1] + " " + M_inverse[1][0] + " " + M_inverse[1][1] , Toast.LENGTH_SHORT).show();
String tampil_banding = M_key[0][0] + " " + M_key[0][1] + " " + M_key[1][0] + " " + M_key[1][1] +"\n\n" + M_inverse[0][0] + " " + M_inverse[0][1] + " " + M_inverse[1][0] + " " + M_inverse[1][1];
txtCipher.setText(tampil_banding);
}
}
public int[] encrypt_hill(int[][] key, int[] p){
int str_length;
str_length = txtPlain.length();
if(str_length%2!=0)
str_length = str_length+1;
int[] c = new int[str_length];
int i = 0;
int zz = 0;
for (int b=0; b< str_length/2 ; b++){
for(int j=0; j<2; j++){
for(int x=0; x<2 ; x++){
c[i] += key[j][x]*p[x+zz];
}
i++;
}
zz += 2;
}
return c;
}
private String to_char(int[] num_cipher){
int[] mod_result = new int[num_cipher.length];
char[] parse_cipher = new char[num_cipher.length];
for(int i=0;i<num_cipher.length;i++){
mod_result[i] = Math.abs(num_cipher[i]%67);
parse_cipher[i] = char_db.charAt(mod_result[i]);
}
String cipher_result = new String(parse_cipher);
return cipher_result;
}
public int determinant(int[][] A){
int res;
res = A[0][0]*A[1][1] - A[1][0]*A[0][1];
return res;
}
public int[][] getInverse(int key[][], int mi){
int[][] key_inv = new int[2][2];
key_inv[0][0] = ((key[1][1]*mi)%67);
key_inv[0][1] = (((-1*key[0][1])*mi)%67);
key_inv[1][0] = (((-1*key[1][0])*mi)%67);
key_inv[1][1] = ((key[0][0]*mi)%67);
return key_inv;
}
public int mi(int d)
{
int q,r1,r2,r,t1,t2,t;
r1=67;
r2=d;
t1=0;
t2=1;
while(r1!=1&&r2!=0)
{
q=r1/r2;
r=r1%r2;
t=t1-(t2*q);
r1=r2;
r2=r;
t1=t2;
t2=t;
}
return (t1+t2);
}
public int[] decrypt_hill(int[][] key_invers, int[] p){
int str_length;
str_length = txtPlain.length();
if(str_length%2!=0)
str_length = str_length+1;
int[] c = new int[str_length];
int i = 0;
int zz = 0;
for (int b=0; b< str_length/2 ; b++){
for(int j=0; j<2; j++){
for(int x=0; x<2 ; x++){
c[i] += key_invers[j][x]*p[x+zz];
}
i++;
}
zz += 2;
}
return c;
}
For now I'm suspecting that the problem comes from the determinant, mi (multiplicative inverse) and getInverse function. I really hope someone out there could help me to find out what's wrong with my code or my algorithm.
After all I really appreciate your attentions and intentions to help me. Thank you very much guys. I'm sorry if my code a little bit messy, because I'm still a beginner in this matters.
from wikipage http://en.wikipedia.org/wiki/Hill_cipher
Not all matrices have an inverse (see invertible matrix). The matrix will have an inverse if and only if its determinant is not zero, and does not have any common factors with the modular base. Thus, if we work modulo 26 as above, the determinant must be nonzero, and must not be divisible by 2 or 13. If the determinant is 0, or has common factors with the modular base, then the matrix cannot be used in the Hill cipher, and another matrix must be chosen (otherwise it will not be possible to decrypt). Fortunately, matrices which satisfy the conditions to be used in the Hill cipher are fairly common.
"abay" is not a valid key of hill cipher.
SORRY for not reading you code carefully.
try change
mod_result[i] = Math.abs(num_cipher[i]%67);
to
mod_result[i] = (((num_cipher[i]%67) + 67)%67);
or
mod_result[i] = num_cipher[i]%67;
if(mod_result[i] <0){
mod_result[i] += 67;
}
I'm working on a programm in which I want my object "this" will be an array of Point but I have this error when I run the programm and I'm not understand why.
Error --> DouglasPeucker.
My programm :
public class DouglasPeucker {
private double epsilon;
protected Point[] coinImage;
public DouglasPeucker(Point [] tab) {
this.coinImage = new Point[tab.length];
for(int i = 0; i < this.coinImage.length; i++) {
double abscisse = tab[i].getX();
double ordonnee = tab[i].getY();
System.out.println(abscisse + " " + ordonnee);
this.coinImage[i].setX(abscisse);
this.coinImage[i].setY(ordonnee);
}
}
You're never assigning a value to coinImage[i], so it will have its default value of null, which you're the dereferencing. You need something like:
for(int i = 0; i < this.coinImage.length; i++) {
double abscisse = tab[i].getX();
double ordonnee = tab[i].getY();
System.out.println(abscisse + " " + ordonnee);
this.coinImage[i] = new Point();
this.coinImage[i].setX(abscisse);
this.coinImage[i].setY(ordonnee);
}
Or preferrably, IMO:
for (int i = 0; i < this.coinImage.length; i++) {
// I'm assuming Point has a sensible constructor here...
coinImage[i] = new Point(tab[i].getX(), tab[i].getY());
// Insert the diagnostics back in if you really need to
}