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++){
Related
I created a little code with visual interface using JFrame... you can type in a date and hit the conformation button. The problem is that I can't return the values out of the ActionListener... cause its a void of cause, I also tried to declare the variables outside of the ActionListener, but I get an Error like not possible from inner function or so. Any Ideas?
Heres a part of my code and a screenshot:
DVDAddenButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//((DefaultListModel)Liste.getModel()).addElement();
String Mediumtitel = DVDTextfeld.getText();
int DVDAusleihdatumZahlTag = 0;
int DVDAusleihdatumZahlMonat = 0;
int DVDAusleihdatumZahlJahr = 0;
String DVDAusleihdatumStringTag = "";
String DVDAusleihdatumStringMonat = "";
String DVDAusleihdatumStringJahr = "";
DVDAusleihdatumStringTag = DVDAusleihdatumFeldTag.getText();
DVDAusleihdatumStringMonat = DVDAusleihdatumFeldMonat.getText();
DVDAusleihdatumStringJahr = DVDAusleihdatumFeldJahr.getText();
try{
DVDAusleihdatumZahlTag = Integer.parseInt(DVDAusleihdatumStringTag);
DVDAusleihdatumZahlMonat = Integer.parseInt(DVDAusleihdatumStringMonat);
DVDAusleihdatumZahlJahr = Integer.parseInt(DVDAusleihdatumStringJahr);
}
catch(NumberFormatException ex){}
if (DVDAusleihdatumZahlTag < 32 && DVDAusleihdatumZahlTag > 0 && DVDAusleihdatumZahlMonat < 13 && DVDAusleihdatumZahlMonat > 0 && DVDAusleihdatumZahlJahr < date.getJahr()+1){
model.addElement(Mediumtitel);
model.addElement("Ausgeliehen am: " + DVDAusleihdatumZahlTag + "." + DVDAusleihdatumZahlMonat + "." + DVDAusleihdatumZahlJahr);
model.addElement(" ");
DatumFehler.setVisible(false);
}
else{
DatumFehler.setVisible(true);
}
}
});
enter image description here
Sorry for messed up code..
You can make a new class, then create the class inside ActionListener, then your class stores the information. When you want to access it, just call it from the class.
how can i scroll a view (recyclerview) in relation to my tts,
ive looked at onUtterance but it seems to only have a start and stop listener, so i need to think outside the box, i give my tts a string from an Arraylist like this
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < SpeakRecyclerGrid.recyclerView.getChildCount(); i++)
{
list.add(((EditText) SpeakRecyclerGrid.recyclerView.getChildAt(i)).getText().toString());
}
speakWords(words);
I was thinking about cutting the string up into sections and giving it to the TTS one string at a time and move the view as I go. I already give my gridlayout manager an int for the amount of columns (called columns).
The array list adds a comma after every word, so I was thinking something like
find the nth/(column) comma
split the string
check if tts is speaking and listen for onUtterance onDone to pass new string
read the string
move the view
and keep doing this until theres no words left and coding for the remainder % im not sure how to do all of that so if anyone wants to help feel free, (I think Im looking at StringUtils and creating each smaller string with a for loop and passing them to the tts in onUtteranceListener onDone, but im still a little new to android), but mainly does anyone have a better way
okay this is what i have so far but it could probably use some work im still an amateur, I've split the string into blocks based on the size of the screen, and the handler that scrolls the screen relies on the amount of letters in each broken up string, also the smoothscrolltoposition is scrolling a custom layout manager that scrolls pretty slowly im having an issue though where on some devices the array list counting the words will only reach 20 not sure why but ill ask and hopefully update this when ive fixed it so here is my speak and move method
public void speakAndMove(){
final ArrayList<String> list = new ArrayList<>();
SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(0);
for (int i = 0; i < SpeakRecyclerGrid.recyclerView.getChildCount(); i++) {
list.add(((EditText) SpeakRecyclerGrid.recyclerView.getChildAt(i)).getText().toString());
}
Integer numOfWords = list.size();
words = list.toString();
Integer count = 0;
Integer startPoint = 0;
scrollPos = 0;
final Integer speed = words.length() * 15;
Integer leftOver = 0;
final int columns = getResources().getInteger(R.integer.grid_columns);
System.out.println(numOfWords);
ArrayList<String> arrayList = new ArrayList<>();
if (list.size() <= columns) {
if (words.contains("[]")) {
speakWords("");
} else if (words.contains(", 's")) {
formatString = words.replaceFirst(", 's", "'s");
speakWords(formatString);
} else if (words.contains(", ing")) {
formatString = words.replaceFirst(", ing", "ing");
speakWords(formatString);
} else {
speakWords(words);
}
}
if (list.size()>=columns) {
for (int i = 0; i < words.length(); i++) {
if (words.charAt(i) == ',') {
count++;
if (count == columns) {
String ab = words.substring(startPoint, i + 1);
//speakWords(ab);
if (ab.contains(", 's")) {
formatString = ab.replaceFirst(", 's", "'s");
speakWords(formatString);
} else if (ab.contains(", ing")) {
formatString = ab.replaceFirst(", ing", "ing");
speakWords(formatString);
} else {
speakWords(ab);
}
startPoint = i + 1;
count = 0;
leftOver = words.length() - startPoint;
}
//SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(scrollPos);
System.out.println("main string"+" scroll " + scrollPos + " startpoint " + startPoint +" speed " + speed);
}
}
if (leftOver > 0) {
String ab2 = words.substring(startPoint, words.length());
//speakWords(ab2);
if (ab2.contains(", 's")) {
formatString = ab2.replaceFirst(", 's", "'s");
speakWords(formatString);
} else if (ab2.contains(", ing")) {
formatString = ab2.replaceFirst(", ing", "ing");
speakWords(formatString);
} else {
speakWords(ab2);
}
//SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(scrollPos);
System.out.println("leftovers "+ leftOver + " scroll " + scrollPos + " startpoint " + startPoint +" count " + scrollPos);
count = 0;
//scrollPos = 0;
}
}
final Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
scrollPos = scrollPos + columns;
SpeakGrid.recyclerView.getLayoutManager().smoothScrollToPosition(SpeakGrid.recyclerView, null ,scrollPos);
System.out.println("position "+ scrollPos + " speed " + speed + " list size " + list.size());
if (scrollPos < list.size())
h.postDelayed(this,speed);
// close this activity
}
}, speed);
}
How can I pass a parameter to a Thread?
The statement log( "before process , counter = " + i);
causes an error:
Cannot refer to a non-final variable i inside an inner class defined in a different method
Please Help
for (int i = 0; i < 20; i++) {
Thread thread = ThreadManager.createThreadForCurrentRequest(new Runnable() {
public void run() {
try {
log("before process , counter = " + i);
Thread.sleep(1000);
log("after process , " + "counter = " + i);
} catch (InterruptedException ex) {
throw new RuntimeException("Interrupted in loop:", ex);
}
}
});
thread.start();
}
As it said, just a final variable. That tells Java that it will not be changed and it can safely be used in the Runnable.
for (int i = 0; i < 20; i++) {
final int counter=i;
You need to do this:
for (int i = 0; i < 20; i++) {
final int counter = i;
// etc
}
then use counter in your thread instead of i (which, as the error message tells you, is not final, thus cannot be used in this situation).
I have done this till now
public void addStorage() {
stockStore = Storage.getLocalStorageIfSupported();
if (stockStore != null) {
stockStore.setItem(("Index" + index), ("state" + HistoryCount));
stockMap.put(("Index" + index), ("state" + HistoryCount));
}
}
public void loadStorage() {
String s;
stockStore = Storage.getLocalStorageIfSupported();
if (stockStore != null) {
stockMap = new
StorageMap(stockStore);
for (int i = 0; i < stockStore.getLength(); i++) {
if (stockMap.containsValue(index)) {
s = stockStore.getItem("Index" + index);
state = stateRecord.get(s);
clearHighlights();
setState(state);
break;
}
}
}
}
I have no idea what am I missing. These two functions are called by their handlers. Load and Save. load storage will load the stored state of chess and save will save the current chess state.
Possibly you have error here in stockMap.containsValue(index) but should be stockMap.containsValue("Index" + index), the corrected version:
for (int i = 0; i < stockStore.getLength(); i++) {
if (stockMap.containsValue("Index" + index)) {
s = stockStore.getItem("Index" + index);
state = stateRecord.get(s);
clearHighlights();
setState(state);
break;
}
}
I am not able to update the value of a global variable in a function which is called continuously by a thread
The function code is:
public void readMessages()
{
if (srv.getServiceStatus() == Service.ServiceStatus.STARTED) {
try {
InboundMessage msg = null;
java.util.LinkedList<InboundMessage> list = new java.util.LinkedList<InboundMessage>();
srv.readMessages(list, InboundMessage.MessageClasses.UNREAD);
int checkArray = list.size();
for (int i = 0; i < list.size(); i++) {
msg = list.get(i);
System.out.println("New incoming message from: " + msg.getOriginator() +" : \t" + msg.getText() + "\n");
saveSMS(msg.getOriginator(),msg.getText());
if (checkArray == 0) {
messageArray = new String [4];
for (int j = 0 ; j<4 ; j++) {
messageArray[j] = "";
}
}
if (noOfSms < 4) {
messageArray[noOfSms] = msg.getText();
noOfSms = noOfSms + 1;
}
if (noOfSms == 3) {
Receiver r = new Receiver ();
r.Processing_ReceivedSMS(msg.getText(),msg,messageArray);
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
Here noOfSms is the global variable but its value does not change.
The function from which readMessage is called is this:
public void run(){
while (true){
readMessages();
try {
t.sleep(5000);
user_status=2;
} catch (InterruptedException e) {
System.out.println("Thread Pause Exception");
}
}
}
What's the reason behind it and what to do about it?
Since you invoke this method from thread/s there are two reasons why your variable does not get updated.
the code inside readMessages() might throw any Exception before your variable gets updated
there is a possibility that your variable never updates because it is located inside if blocks. Check the initial value of it so it can pass the if-condition