Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,523 people online right now. Registration is fast and FREE... Join Now!




Displaying Java String Arrays

 
Reply to this topicStart new topic

Displaying Java String Arrays

Israel
20 Apr, 2007 - 10:09 PM
Post #1

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Man, this has been the worst week I've had in a long time. I really can't focus to do this project. But I figure if I can get a little push forward that might get my mind going. I'm supposed to take a class named 'student' and a constructor named student that initializes a string named 'name' and an int name 'ID' and use Math.random to produce a 4 digit number. Then use a printdata method to print the name with ID. Next building a class named 'first' that creates an array of 5 students. I have to ask the names with JOptionPane.showInputDialog and print the results with the printdata method.
CODE
import javax.swing.JOptionPane;

public class project3 {
    // Main Method
    public static void main(String[]args) {
  
    
    JOptionPane.showInputDialog("Enter the next Student's Name:");
    
    }
}
class student{

    String name;
    int ID = (int)(Math.random() * 1000);
    // Return the ID Number
   double getID(){
       return ID;
   }

}
class first{

    String name = new String();
      
}


Ok, I know this absolutely sucks, but I'm really having trouble focusing. I'm not asking for someone to do my homework for me, but a push in the right direction would help. I've tried writing for loops (so I don't have write the JOption.showInputDialog message 5 times, and I'm having trouble inputing the names into an Array.

This post has been edited by Israel: 20 Apr, 2007 - 10:10 PM
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Displaying Java String Arrays
21 Apr, 2007 - 01:08 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions

I would say your class needs to look more like this:
CODE
class student{

    private String name;
    private int ID;
    
    public student (String n) {
        name = n;
        ID = (int)(Math.random() * 1000);
        
    }


With name and ID as class variables, and the name being passed into the constructor method, and the ID being assigned when the constructor is called.

You should be able to assign students in a loop
CODE

for (int i = 0; i < 5; i++)
{
String name = JOptionPane.showInputDialog("Enter the next student's name");

/*....Then you need code to create a new student, and assign it to position i in your student array e.g.*/

StuArr[i] = new Student(name);
}


Hope that's some help smile.gif
User is offlineProfile CardPM
+Quote Post

Israel
RE: Displaying Java String Arrays
21 Apr, 2007 - 12:31 PM
Post #3

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Thanks! But I'm still having trouble the printdata method?

CODE
public static void main(String[]args) {

        for (int i = 0; i < 5; i++)
        {
        String name = JOptionPane.showInputDialog("Enter the next student's name");
  
        StuArr[i] = new Student(name);
        String.printdata();

        JOptionPane.showMessageDialog(null, "This student's Id is: "
         );
        
    }
}

User is offlineProfile CardPM
+Quote Post

Ellie
RE: Displaying Java String Arrays
21 Apr, 2007 - 02:21 PM
Post #4

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions

CODE
StuArr[i].printData();


To call the method, but I don't think you posted a printData method, so that might also cause problems
User is offlineProfile CardPM
+Quote Post

Israel
RE: Displaying Java String Arrays
21 Apr, 2007 - 08:25 PM
Post #5

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
I just did a project like this last week, but I feel like I'm getting worse instead of better. (Of course it might help if the printdata() method was actually in my book) But I'm really having trouble remembering how to link these arrays and methods right.
CODE
import javax.swing.JOptionPane;

public class project3 {
    // Main Method
    public static void main(String[]args) {

        for (int i = 0; i < 5; i++)
        {
        String name = JOptionPane.showInputDialog("Enter the next student's name");

        StuArr[i].printData();

        String out = "The student " + name + " has " + ID + " for an Id";  
        JOptionPane.showMessageDialog(null, out);
      

    }
}
class Student{

    public String name;
    public int ID;

    public Student (String n){
        name = n;
         int ID = (int)(Math.random() * 1000);
    }
}
class first{

    public static int[] printdata(int[] Strarr){
    
    String name = new String();
    int[] Strarr = new int[5];

  
    
     }

   }
}

User is offlineProfile CardPM
+Quote Post

Ellie
RE: Displaying Java String Arrays
21 Apr, 2007 - 11:18 PM
Post #6

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
Hey, just a quick reply as I'm going out this am, but here's a working version fixing the problems you had with the last piece of code you posted

CODE
import javax.swing.JOptionPane;

class Student{

    public String name;
    public int ID;

    public Student (String n){
        name = n;
         int ID = (int)(Math.random() * 1000);
    }


public String printData(){
    return ("Name: " + name + " Student ID: " + ID + "\n");
}
}

public class project3 {
    // Main Method
    public static void main(String[]args) {
    Student StuArr[] = new Student[5];
    String output = "";
        for (int i = 0; i < 5; i++)
        {
        String name = JOptionPane.showInputDialog("Enter the next student's name");
        StuArr[i] = new Student(name);
        output += StuArr[i].printData();
        
        //String out = "The student " + name + " has " + ID + " for an Id";  
        //JOptionPane.showMessageDialog(null, out);
      
    }
    JOptionPane.showMessageDialog(null, output);
    
}
}
/*class first{

    public static int[] printdata(int[] Strarr){
    
    String name = new String();
    int[] Strarr = new int[5];

  
    
     }

   }
} I don't think you need this class at all*/

User is offlineProfile CardPM
+Quote Post

Israel
RE: Displaying Java String Arrays
22 Apr, 2007 - 09:22 PM
Post #7

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Yeah, I didn't see the point of another class either but for some reason my professor did. Oh well, I'll figure that part out. I'm just curious, the code you last posted only seems to assign zero as the student ID. Math.random is throwing any errors, nor is anything else. There must be a logical error in this that I can't see...?

This post has been edited by Israel: 22 Apr, 2007 - 09:22 PM
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Displaying Java String Arrays
23 Apr, 2007 - 10:49 AM
Post #8

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions

Yeah, appologies, remove the 'int' from inside the constructor method before 'ID' and you should be ok. It was assigning the ID number to a different variable than the class variable.


User is offlineProfile CardPM
+Quote Post

Israel
RE: Displaying Java String Arrays
23 Apr, 2007 - 09:19 PM
Post #9

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
I must say Ellie, you've been kicking a** and taking names over here in the Java forum. If this keeps up I could see you becoming a </dic> expert in the future. But unfortunately that's not my decision to make. Thanks for the help though. After, my girlfriend left me this week and all the other 1000 things that happened that happened I've really needed some outside help with my studies. Thanks! icon_up.gif
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Displaying Java String Arrays
24 Apr, 2007 - 12:52 AM
Post #10

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions

Ah thanks, that's really nice of you biggrin.gif

I'm still learning myself, it's good to keep practicing so next semester when I have another Java class, I've not forgotten everything again rolleyes.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month