Off Topic > Off Topic
Programming Megathread
Legodude77:
So I'm working on some simple java project for class just to get situated with it again after winter break. I'm trying to traverse a string so that if there is a certain character at any position it gets pushed to a stack. Now I am having trouble with using charAt for some reason. My string is called str and whenever I try calling str.charAt[z] I'm always getting this error:
--- Code: --- cannot find symbol
if (str.charAt[z] == '(' )
(Then it points to the . between str and charAt)
symbol: variable charAt
location: variable str of type string
--- End code ---
I'm guessing I'm not using charAt correctly? Like I said it's been a month since I've done java.
BluetoothBoy:
--- Quote from: Legodude77 on January 26, 2016, 08:05:17 PM ---So I'm working on some simple java project for class just to get situated with it again after winter break. I'm trying to traverse a string so that if there is a certain character at any position it gets pushed to a stack. Now I am having trouble with using charAt for some reason. My string is called str and whenever I try calling str.charAt[z] I'm always getting this error:
--- Code: --- cannot find symbol
if (str.charAt[z] == '(' )
(Then it points to the . between str and charAt)
symbol: variable charAt
location: variable str of type string
--- End code ---
I'm guessing I'm not using charAt correctly? Like I said it's been a month since I've done java.
--- End quote ---
No, you're using it correctly from what I can see here... Can you post the rest of your code?
Legodude77:
--- Code: ---String str = scan.next();
for (int i = 0; i < str.length(); i++)
if (str.charAt[i] == '(' )
stackVaribleName.push(str.charAt[i]);
--- End code ---
This will also give me the same error at the last line.
BluetoothBoy:
--- Quote from: Legodude77 on January 26, 2016, 09:07:28 PM ---
--- Code: ---String str = scan.next();
for (int i = 0; i < str.length(); i++)
if (str.charAt[i] == '(' )
stackVaribleName.push(str.charAt[i]);
--- End code ---
This will also give me the same error at the last line.
--- End quote ---
Surely that's not the rest of the code... Can you just copy-paste it? Sometimes errors don't actually come from where the compiler tells you they do.
Legodude77:
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 ---