Infix to Postfix ' StringIndexOutOfBoundsException' error [duplicate]What is IndexOutOfBoundsException? How can I fix it?infix to postfixInfix to Postfix using stacks and Precedence of OperatorsUnderstanding Postfix-expression Evaluation in Java code using a stackInfix to Postfix using stackconvert infix to postfixHow do I properly test whether my postfix expression is valid?Infix calculatorCreating a postfix converterDoes pushing a character onto a stack change the string it comes from?Infix to Postfix program doesn't work as intended
Why is oil used as as the lubricant in power generators, while water is the most available, cheapest and accessible lubricant?
Ethics: Is it ethical for a professor to conduct research using a student's ideas without giving them credit?
How would you idiomatically translate the French saying "Il n’y a pas d'amour, il n’y a que des preuves d’amour" to English?
Relationship between two graph optimization problems
Is there such a thing as Intrusive-L (as opposed to Intrusive-R)?
CEO says not to expect pay increases unless you do something really exceptional. Is this counter-productive?
Is there a preferred time in their presidency when US presidents pardon the most people?
I noticed an error in a graded exam during office hours. Should I give the student the lower grade?
Patent Agreement in Order to Graduate
How to remove solidified paste from toothbrush
SSD not reaching advertised speed
Why would one use "enter the name of the project to confirm"?
Was Haile Selassie the world's only involuntary messiah?
If a photon truly goes through both slits (at the same time), then why can't we detect it at both slits (at the same time)?
Should I present forged documents in a Penetration Test/Red team engagement?
How do you get the Super Rod in Pokémon Sword and Shield?
Consequences of eigenvector-eigenvalue formula found by studying neutrinos
Are conjugate vectors unique?
Using a sealant to stop a toilet tank leak
Mistake with Whole/Half step intervals problem
Locked folder with obscure app from Sourceforge, now cannot unlock folder
"Indexed" version of compactness and Axiom of Choice
What does "lequel" mean in this sentence, and how does the rest of its grammar operate?
A new type of builder pattern
Infix to Postfix ' StringIndexOutOfBoundsException' error [duplicate]
What is IndexOutOfBoundsException? How can I fix it?infix to postfixInfix to Postfix using stacks and Precedence of OperatorsUnderstanding Postfix-expression Evaluation in Java code using a stackInfix to Postfix using stackconvert infix to postfixHow do I properly test whether my postfix expression is valid?Infix calculatorCreating a postfix converterDoes pushing a character onto a stack change the string it comes from?Infix to Postfix program doesn't work as intended
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
This question already has an answer here:
What is IndexOutOfBoundsException? How can I fix it? [duplicate]
1 answer
I am setting up a method that turn a infix string into a postfix equation with a custom LinkStack.
I have tried to to check if the charAt(i) was null and a if statement to check if i is greater than exp.length() but neither worked.
public static String infixToPostfix(String exp)
// make variable
String result = new String("");
int temp = 0;
LinkedStack stack = new LinkedStack();
for (int i = 0; i<exp.length(); ++i)
char c = exp.charAt(i);
if(Character.isDigit(c))
int n = 0;
//extract the characters and store it in num
while(Character.isDigit(c))
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i); //exception occurs
System.out.println(n);
i--;
//push the number in stack
stack.push(n);
//System.out.println(stack.size() + ", Stack size");
// If ( push it to the stack.
if (c == '(')
stack.push(c);
// If ) pop and output from the stack
// until an '(' is encountered.
else if (c == ')')
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression"; // invalid expression
else
stack.pop();
else // an operator is encountered
while (!stack.isEmpty() && pre(c) <= pre((char) stack.peek()))
result += stack.pop();
stack.push(c);
// pop all the operators from the stack
while (!stack.isEmpty())
result += stack.pop();
String temp2 = stack.print();
System.out.println(temp2);
return result;
I expect the output to be 469 645 + if the input is 496+645 but the actual output is java.lang.StringIndexOutOfBoundsException: String index out of range: 7.
java indexoutofboundsexception postfix-notation infix-notation
marked as duplicate by Nathan Hughes
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 29 at 17:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment
|
This question already has an answer here:
What is IndexOutOfBoundsException? How can I fix it? [duplicate]
1 answer
I am setting up a method that turn a infix string into a postfix equation with a custom LinkStack.
I have tried to to check if the charAt(i) was null and a if statement to check if i is greater than exp.length() but neither worked.
public static String infixToPostfix(String exp)
// make variable
String result = new String("");
int temp = 0;
LinkedStack stack = new LinkedStack();
for (int i = 0; i<exp.length(); ++i)
char c = exp.charAt(i);
if(Character.isDigit(c))
int n = 0;
//extract the characters and store it in num
while(Character.isDigit(c))
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i); //exception occurs
System.out.println(n);
i--;
//push the number in stack
stack.push(n);
//System.out.println(stack.size() + ", Stack size");
// If ( push it to the stack.
if (c == '(')
stack.push(c);
// If ) pop and output from the stack
// until an '(' is encountered.
else if (c == ')')
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression"; // invalid expression
else
stack.pop();
else // an operator is encountered
while (!stack.isEmpty() && pre(c) <= pre((char) stack.peek()))
result += stack.pop();
stack.push(c);
// pop all the operators from the stack
while (!stack.isEmpty())
result += stack.pop();
String temp2 = stack.print();
System.out.println(temp2);
return result;
I expect the output to be 469 645 + if the input is 496+645 but the actual output is java.lang.StringIndexOutOfBoundsException: String index out of range: 7.
java indexoutofboundsexception postfix-notation infix-notation
marked as duplicate by Nathan Hughes
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 29 at 17:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment
|
This question already has an answer here:
What is IndexOutOfBoundsException? How can I fix it? [duplicate]
1 answer
I am setting up a method that turn a infix string into a postfix equation with a custom LinkStack.
I have tried to to check if the charAt(i) was null and a if statement to check if i is greater than exp.length() but neither worked.
public static String infixToPostfix(String exp)
// make variable
String result = new String("");
int temp = 0;
LinkedStack stack = new LinkedStack();
for (int i = 0; i<exp.length(); ++i)
char c = exp.charAt(i);
if(Character.isDigit(c))
int n = 0;
//extract the characters and store it in num
while(Character.isDigit(c))
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i); //exception occurs
System.out.println(n);
i--;
//push the number in stack
stack.push(n);
//System.out.println(stack.size() + ", Stack size");
// If ( push it to the stack.
if (c == '(')
stack.push(c);
// If ) pop and output from the stack
// until an '(' is encountered.
else if (c == ')')
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression"; // invalid expression
else
stack.pop();
else // an operator is encountered
while (!stack.isEmpty() && pre(c) <= pre((char) stack.peek()))
result += stack.pop();
stack.push(c);
// pop all the operators from the stack
while (!stack.isEmpty())
result += stack.pop();
String temp2 = stack.print();
System.out.println(temp2);
return result;
I expect the output to be 469 645 + if the input is 496+645 but the actual output is java.lang.StringIndexOutOfBoundsException: String index out of range: 7.
java indexoutofboundsexception postfix-notation infix-notation
This question already has an answer here:
What is IndexOutOfBoundsException? How can I fix it? [duplicate]
1 answer
I am setting up a method that turn a infix string into a postfix equation with a custom LinkStack.
I have tried to to check if the charAt(i) was null and a if statement to check if i is greater than exp.length() but neither worked.
public static String infixToPostfix(String exp)
// make variable
String result = new String("");
int temp = 0;
LinkedStack stack = new LinkedStack();
for (int i = 0; i<exp.length(); ++i)
char c = exp.charAt(i);
if(Character.isDigit(c))
int n = 0;
//extract the characters and store it in num
while(Character.isDigit(c))
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i); //exception occurs
System.out.println(n);
i--;
//push the number in stack
stack.push(n);
//System.out.println(stack.size() + ", Stack size");
// If ( push it to the stack.
if (c == '(')
stack.push(c);
// If ) pop and output from the stack
// until an '(' is encountered.
else if (c == ')')
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression"; // invalid expression
else
stack.pop();
else // an operator is encountered
while (!stack.isEmpty() && pre(c) <= pre((char) stack.peek()))
result += stack.pop();
stack.push(c);
// pop all the operators from the stack
while (!stack.isEmpty())
result += stack.pop();
String temp2 = stack.print();
System.out.println(temp2);
return result;
I expect the output to be 469 645 + if the input is 496+645 but the actual output is java.lang.StringIndexOutOfBoundsException: String index out of range: 7.
This question already has an answer here:
What is IndexOutOfBoundsException? How can I fix it? [duplicate]
1 answer
java indexoutofboundsexception postfix-notation infix-notation
java indexoutofboundsexception postfix-notation infix-notation
asked Mar 28 at 22:08
L0cusL0cus
14 bronze badges
14 bronze badges
marked as duplicate by Nathan Hughes
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 29 at 17:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Nathan Hughes
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 29 at 17:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Nathan Hughes
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 29 at 17:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
while(Character.isDigit(c))
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i); //exception occurs
System.out.println(n);
You aren't length checking here, so you readily parse right off the end of the string.
while(i < exp.length() && Character.isDigit(c))
n = n*10 + (int)(c-'0');
if (++i < exp.length())
c = exp.charAt(i); //exception occurs
System.out.println(n);
Note: I'd cache the length because of how many times you use it, but that's not the cause of your problem.
Note, however, that this is cleaner code style:
public class Foo
public static void main(String[] args)
String myString = "12345";
int index = 0;
for (char c: myString.toCharArray())
System.out.printf("Char at %d == %cn", index, c);
++index;
Notice the for-loop. I didn't do your calculations or break out or anything, but this is a cleaner way.
You can also do...
for (int index = 0; index < exp.length(); ++index)
char c = exp.charAt(index);
if (!Character.isDigit(c))
break;
// Do other stuff here.
There are a variety of other ways to structure your code. Your while loop is awkward.
That edit still gives me the same error.
– L0cus
Mar 28 at 22:21
Hmm. You're right. I've edited my answer.
– Joseph Larson
Mar 29 at 16:24
The shape of your code is a little odd. You can do a for-loop instead. I'll edit my answer once more.
– Joseph Larson
Mar 29 at 16:30
add a comment
|
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
while(Character.isDigit(c))
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i); //exception occurs
System.out.println(n);
You aren't length checking here, so you readily parse right off the end of the string.
while(i < exp.length() && Character.isDigit(c))
n = n*10 + (int)(c-'0');
if (++i < exp.length())
c = exp.charAt(i); //exception occurs
System.out.println(n);
Note: I'd cache the length because of how many times you use it, but that's not the cause of your problem.
Note, however, that this is cleaner code style:
public class Foo
public static void main(String[] args)
String myString = "12345";
int index = 0;
for (char c: myString.toCharArray())
System.out.printf("Char at %d == %cn", index, c);
++index;
Notice the for-loop. I didn't do your calculations or break out or anything, but this is a cleaner way.
You can also do...
for (int index = 0; index < exp.length(); ++index)
char c = exp.charAt(index);
if (!Character.isDigit(c))
break;
// Do other stuff here.
There are a variety of other ways to structure your code. Your while loop is awkward.
That edit still gives me the same error.
– L0cus
Mar 28 at 22:21
Hmm. You're right. I've edited my answer.
– Joseph Larson
Mar 29 at 16:24
The shape of your code is a little odd. You can do a for-loop instead. I'll edit my answer once more.
– Joseph Larson
Mar 29 at 16:30
add a comment
|
while(Character.isDigit(c))
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i); //exception occurs
System.out.println(n);
You aren't length checking here, so you readily parse right off the end of the string.
while(i < exp.length() && Character.isDigit(c))
n = n*10 + (int)(c-'0');
if (++i < exp.length())
c = exp.charAt(i); //exception occurs
System.out.println(n);
Note: I'd cache the length because of how many times you use it, but that's not the cause of your problem.
Note, however, that this is cleaner code style:
public class Foo
public static void main(String[] args)
String myString = "12345";
int index = 0;
for (char c: myString.toCharArray())
System.out.printf("Char at %d == %cn", index, c);
++index;
Notice the for-loop. I didn't do your calculations or break out or anything, but this is a cleaner way.
You can also do...
for (int index = 0; index < exp.length(); ++index)
char c = exp.charAt(index);
if (!Character.isDigit(c))
break;
// Do other stuff here.
There are a variety of other ways to structure your code. Your while loop is awkward.
That edit still gives me the same error.
– L0cus
Mar 28 at 22:21
Hmm. You're right. I've edited my answer.
– Joseph Larson
Mar 29 at 16:24
The shape of your code is a little odd. You can do a for-loop instead. I'll edit my answer once more.
– Joseph Larson
Mar 29 at 16:30
add a comment
|
while(Character.isDigit(c))
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i); //exception occurs
System.out.println(n);
You aren't length checking here, so you readily parse right off the end of the string.
while(i < exp.length() && Character.isDigit(c))
n = n*10 + (int)(c-'0');
if (++i < exp.length())
c = exp.charAt(i); //exception occurs
System.out.println(n);
Note: I'd cache the length because of how many times you use it, but that's not the cause of your problem.
Note, however, that this is cleaner code style:
public class Foo
public static void main(String[] args)
String myString = "12345";
int index = 0;
for (char c: myString.toCharArray())
System.out.printf("Char at %d == %cn", index, c);
++index;
Notice the for-loop. I didn't do your calculations or break out or anything, but this is a cleaner way.
You can also do...
for (int index = 0; index < exp.length(); ++index)
char c = exp.charAt(index);
if (!Character.isDigit(c))
break;
// Do other stuff here.
There are a variety of other ways to structure your code. Your while loop is awkward.
while(Character.isDigit(c))
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i); //exception occurs
System.out.println(n);
You aren't length checking here, so you readily parse right off the end of the string.
while(i < exp.length() && Character.isDigit(c))
n = n*10 + (int)(c-'0');
if (++i < exp.length())
c = exp.charAt(i); //exception occurs
System.out.println(n);
Note: I'd cache the length because of how many times you use it, but that's not the cause of your problem.
Note, however, that this is cleaner code style:
public class Foo
public static void main(String[] args)
String myString = "12345";
int index = 0;
for (char c: myString.toCharArray())
System.out.printf("Char at %d == %cn", index, c);
++index;
Notice the for-loop. I didn't do your calculations or break out or anything, but this is a cleaner way.
You can also do...
for (int index = 0; index < exp.length(); ++index)
char c = exp.charAt(index);
if (!Character.isDigit(c))
break;
// Do other stuff here.
There are a variety of other ways to structure your code. Your while loop is awkward.
edited Mar 29 at 16:32
answered Mar 28 at 22:19
Joseph LarsonJoseph Larson
1,8901 gold badge12 silver badges15 bronze badges
1,8901 gold badge12 silver badges15 bronze badges
That edit still gives me the same error.
– L0cus
Mar 28 at 22:21
Hmm. You're right. I've edited my answer.
– Joseph Larson
Mar 29 at 16:24
The shape of your code is a little odd. You can do a for-loop instead. I'll edit my answer once more.
– Joseph Larson
Mar 29 at 16:30
add a comment
|
That edit still gives me the same error.
– L0cus
Mar 28 at 22:21
Hmm. You're right. I've edited my answer.
– Joseph Larson
Mar 29 at 16:24
The shape of your code is a little odd. You can do a for-loop instead. I'll edit my answer once more.
– Joseph Larson
Mar 29 at 16:30
That edit still gives me the same error.
– L0cus
Mar 28 at 22:21
That edit still gives me the same error.
– L0cus
Mar 28 at 22:21
Hmm. You're right. I've edited my answer.
– Joseph Larson
Mar 29 at 16:24
Hmm. You're right. I've edited my answer.
– Joseph Larson
Mar 29 at 16:24
The shape of your code is a little odd. You can do a for-loop instead. I'll edit my answer once more.
– Joseph Larson
Mar 29 at 16:30
The shape of your code is a little odd. You can do a for-loop instead. I'll edit my answer once more.
– Joseph Larson
Mar 29 at 16:30
add a comment
|