Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Problem using tables

    Hi! I have just started studying programming in my school and the first course is about java. I have a task to ask the user for a name and the grade the name relates to. Grade value can go from 0 to 5. The problem is that it prints out name values 'null' and all the grades are zero.

    Code:
    import java.util.*;
    public class Teht49 {
        public static void main(String[] args) {
        	int size = 0;
            Scanner reader = new Scanner(System.in);
            // INPUT
            for (int i=0;i<=size;i++) {
                size++;
                String names[] = new String[size];
                int grades[] = new int[size];
                System.out.print("Give a name (write 'end' to stop giving names): ");
                names[i] = reader.next();
                
                if (names[i].equals("end")) {
                    for (int j=0; j<names.length-1; j++) {
                        System.out.println("Name: "+names[j]+" - Grade: "+grades[j]);
                    }
                    System.exit(0);
                }
                else {
                    System.out.print("Give a course grade for "+names[i]+": ");
                    grades[i] = reader.nextInt();
                        while (grades[i] < 0 || grades[i] > 5) {
                            System.out.print("Give the course grade again (0..5): ");
                            grades[i] = reader.nextInt();
                        }
                        }
            }
    
        }
    }
    The teacher said something like the size value is overwritten or sth, but I didn't get it. Can u help me?
    Last edited by mastahc; 11-23-2010 at 04:19 PM.

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    I can't, but I can direct you towards the Java forum. Java and Javascript are very different, despite their confusingly-similar names.

  • Users who have thanked Spudhead for this post:

    mastahc (11-23-2010)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    OK. I have moved the Thread into the Java Forum
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    I see your problem.

    You are trying to dynamically change the size of your arrays by reintializing them at the top of your loop, like "String names[] = new String[size];"

    By doing so, you are clearing out your array, so it's in effect a new array. That is why you are getting null values. Nothing is in them any more after you redeclare the string size.

    Arrays in Java are static data structures. You've got to know how big they are going to be when you declare them. You are trying to use them dynamically, making them grow as your programs runs, which won't work.

    To make them dynamic, grow at runtime, you could use the Vector class, but it's better to use the newer ArrayList class, imo.

    Code:
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Integer> grades = new ArrayList<Integer>();
    Note, because the ArrayList class takes objects, not primitives, you'll have to use the Integer class instead of the "int" primitive datatype, which are not objects.

    Also be sure not to put your ArrayList declaration inside your loop, but declare it outside the loop so it won't get redeclared during every iteration of your loop and wiped out. Important.

    With ArrayList, you can dynamically add new indexes via the add() method. In your case, you are adding the value from your console to the names ArrayList, so you would do:

    Code:
    names.add(reader.next());
    To read the values, use the get() method. If inside a loop and you want to read the index i of names, do this:

    Code:
    names.get(i);
    To get the length of an ArrayList, you use the size() method.

    Code:
    names.size()
    As with Arrays, you can still use equals() method, so this will still work in your if conditional statement:
    Code:
    if (names.get(i).equals("end")) {
    That should give you enough info to redo your code using ArrayList.

    You can read more about ArrayList here.

    Be sure to post back to let us know how you are progressing.

  • Users who have thanked pigpen for this post:

    mastahc (11-24-2010)


  •  

    Posting Permissions

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