Off Topic > Off Topic
Programming Megathread
BluetoothBoy:
--- Quote from: Legodude77 on January 26, 2016, 09:30:28 PM ---Sorry, I'm writing it through a putty console to my school's linux server, or else I would have posted the whole thing right then. Give me one moment though.
--- Code: ---import java.util.Scanner;
public class StackTester
{
CharStack testStack = new CharStack();
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a string:");
String str = scan.next();
for (int i = 1; i < str.length(); i++)
if (str.charAt[i] == '(')
testStack.push(str.charAt[i]);
}//main
--- End code ---
--- End quote ---
First of all, wouldn't you want int i = 0; instead? charAt begins at index 0.
As for your problem, here's working code:
--- Code: ---import java.util.Scanner;
import java.util.Stack;
class StackTester
{
public void main()
{
Stack<Character> testStack = new Stack<Character>();
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a string:");
String str = scan.next();
for (int i = 1; i < str.length(); i++)
{
if (str.charAt(i) == '(')
{
testStack.push(str.charAt(i));
}
}
}
}//main
--- End code ---
You had three problems.
1) You used brackets "[]" instead of parentheses after charAt.
2) You did not import Java's stack class.
3) The stack was declared incorrectly.
Hope this helps!
portify:
--- Quote from: ZSNO on January 26, 2016, 03:12:06 PM ---Name 10 out of your 100 reasons as to why we should still be capped at 80 width.
--- End quote ---
1. if you have a line of code that needs to longer then generally your code is getting too complicated to the point where you're jamming many parts of an expression onto a single line just because you can (or you're writing NSAPI code forget that)
if you have a function which has so many arguments that it makes callsite lines that long then either 1) don't use so many positional arguments for a function or 2) put arguments on different lines with clear indentation from the first
Legodude77:
Thanks for the help! We are making a stack from scratch using arrays so that's why the stack was weird.
ZSNO:
--- Quote from: portify on January 27, 2016, 02:56:51 AM ---1. if you have a line of code that needs to longer then generally your code is getting too complicated to the point where you're jamming many parts of an expression onto a single line just because you can (or you're writing NSAPI code forget that)
if you have a function which has so many arguments that it makes callsite lines that long then either 1) don't use so many positional arguments for a function or 2) put arguments on different lines with clear indentation from the first
--- End quote ---
Usually when ever I go over 80 width, it's generally when I'm writing comments or strings to output. I agree that you shouldn't have like a bazillion things in your function call, but 80 width is still rather short for this time and age.
BluetoothBoy:
--- Quote from: Legodude77 on January 27, 2016, 08:50:08 AM ---Thanks for the help! We are making a stack from scratch using arrays so that's why the stack was weird.
--- End quote ---
Oh, ok. That makes more sense. ;)