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 5 of 5

Thread: HELP: My program is having an error which says "cannot find symbol", but the symbols are actually there, I think I placed it in the wrong place. Please tell me where I got it wrong.

  1. #1
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default HELP: My program is having an error which says "cannot find symbol", but the symbols are actually there, I think I placed it in the wrong place. Please tell me where I got it wrong.

    I have to make a program using a one-dimensional array that accepts as input an integer value asking for the number of elements for each list. This will be used to generate random numbers (10-99) for the one-dimensional arrays (list a and list b). The program will compute for the product and store it in another array (list c).

    Example of an output that I should get should be:

    Input the number of elements: 3
    List A [space] List B [space] List C
    34 [---space---] 15 [-space-] 510
    64 [---space---] 25 [-space-] 1600
    11 [---space---] 50 [-space-] 550


    But the program I did is having errors in my last statement saying that: cannot find the symbol i and j
    Can you tell me where I got it wrong?

    Here's my program:

    import java.util.* ;
    public class onedimensional_array{
       public static void main(String[] args){
          Random rand = new Random();
          Scanner kbd = new Scanner(System.in);
     
          System.out.print("Input the number of elements: ");
            int elements = kbd.nextInt();
          System.out.println("List A" + "     " + "List B" + "      " + "List C");  
     
            int listA[] = new int[elements];
            int listB[] = new int[elements];
     
                for(int i=0; i<elements; i++){
                    listA[i] = 10 + rand.nextInt(90);
                }    
     
                for(int j=0; j<elements; j++){
                    listB[j] = 10 + rand.nextInt(90);
                }
                        System.out.println(listA[i] + "         " + listB[j] + "           " + listA[i]*listB[j]);
            }
       }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,616
    Thanks
    53
    Thanked 2,529 Times in 2,480 Posts

    Default Re: HELP: My program is having an error which says "cannot find symbol", but the symbols are actually there, I think I placed it in the wrong place. Please tell me where I got it wrong.

    cannot find the symbol i and j
    The variables i and j are declared inside of the for loops and are not known outside of the loops.

    What do you want to print in that statement with the errors? If will be executed one time after both the loops have finished execution and the values of i and j will be past the end of the arrays.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP: My program is having an error which says "cannot find symbol", but the symbols are actually there, I think I placed it in the wrong place. Please tell me where I got it wrong.

    Should I declare i and j outside of the loop ?

    I wanted to print the values from the loops in the last statement with errors.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,616
    Thanks
    53
    Thanked 2,529 Times in 2,480 Posts

    Default Re: HELP: My program is having an error which says "cannot find symbol", but the symbols are actually there, I think I placed it in the wrong place. Please tell me where I got it wrong.

    wanted to print the values from the loops
    If you need the values from both arrays, either add the print statement to the second loop
    or create a new loop for printing the values from the two arrays.

    indexing into the two arrays can use the same index variable to access the corresponding values from the two arrays.

    The whole process could be done in one loop:
    begin loop
    get value for listA
    get value for listB
    print out contents of the lists and their product
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    winwin (February 21st, 2021)

  6. #5
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP: My program is having an error which says "cannot find symbol", but the symbols are actually there, I think I placed it in the wrong place. Please tell me where I got it wrong.

    hi can you give an example on how I can put the values of the two arrays in one loop?

    thank you so much, I'm so sorry for the inconvenience

    --- Update ---

    oh nevermind, I already got it, thank you so much for the help.

Similar Threads

  1. [SOLVED] "cannot find symbol-method" error
    By raf sevenants in forum Object Oriented Programming
    Replies: 4
    Last Post: August 25th, 2018, 08:54 AM
  2. "Error cannot find symbol" "throws BadLocationException"
    By coltson in forum AWT / Java Swing
    Replies: 1
    Last Post: June 30th, 2013, 10:33 PM
  3. "cannot find symbol" error when trying to use the getInt() method.
    By simpson_121919 in forum Collections and Generics
    Replies: 6
    Last Post: February 21st, 2013, 12:48 PM
  4. Replies: 3
    Last Post: January 22nd, 2013, 07:14 AM
  5. "Cannot find symbol" compilation error
    By collegejavastudent in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 12th, 2011, 05:07 PM