|
Thank you so much for your great answer.
|
|
|
|
|
Hello. I have written a code like below for Valid Parentheses problem. and after running it, I get error such: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space in the line where I push in the stack. Could any one help what is the problem?
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
int i=0;
boolean result=false;
if(s.length()==0||s==null) return true;
while(i<s.length()){
if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='[')
stack.push(s.charAt(i));
if((!stack.empty())&&(s.charAt(i)==')'||s.charAt(i)=='}'||s.charAt(i)==']')){
if(stack.peek()==s.charAt(i)){
stack.pop();
i++;
continue;
}
else
result=false;
}
}
if(stack.empty())
return true;
return result;
}
}
|
|
|
|
|
I would guess that the two following if statements are not correct, so you are stuck in the while loop.
if((!stack.empty())&&(s.charAt(i)==')'||s.charAt(i)=='}'||s.charAt(i)==']')){
if(stack.peek()==s.charAt(i)){
So if you have just encountered an open bracket the first of these statements will be false, because i is still pointing to the open bracket in your string. The loop will just continue round examining the same character.
|
|
|
|
|
Hi,
1. what Richard said.
2. Your code is far from correct, there is no reason whatsoever to look more than once at the same character, so index i should get incremented unconditionally, hence a for-loop or better yet a foreach-loop would be preferable.
3. You shouldn't be calling CharAt() over and over, do it once and store the result in currentCharacter
4. What closes a "{" isn't another "{", it is a "}"; an easy way to get this, is by having separate test-and-push combinations (if it is "{" then push "}").
|
|
|
|
|
Elenora wrote: stack.push(s.charAt(i));
Only way that code can run out of memory is if that line is called way to many times.
|
|
|
|
|
i made a form that select file and provide key i want to encrypt that file by aes encrytion in jsp
my form code is:=
<html>
<body bgcolor="#006699" text=#006699 >
<center>
<br>
<div style="width:95%;border-style:solid;border-width:3px;border-color:white;height:600px;border-radius:15px;background-color:white;box-shadow:-1px -1px 20px black" >
<img src=../images/titlebar.jpg width=100% style="border-radius:15px" >
<h1>Encrypt Document</h1>
<hr color=orange size=3px>
<form name=f1 method=post action=uploadfile.jsp enctype="multipart/form-data" >
<input type=hidden name=demo value=demo >
<table>
<tr><td>Select File</td><td><input type=file name=filename required size=60 ></td></tr>
<tr><td>Enter Key</td><td><input type=text name=title size=80 required ></td></tr>
<tr><td></td><td><input type=submit name=btnencript value=Encrypt onclick=dosubmit() ></td></tr>
</table>
<hr color=orange size=3px>
</form>
</div>
</center>
</body>
</html>
|
|
|
|
|
|
Hey there,
I´m going to code breakout in java via the mvc pattern. And I have some problems with it.
Here´s my approach so far:
Model: coordinates of bricks, ball, panel and given coordinates by mouseListener
Controller: every physics, what happens with collisions, how to move the ball
View: every object that has to be drawn
But I´m not really cool with that because I´m not really confident about the implementation. And how do I put everything together to let the game run?
Grateful for any advice!
|
|
|
|
|
|
Posting a single word is not a question.
At least your repost of this in QA[^] provided some details of the question.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Does make for a fast read though.
|
|
|
|
|
Hi i am a beginner user of Java and I want to make an weather Java program which use gui and the openweathermap api. Can anyone help me???
|
|
|
|
|
|
1. Learn how to write a problem
2. Learn about sockets. Probably need to learn about http and Rest also (but I didn't look at the API.)
3. Learn about UI
4. Create a program that uses the weather API. This uses 1, 2
5. Create a program that displays the results. This uses 1, 2, 3, 4.
|
|
|
|
|
You want to write a desktop or web application?
|
|
|
|
|
Hello,
I am learning hoe to program in Java and have come up with some code to practice what I have learned. I have created a program to enter password and confirm it and the only way I have found to do this is to exit the program and try it again. Please help
Scanner scanner3 = new Scanner(System.in);
String passwordConfirm = scanner3.nextLine();
if (passwordConfirm.equals(password) ) {
System.out.println("Your password has been confirmed");
}
else {
System.out.println("The passwords do not match, please try again below");
Scanner scanner5 = new Scanner(System.in);
String passConfirm = scanner5.nextLine();
if (passConfirm.equals(password) ) {
System.out.println("The passwords now match, thank you");
scanner5.close();
}
else {
System.out.println("The passwords still do not match, please reload the app to try again");
System.exit(0);
}
}
Your help is greatly appreciated! I have the same problem with confirming email but once I figure out password, I can do email myself.
|
|
|
|
|
A do-while loop is perffect for that:
String passwordConfirm;
do {
System.out.println("Please enter password:");
passwordConfirm = scanner3.nextLine();
} while (!passwordConfirm.equals(password));
System.out.println("Your password has been confirmed");
will keep asking for password until the correct one is provided.
If you want only a fixed number of tries, you may have to declare a counter variable, increased in the loop, and tested in the while statement.
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
Hi there,
Thanks for the help. But could I ask where I place that code and the code I should replace with it? I can't seem to figure out where to put it and the code to erase. Thanks!
|
|
|
|
|
It could replace the whole code block you have shown, except the first line which declares the scanner3 variable.
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
Thanks a million, it has worked beautifully also with username. Thank you!
|
|
|
|
|
You are welcome.
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
|
I Need Java Compiler source code
|
|
|
|
|
|
install the JDK. and search for src file
"C:\Program Files\Java\jdk1.8.0_131\src.zip"
unzip the file and enjoy !!
|
|
|
|