Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Very new to Java, getting some syntax errors

  1. #1
    Junior Member
    Join Date
    Sep 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Very new to Java, getting some syntax errors

    Code for Main:
    1 import java.util.ArrayList;
    2 import java.util.Scanner;
    3 
    4 public class Main {
    5 
    6 public static void main(String[] args) {
    7       ArrayList<Trivia> Questions = new ArrayList<>();
    8       Questions.add(new Trivia("q1", "yes"));
    9       Questions.add(new Trivia("q2", "2"));
    10      Questions.add(new Trivia("q3", "5"));
    11      int score = DoTrivia(Questions);
    12      Results(score);
    13  }
    14
    15  public static int DoTrivia(ArrayList<> q) {
    16      int Score = 0;
    17      Scanner scanner = new Scanner(System.in);
    18      for (int i = 0; i < q.size(); i++) {
    19          System.out.println(q.get(i));
    20          String input = scanner.next();
    21          if (input.toLowerCase().equals(q.get(i).qAns)) {
    22              Score++;
    23              System.out.println("Correct! You have " + Score + " points!");
    24          } else {
    25              System.out.println("Incorrect. The answer was " + q.get(i) + ". You have " + Score + " points.");
    26          }
    27      }
    28      scanner.close();
    29      return Score;
    30  }
    31  public static void Results(int Score){
    32     if (Score >= 2) {
    33          System.out.println("You won with " + Score + " points!");
    34     } else {
    35          System.out.println("You lost with " + Score + " points.");
    36      }
    37  }
    38 }
    Errors for it:
    Identifier expected:15
    Cannot resolve symbol 'qAns':21


    Code for Trivia class...:
    1 public class Trivia {
    2   public Trivia(String QText, String qAns) {
    3
    4   }
    5 }
    Any ideas to better utilize the new class? (I made it as the new class was a requirement for the tutorial's exercise.)

    Thanks!
    Last edited by Ipplayz343; September 10th, 2022 at 07:49 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Very new to Java, getting some syntax errors

    Cannot resolve symbol 'QAns':21
    Where is the variable QAns defined that is in scope where it is referenced?

    The only declaration for QAns I see is as a local parameter to the Trivia class.
    It looks like that variable needs to be saved in a variable in that class and a getter method declared that returns its value.


    Note: import statements need to be before the class declaration.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very new to Java, getting some syntax errors

    Quote Originally Posted by Norm View Post
    Where is the variable QAns defined that is in scope where it is referenced?

    The only declaration for QAns I see is as a local parameter to the Trivia class.
    It looks like that variable needs to be saved in a variable in that class and a getter method declared that returns its value.
    So, QAns wasn't causing errors until converting the List to an ArrayList. Do you know what might have caused that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Very new to Java, getting some syntax errors

    Please copy the current code and paste it here.
    The posted code will not compile because the import statements are inside of the class. They should be before the class.

    Changing an ArrayList to a List should not cause the compiler error you got.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very new to Java, getting some syntax errors

    Problem is that is the code. If this might help, I'm running IntelliJ

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Very new to Java, getting some syntax errors

    A problem with the posted code is that the import statements are inside of the class declaration. They need to be moved outside.
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class Main {
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very new to Java, getting some syntax errors

    They are already outside, when moving the code from IntelliJ to here I messed up that, i should have detected that lol.
    So, when using normal lists, doing q[i].QAns wouldn't throw errors. The problem was that Questions was an ArrayList, but doTrivia would only accept lists, so i converted everything to arraylist syntax, but now it's throwing that error.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Very new to Java, getting some syntax errors

    it's throwing that error
    Please copy the full text of the error message and paste it here.

    doing q[i].QAns wouldn't throw errors.
    Can you post the code that did not throw errors? The variable: QAns must have been declared in scope for the code to compile without errors.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very new to Java, getting some syntax errors

    errors in full:
    Identifier expected :15
    Cannot resolve symbol 'qAns' :21
    Unfortunately, my brainlet self didn't to archive older versions

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Very new to Java, getting some syntax errors

    Identifier expected :15
    What line is line 15?

    Cannot resolve symbol 'qAns' :21
    Where is qAns declared? I do see the symbol qAns used in the posted code. What code did that come from?
    I see a similar named variable: QAns

    I therefore assume you did not copy the full text of the compiler's error messages and paste it, but rather typed them in and made some mistakes which make it hard to solve the problem if the posted error messages are not correct.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very new to Java, getting some syntax errors

    ill modify the main post to fix the confusions

    --- Update ---

    I have modified the main post, hopefully this should help

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Very new to Java, getting some syntax errors

    public static int DoTrivia(ArrayList<> q) {
    There must be a class name within the <> to declare what type the list contains

    if (input.toLowerCase().equals(q.get(i).qAns)) {
    Where is qAns declared? It must be in scope (declared within the {}s) where it referenced.
    See my post#2
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: November 26th, 2021, 08:48 AM
  2. Syntax Highlighting errors and weirdness?
    By sci4me in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 27th, 2013, 10:25 PM
  3. 2nd week with Java. Help with a few basic syntax errors?
    By D P in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 28th, 2012, 11:13 PM
  4. Java Equation correct syntax
    By macko in forum What's Wrong With My Code?
    Replies: 18
    Last Post: November 4th, 2011, 11:16 PM
  5. java syntax highlighting
    By Saulius in forum Java Theory & Questions
    Replies: 2
    Last Post: August 24th, 2011, 05:47 AM