Random and Jtable

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 821 Views - Last Post: 04 October 2012 - 07:53 PM Rate Topic: -----

#1 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Random and Jtable

Posted 03 October 2012 - 07:24 PM

Hello. I just started learning java, so im a big noob. Made a program about student, that generates random name, ID, major.
Problems
1) I need the names, ID, major to generate 50 different times.
2) I need to place the generated data on a Jtable. With inverted colors for the columns.

Also reason why i have "html" at the end if because im trying to put this table on a website. Any help is appreciated. Thanks.

 public static void main(String[] args) {
        // TODO code application logic here
        
        
        
        
        
        
        
        String[] fnames = {"Mike","David","Jonathan","Peter","Paul","Nathan","Nicole","Eddie","Estelle","Emily","Joey","Aaron"};
        String[] lnames = {"Eddy","Muggli","Defourneaux"};
        //CODE which uses setup above to produce random name - use in loop
        
        String randomFName;
        randomFName = fnames[(int)(fnames.length*Math.random())];
        String randomLName = lnames[(int)(lnames.length*Math.random())];
        
        
        String randomName = randomFName + " " + randomLName;
        //code produces random Customer ID - use in loop
        String randomStudentID = ((int)(Math.random()*8999 + 1000)) + "";
        //code produces random Customer ID - use in loop
        
        String[] majors = {"Math","Actuarial Science","Computer Science","Information Technology"};
        
        String randomMajor = majors[(int)(Math.random()*majors.length)];
        
        
      
        
        
        System.out.println("<html>");
        System.out.println(randomName);
        //System.out.println(randomLName);
        System.out.println(randomStudentID);
        System.out.println(randomMajor);
       
        
        
        System.out.println("</html>");
    
    
    }  

}




Is This A Good Question/Topic? 0
  • +

Replies To: Random and Jtable

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 03 October 2012 - 07:40 PM

You are telling us you are trying to write a Java program that will generate HTML code ?

You are far from displaying a JTable. A JTable requires an Applet, you haven't an Applet here and a JApplet is invoked by .html code that download it
Was This Post Helpful? 0
  • +
  • -

#3 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 03 October 2012 - 07:45 PM

Right now im just trying to get random 50 names and put it on a table.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 03 October 2012 - 07:49 PM

Table or JTable ?
one is a universal computer language concept
the other is a Java specific GUI component but this what you mentionned in your first post
Was This Post Helpful? 0
  • +
  • -

#5 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 03 October 2012 - 07:56 PM

View PostTomas1122, on 03 October 2012 - 07:45 PM, said:

Right now im just trying to get random 50 names and put it on a table.


it doesn't have to be jtable. i just need to put 50 random name, id, major on a table.

View Postpbl, on 03 October 2012 - 07:49 PM, said:

Table or JTable ?
one is a universal computer language concept
the other is a Java specific GUI component but this what you mentionned in your first post


Yeah sorry lol. I just need to put it on any table doesn't need to be jtable.

So i just need any table that displays my random 50 outputs on the table.

1) table to display the 50 students names, id, major
2) The table should have a header row
3) Each student should form its own row with each element in its own cell
4) The table should have a border equal to 1, cellspacing set to 0, and cellpadding equal to 5
5) The rows should alternate colors to make the data easier to read
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 03 October 2012 - 07:57 PM

Here is a quick one that should combine the 2

public class Names extends JFrame {
	
	String[][] names;
	String[] header = {"Name", "ID"};
	
	Names() {
		super("Names");
		Random ran = new Random();
		names = new String[50][2];
		for(int i = 0; i < 50; ++i) {
			names[i][0] = "This is name" + (ran.nextInt(5000));
			names[i][1] = "" + ran.nextInt(1000);
		}
		JTable t = new JTable(names, header);
		add(new JScrollPane(t));
	}
	
	public static void main(String[] args) {
		JFrame f = new Names();
		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		f.setSize(300,200);
		f.setVisible(true);
	}

}


Was This Post Helpful? 0
  • +
  • -

#7 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 03 October 2012 - 08:13 PM

View Postpbl, on 03 October 2012 - 07:57 PM, said:

Here is a quick one that should combine the 2

public class Names extends JFrame {
	
	String[][] names;
	String[] header = {"Name", "ID"};
	
	Names() {
		super("Names");
		Random ran = new Random();
		names = new String[50][2];
		for(int i = 0; i < 50; ++i) {
			names[i][0] = "This is name" + (ran.nextInt(5000));
			names[i][1] = "" + ran.nextInt(1000);
		}
		JTable t = new JTable(names, header);
		add(new JScrollPane(t));
	}
	
	public static void main(String[] args) {
		JFrame f = new Names();
		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		f.setSize(300,200);
		f.setVisible(true);
	}

}



Thanks. Once i put this code with mine, Im getting error though. Sorry if this very common but im very new to java. Says: Caused by: java.lang.RuntimeException: Uncompilable source code - class Names is public, should be declared in a file named Names.java.
How would i fix this?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 03 October 2012 - 08:15 PM

You have to cut & paste this code in a file named: Names.java
Was This Post Helpful? 0
  • +
  • -

#9 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 03 October 2012 - 08:22 PM

View Postpbl, on 03 October 2012 - 08:15 PM, said:

You have to cut & paste this code in a file named: Names.java


Great. Worked. How would i put my random 50 generated names above in my code to this table?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 03 October 2012 - 08:28 PM

Sorry I tough you wanted completly random name
Simply randomly select from the 2 names arrays

		for(int i = 0; i < 50; ++i) {
			int first = ran.nextInt(fnames.length);
			int last = ran.nextInt(lnames.length);
			names[i][0] = fnames[first] + " " + lnames[last];
			names[i][1] = "" + ran.nextInt(1000);
		}


Was This Post Helpful? 0
  • +
  • -

#11 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 03 October 2012 - 08:37 PM

View Postpbl, on 03 October 2012 - 08:28 PM, said:

Sorry I tough you wanted completly random name
Simply randomly select from the 2 names arrays

		for(int i = 0; i < 50; ++i) {
			int first = ran.nextInt(fnames.length);
			int last = ran.nextInt(lnames.length);
			names[i][0] = fnames[first] + " " + lnames[last];
			names[i][1] = "" + ran.nextInt(1000);
		}



No its okay. Im sorry for not being clear. When i put this code in i just get bunch of errors now :-/. Do i need to put my previous code with randoms names with your code?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 03 October 2012 - 08:41 PM

Just add your arrays after my header
	String[] header = {"Name", "ID"};
    String[] fnames = {"Mike","David","Jonathan","Peter","Paul","Nathan","Nicole","Eddie","Estelle","Emily","Joey","Aaron"};  
    String[] lnames = {"Eddy","Muggli","Defourneaux"};  


you will have to understand a bit what you do :)
Was This Post Helpful? 0
  • +
  • -

#13 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 03 October 2012 - 08:52 PM

View Postpbl, on 03 October 2012 - 08:41 PM, said:

Just add your arrays after my header
	String[] header = {"Name", "ID"};
    String[] fnames = {"Mike","David","Jonathan","Peter","Paul","Nathan","Nicole","Eddie","Estelle","Emily","Joey","Aaron"};  
    String[] lnames = {"Eddy","Muggli","Defourneaux"};  


you will have to understand a bit what you do :)


Thank you. I got it working. Now i need to add majors. Would i need to do the same?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 03 October 2012 - 08:55 PM

You have a good start
Yopu will have to add one elemnt to the header array
to use [3] rather thean [2] as second dimention for the names array
Was This Post Helpful? 0
  • +
  • -

#15 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 03 October 2012 - 09:02 PM

View Postpbl, on 03 October 2012 - 08:55 PM, said:

You have a good start
Yopu will have to add one elemnt to the header array
to use [3] rather thean [2] as second dimention for the names array


great i got that working. but still having problem generating random majors.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2