while(counter != this.data.size()) {
for(int i=0; i <= p.data.size(); i++) {
double coefficient = first.getData().getCoefficient() * sec.getData().getCoefficient();
int degree = first.getData().getDegree() + sec.getData().getDegree();
Term current = new Term(coefficient,degree);
if(ans.data.isEmpty()) {
ans.data.addFirst(current);
}
else {
boolean found = false;
DNode<Term > tmp = ans.data.getFirst();
while(found != true) {
if(tmp.getData() == null) {
ans.data.addLast(current);
found = true;
}
else if(current.getDegree() > tmp.getData().getDegree()) {
ans.data.addBefore(current, tmp);
found = true;
}
else if(current.getDegree() == tmp.getData().getDegree()) {
double co = current.getCoefficient() * tmp.getData().getCoefficient();
int deg = current.getDegree() + tmp.getData().getDegree();
tmp.getData().setCoefficient(co);
tmp.getData().setDegree(deg);
found = true;
}
System.out.println("DID I GET HERE");
tmp = tmp.getNext();
}
System.out.println("DID I GET HERE2");
}
System.out.println("DID I GET HERE3");
sec = sec.getNext();
}
System.out.println("DID I GET HERE4");
first = first.getNext();
counter++;
}
My code doesnt even reach the did i get here4 for some reason can anyone tell me why?
I am having a lot of difficulty as this project is due later today the code is suppose to multiply 2 polynomials
I am trying to create a function that does the following:
Assuming that the code input is "(a 1 2 (b 3 4 5 (c 6) |7) 8 9)"
where the pipe | symbol is the position of the cursor,
the function returns:
a String "b 3 4 5 (c 6) 7" representing the code that is in the scope of the cursor
an int 8 representing the start index of the string relative to the input
an int 30 representing the end index of the string relative to the input
I already have working code that returns exactly that. However, the problem lies in ignoring comments, while keeping track of context (e.g. String literals, my own literal delimiters, etc).
Here is the code which keeps track of context:
public static void applyContext(Context context, String s, String snext, String sprev) {
if (s.equals("\"")) {
if (context.context == Context.Contexts.MAIN) {
context.context = Context.Contexts.STRING;
context.stringDelimiterIsADoubleQuote = true;
} else if (context.context == Context.Contexts.STRING && context.stringDelimiterIsADoubleQuote && !sprev.equals("\\"))
context.context = Context.Contexts.MAIN;
} else if (s.equals("\'")) {
if (context.context == Context.Contexts.MAIN) {
context.context = Context.Contexts.STRING;
context.stringDelimiterIsADoubleQuote = false;
} else if (context.context == Context.Contexts.STRING && !context.stringDelimiterIsADoubleQuote && !sprev.equals("\""))
context.context = Context.Contexts.MAIN;
} else if (s.equals("/") && snext.equals("/")) {
if (context.context == Context.Contexts.MAIN)
context.context = Context.Contexts.COMMENT;
} else if (s.equals("\n")) {
if(context.context == Context.Contexts.COMMENT)
context.context = Context.Contexts.MAIN;
}
else if (s.equals("\\")) {
if(context.context == Context.Contexts.MAIN)
context.context = Context.Contexts.PATTERN;
else if(context.context == Context.Contexts.PATTERN)
context.context = Context.Contexts.MAIN;
}
}
Firstly, I'll be using the function above like so:
String sampleCode = "(a b "cdef" g \c4 bb2 eb4 g4v0.75\)";
Context c = new Context(Context.Contexts.MAIN);
for(int i = 0; i < sampleCode.length(); i++) {
String s = String.valueOf(sampleCode.charAt(i));
String snext = *nullcheck* ? String.valueOf(sampleCode.charAt(i + 1)) : "";
String sprev = *nullcheck* ? String.valueOf(sampleCode.charAt(i - 1)) : "";
applyContext(c, s, snext, sprev);
if(c.context == blahlbah) doBlah();
}
Second, I'll be using this both forwards an backwards, as the current method of doing the function stated at the top of the description is (in pseudocode) this:
function returnCodeInScopeOfCursor(theWholeCode::String, cursorIndex::int) {
var depthOfCodeAtCursorPosition::int = getDepth(theWholeCode, cursorIndex);
Context c = new Context(getContextAt(theWholeCode, cursorIndex));
var currDepth::int = depthOfCodeAtCursorPosition;
var startIndex::int, endIndex::int;
for(i = cursorIndex; i >= 0; i--) {//going backwards
s = .....
snext = ......
sprev = ......
applyContext(c, s, snext, sprev);
if(c.context == Context.MAIN) {
if s = "(" then currDepth --;
if s = ")" then currDepth ++;
}
when currDepth < depthOfCodeAtCursorPosition
startIndex = i + 1;
break;
}
currDepth = depthOfCodeAtCursorPosition;//reset
for(i = cursorIndex; i < theWholeCode.length; i++) {//going forwards
s = ...
snex......
sprev.....
applyContext(c, s, snext, sprev);
if(c.context == Context.MAIN) {
if s = "(" then currDepth ++;
if s = ")" then currDepth --;
}
when currDepth < depthOfCodeAtCursorPosition
endIndex = i - 1;
break;
}
var returnedStr = theWholeCode->from startIndex->to endIndex
return new IndexedCode(returnedStr, startIndex, endIndex);
As you can see, this function would work both forwards and in reverse. Or at least most of it. The only problem is that if I were to use this function backwards, the proper scanning of comments (denoted by the standard ECMA double slash "//") goes haywire.
If I were to create a separate function for reverse context application and check every line recursively for a double slash, then making everything after that '//' a COMMENT (or in the direction of the function's usage, everything before that //), it will take way too much processing time as I want to use this as a livecoding environment for music.
Also, removing the comments before trying to do that returnCodeInScopeOfCursor method may not be feasible... as I need to keep track of the indexes of the code and what not. If I were to remove the comments, there will be a big mess with all the code positions and keeping track of where did I remove what exactly and how many characters etc....
The text area input GUI I'm working with (RichTextFX) does not support Line-Char tracking, so everything is tracked using char index only, hence the problems...
So... I'm utterly perplexed as with what to do with my current code. Any help, suggestions, advice etc... will be greatly appreciated.
Could you pre-transform comments from // This is a comment<CR> to { This is a comment}<CR> you then have a language you can walk backwards and forwards.
Apply this transform on the way in and reverse it on the way out and all should be well. Notice we are replacing //... with {...} so all charaqcter offsets are retained.
Anyways, after a little experimenting with OldCurmudgeon's idea, I came up with a separate function to get context of the code in a reverse direction.
public static void applyContextBackwards(Context context, String entireCode, int caretPos) {
String s = String.valueOf(entireCode.charAt(caretPos));
//So far this is not used
//String snext = caretPos + 1 < entireCode.length() ? String.valueOf(entireCode.charAt(caretPos + 1)) : "";
String sprev = caretPos - 1 >= 0 ? String.valueOf(entireCode.charAt(caretPos - 1)) : "";
//Check for all the flags and what not...
if(context.commentedCharsLeft > 0) {
context.commentedCharsLeft--;
if(context.commentedCharsLeft == 0)
context.context = Context.Contexts.MAIN;//The comment is over
}
if(context.expectingEndOfString){
context.context = Context.Contexts.MAIN;
context.expectingEndOfString = false;
}
if(context.expectingEndOfPattern) {
context.context = Context.Contexts.MAIN;
context.expectingEndOfPattern = false;
}
//After all the flags are cleared, do this
if(context.commentedCharsLeft == 0) {
if (s.equals("\"")) {
if (context.context == Context.Contexts.MAIN) {
context.context = Context.Contexts.STRING;
context.stringDelimiterIsADoubleQuote = true;
} else if (context.context == Context.Contexts.STRING && context.stringDelimiterIsADoubleQuote && !sprev.equals("\\"))
context.expectingEndOfString = true;//Change the next char to a MAIN, cuz this one's still part of the string
} else if (s.equals("\'")) {
if (context.context == Context.Contexts.MAIN) {
context.context = Context.Contexts.STRING;
context.stringDelimiterIsADoubleQuote = false;
} else if (context.context == Context.Contexts.STRING && !context.stringDelimiterIsADoubleQuote && !sprev.equals("\""))
context.expectingEndOfString = true;//Change the next char to a MAIN, cuz this one's still part of the string
} else if (s.equals("\n")) {
int earliestOccuranceOfSingleLineCommentDelimiterAsDistanceFromThisNewLine = -1;//-1 for no comments
//Loop until the next \n is found. In the process, determine location of comment if any
for(int i = caretPos; i >= 0; i--) {
String curr = String.valueOf(entireCode.charAt(i));
String prev = i - 1 >= 0 ? String.valueOf(entireCode.charAt(i - 1)) : "";
if(curr.equals("\n"))
break;//Line has been scanned through
if(curr.equals("/") && prev.equals("/"))
earliestOccuranceOfSingleLineCommentDelimiterAsDistanceFromThisNewLine = caretPos - i;
}
//Set the comment context flag
//If no comments, -1 + 1 will be 0 and will be treated as no comments.
context.commentedCharsLeft = earliestOccuranceOfSingleLineCommentDelimiterAsDistanceFromThisNewLine + 1;
if(earliestOccuranceOfSingleLineCommentDelimiterAsDistanceFromThisNewLine > 0) {
context.context = Context.Contexts.COMMENT;
}
} else if (s.equals("\\")) {
if (context.context == Context.Contexts.MAIN)
context.context = Context.Contexts.PATTERN;
else if (context.context == Context.Contexts.PATTERN)
context.expectingEndOfPattern = true;//Change the next char to a MAIN cuz this one's still part of the Pattern
}
}
}
I am trying to check an array for a 1 or a 3, if either is found, print false, else print true. I ahve this:
if(array[i] == 1){
bool = false;
}
else if(array[i] == 3){
bool = false;
}
else{
bool = true;
}
However it does not work in all cases.
If the input is 0 2 4 it prints true as it should.
but if the input is 4 2 7 1 8 it should be false as there is a 1, but it prints true.
What am i doing wrong?
EDIT: Added a break to the if and changed the if statement. Works now.
if(array[i] == 1 || array[i] == 3){
bool = false;
break;
}
else{
bool = true;
}
A better approach is
boolean flag = true;
for(int i=0; i<myArray.length; i++)
{
if(myArray[i]==1 || myArray[i]==3){
flag = false;
break;
}
}
return flag;
this code
if(array[i] == 1 || array[i] == 3){
bool = false;
break;
}
else{
bool = true;
}
equal
if(array[i] == 1){
bool = false;
}else{
bool = true;
}
if(array[i] == 3){
bool = false;
}
else{
bool = true;
}
Here is my code... can someone please tell me what is wrong?
void keyPressed() {
if (key == '\n') {
equation = typing;
switch (equation.charAt(2)) {
case "-":
if (equation.charAt(3) == "x") {
math[0] = -1;
};
else {
math[0] = int(equation.charAt(3) * -1);
};
}
}
}
I don't understand what is wrong. All of the braces match up. Is it that you can't use an if else inside a switch???
try replacing
if (equation.charAt(3)=="x") {
math[0] = -1;
};
else {
math[0] = int(equation.charAt(3)*-1);
};
with
if (equation.charAt(3)=='x') { // NOTE character comparision
math[0] = -1;
}
else {
math[0] = int(equation.charAt(3)*-1);
}
I am coding a plugin for a game. This is probably a really simple answer and I have stared at it for about 15 minutes and cannot figure out what the error is on this else statement. It is after an if statement but Eclipse still says that there is a syntax error. I'm sure that this is an easy fix but I is eluding me all the same. Thank you in advance.
#EventHandler (priority = EventPriority.HIGHEST)
public void onPlayerChat(AsyncPlayerChatEvent event)
{
Player sender = event.getPlayer();
String sentmessage = event.getMessage();
if (sentmessage.charAt(0) == '#')
{
event.setCancelled(true);
String message = "";
ArrayList<String> recipients = new ArrayList<String>();
recipients.add("");
int nor = 0;
for (int x = 1; true; x++)
{
if (sentmessage.charAt(x) != ' ')
{
recipients.set(nor, recipients.get(nor) + sentmessage.charAt(x));
}
else if (sentmessage.charAt(++x) == '#');
{
nor++;
recipients.ensureCapacity(nor);
recipients.add("");
}
else //Syntax error is on this else statement
{
message = sentmessage.substring(x);
break;
}
}
plugin.privateMessage(recipients, sender, message);
}
}
else if (sentmessage.charAt(++x) == '#');
Remove ; at end
should be:
else if (sentmessage.charAt(++x) == '#'){
.........
}