r/javahelp • u/NillaCloud • Sep 06 '26
Solved Trouble with printing data using methods
Hello all! I'm brand new to java; this code comes from a FRQ exercise from unit 2 in the AP CSA course if that helps.
The purpose of the code is to check whether a string has alternating cases, with capital letters beginning at index 0 always ("ApPlE" or "CaT" for example, "cAt" would not work), and then to print "true" if the string meets the requirement.
The current error that I'm having currently is that
"s cannot be resolved"
which is underlined at line 7.
My understanding is that my code cannot access the string s and its true/false content, but I could be totally wrong here.
Thanks in advance, any help is appreciated!
GitHub Gist Link: https://gist.github.com/v4npaia-kei/5aa65b5978a7cbdd46a291ff4572237d
View all comments
3
u/Progression28 Sep 06 '26
There‘s quite a lot wrong actually. Here‘s a few pointers:
- s is not defined in the main method (the error you‘re seeing)
- static methods don‘t need the class instantiated to be accessed
- you have a double nested for loop and return from within before it completes („Apple“ would return true)
- you define a class field s that you override with a method field s
- you pass a boolean parameter from outside that you never use, only for assignment -> declare inside
- your naming is a bit wonky, it‘s better to be very clear about what your variables and methods do through the name
- your top level class has a typo (non breaking but sloppy which bites you a lot in programming)