Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-23-2010, 12:08 PM   PM User | #1
mastahc
New to the CF scene

 
Join Date: Nov 2010
Posts: 1
Thanks: 2
Thanked 0 Times in 0 Posts
mastahc is an unknown quantity at this point
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..
mastahc is offline   Reply With Quote
Old 11-23-2010, 01:16 PM   PM User | #2
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
I can't, but I can direct you towards the Java forum. Java and Javascript are very different, despite their confusingly-similar names.
Spudhead is offline   Reply With Quote
Users who have thanked Spudhead for this post:
mastahc (11-23-2010)
Old 11-23-2010, 02:37 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
OK. I have moved the Thread into the Java Forum
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 11-23-2010, 10:59 PM   PM User | #4
pigpen
Regular Coder

 
Join Date: Dec 2007
Posts: 137
Thanks: 1
Thanked 21 Times in 21 Posts
pigpen is on a distinguished road
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.
pigpen is offline   Reply With Quote
Users who have thanked pigpen for this post:
mastahc (11-24-2010)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:28 AM.


Advertisement
Log in to turn off these ads.