Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Array index problem

  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Array index problem

    Hello. My apologies in advance for the dumb question. I want to write a simple program to print out the days of the month of January like so: "Mon. Jan. 1; Teus, Jan. 2; Weds, Jan 3"; etc.

    I get a number of errors when I try to compile this code. The errors reference the array index I've set up.
    Java Code:
    class DaysCalc4 {
        public static void main(String[] args) {
            //  calendar counting program
            
            int dayNum = 0;
            String monthName = "Jan";
            int monthLength = 31;       
            String[] dayName = { "Sun", "Mon", "Tues", "Weds", "Thurs", "Fri", "Sat" };
                                    
            for(int i = 0; i < monthLength; i++) {
                String dayOut = dayName[0];   
                dayNum += 1;
                System.out.println(dayOut + "., Jan. " + dayNum + ", 2012");  
                dayName[j]++;
                if (dayName[j] == 7) {
                    dayName[j] = 0;
                }               
                
            }
        }
    }
    And the error messages:
    6 errors found:
    File: C:\Users\jmp\Desktop\Java 6\DaysCalc4.java [line: 14]
    Error: cannot find symbol
    symbol: variable j
    location: class DaysCalc4
    File: C:\Users\jmp\Desktop\Java 6\DaysCalc4.java [line: 14]
    Error: bad operand type java.lang.String for unary operator '++'
    File: C:\Users\jmp\Desktop\Java 6\DaysCalc4.java [line: 15]
    Error: cannot find symbol
    symbol: variable j
    location: class DaysCalc4
    File: C:\Users\jmp\Desktop\Java 6\DaysCalc4.java [line: 15]
    Error: incomparable types: java.lang.String and int
    File: C:\Users\jmp\Desktop\Java 6\DaysCalc4.java [line: 16]
    Error: cannot find symbol
    symbol: variable j
    location: class DaysCalc4
    File: C:\Users\jmp\Desktop\Java 6\DaysCalc4.java [line: 16]
    Error: incompatible types
    required: java.lang.String
    found: int

    In my ignorance, I thought java would understand that dayName[j] is the index of the array dayNames, and that index value should increment with each pass of the loop until the index value becomes 7, and then reset to 0.

    How do I get the loop to step through the elements of the dayNames array? Thanks for your help. hmb

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: Array index problem

    You use i as a variable in your loop, but you then try to use j inside the loop body. What is j? Where is it declared and initialized?

    Also, your array contains Strings, but you're comparing them to ints. That doesn't make sense.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Array index problem

    Hello. Thank you for your reply.

    The array dayName has an index, yes? I would like to increment the value of that index each time thru the for loop, and when the index value reaches 7, reset the index value to 0.

    Can you tell me how to do that? Thanks. hmb

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: Array index problem

    You are incrementing a variable in your for loop. But then you're using a different variable (which you have not declared) to access the array.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Array index problem

    Hello. Thanks for your help. I appreciate it.

    Each time thru the for loop the dayNum variable increases by one, up to its limit of 31. With each pass of the for loop the dayOut variable gets assigned a value from the dayNam array, starting with dayNam index 0 (i.e. the string "Sun"). With each subsequent pass thru the for loop, I hope to get the next element of the dayNam array assigned to the dayOut variable. I thought I can increment the dayNam array index with each pass of the for loop, but of course, it's not working out.

    Can you tell me how to sequentially step through the elements of the dayNam array with each pass of the for loop?

    Thanks. hmb

    PS. If I declare a j variable, I get the following errors

    class DaysCalc4 {
    public static void main(String[] args) {
    // calendar counting program

    int dayNum = 0;
    String monthName = "Jan";
    int monthLength = 31;
    String[] dayName = { "Sun", "Mon", "Tues", "Weds", "Thurs", "Fri", "Sat" };
    int j;

    for(int i = 0; i < monthLength; i++) {
    String dayOut = dayName[0];
    dayNum += 1;
    System.out.println(dayOut + "., Jan. " + dayNum + ", 2012");
    dayName[j]++;
    if (dayName[j] == 7) {
    dayName[j] = 0;
    }

    }
    }
    }

    3 errors found:
    File: C:\Users\jmp\Desktop\Java 6\DaysCalc4.java [line: 15]
    Error: bad operand type java.lang.String for unary operator '++'
    File: C:\Users\jmp\Desktop\Java 6\DaysCalc4.java [line: 16]
    Error: incomparable types: java.lang.String and int
    File: C:\Users\jmp\Desktop\Java 6\DaysCalc4.java [line: 17]
    Error: incompatible types
    required: java.lang.String
    found: int

    Obviously, I am not accessing the dayNam array index correctly.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: Array index problem

    Right, you're still treating those Strings like ints, which is a separate problem.

    However, now you have three variables that seem to be overlapping in functionality. What is the difference between i, j, and dayNum?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Array index problem

    Hello. sorry to be so stupid about this.

    Yes, dayNam is an array of Strings. But each element of that array is defined by an index which has a numeric value. And I would like to access the next String element with each pass thru the for loop. So I thought I could increment the dayNam index value with "dayNam[j]++" which doesn't work (as an aside, this doesn't work either "dayNam [j++]" - the compiler tells me this "is not a statement".)

    Regardless of whether I'm using an array of Strings, or ints, or whatever, is there a way to tell the program to acces the next element in an array?

    Thanks again. hmb

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: Array index problem

    I recommend you read through the tutorial on arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    roozeech is offline Member
    Join Date
    Mar 2012
    Location
    Belarus, Minsk
    Posts
    5
    Rep Power
    0

    Default Re: Array index problem

    Humphrey, why you doesn't use enums for dayName? Your apps will be smaller with enum.
    But try this ugly variant :)
    Java Code:
    		String[] weekDays = { "Sun", "Mon", "Tue","Wed", "Thu", "Fri", "Sat" };
    		int i=0;
    		int a;
    		while(i!=30){
    		a=i%weekDays.length;
    		System.out.println(weekDays[a]+". "+(i+1)+", 2012");
    		i++;
    I guess, it works, but sorry me for any mistakes...I wrote this without any editor.
    insert it in main.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: Array index problem

    roozeech, spoon-feeding is not helping. Uncompilable spoon-feeding that doesn't even solve the original problem is actually hurting. Please don't post full code solutions- this is not only unfair and against most academic dishonesty policies, but it actually makes it harder to actually help people. A big part of programming is problem solving, and giving out full solutions eliminates the process of learning how to solve problems, which is actually harder to teach than the syntax part. And when the code you post doesn't even solve the problem, that just leads to more confusion.

    Please don't spoon-feed. Consider this a warning.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    roozeech is offline Member
    Join Date
    Mar 2012
    Location
    Belarus, Minsk
    Posts
    5
    Rep Power
    0

    Default Re: Array index problem

    Quote Originally Posted by KevinWorkman View Post

    Please don't spoon-feed. Consider this a warning.
    Okay, sorry!

Similar Threads

  1. how to read array index
    By aconti in forum New To Java
    Replies: 5
    Last Post: 08-12-2011, 09:52 PM
  2. having an array index out of bounds problem
    By samanyu in forum New To Java
    Replies: 18
    Last Post: 06-03-2011, 08:12 AM
  3. Replies: 4
    Last Post: 01-31-2011, 08:20 PM
  4. get index from an array (very frustrating)
    By Yakg in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 06:26 PM
  5. Array Index Out Of Bounds and Problem in Assigning Values
    By chronoz1300 in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •