Results 1 to 3 of 3
  1. #1
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Whats wrong with the program?

    // Question 1: is there any way i can shorten the main and put it in sub? //Question 2: although the output is what i want, it feels like im doing or explaining something wrong.
    public class Example2 {
    public Example2(String aname){
    name=aname;
    count=0;
    count++;
    //constructor}
    public static void entername(){
    System.out.println("Please Enter Your Name");

    }

    // This is a static method that will run right when the program is rans.
    public void message(){
    System.out.println("Your name is "+name);
    System.out.println("Total registered:"+count);}



    // This is a instance method that uses instance field”name,count”.

    private String name;
    private int count;
    }


    Main:
    import java.util.Scanner;
    import java.util.Random;
    public class Example2Tester {

    public static void main(String[] args) {
    Example2.entername(); //Class.method
    Scanner input = new Scanner(System.in); /*creates an object for Scanner to run on*/
    String i = input.next(); //stores the name you typed in i
    Example2 name=new Example2(i);//creates object for name to be stored
    Random r= new Random(); //creates object for random to run on
    int d=r.nextInt(9999); /*sets the limit from 0to9999 intergers,
    assign the random number as your ID*/
    System.out.println("Your ID is : "+d);
    name.message(); //object.method
    }

    }






    Output:
    Please Enter Your Name
    asdf
    Your ID is : 8677
    Your name is asdf
    Total registered:1



    //Thanks alot

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Whats wrong with the program?

    Quote Originally Posted by fishy8158 View Post
    Whats wrong with the program?
    How about you first tell us -- what is wrong with it? Or are we supposed to guess?

  3. #3
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Whats wrong with the program?

    well, did i comment anything wrong on the main? if not, then how can i put some more of the code from main to sub? thanks for replying.

Similar Threads

  1. i can't figure out whats wrong
    By redbrawn06 in forum New To Java
    Replies: 4
    Last Post: 10-17-2011, 10:44 PM
  2. Whats wrong with my program?
    By jwb4291 in forum New To Java
    Replies: 7
    Last Post: 12-12-2010, 05:40 AM
  3. whats wrong
    By atenv in forum New To Java
    Replies: 6
    Last Post: 06-15-2010, 02:55 PM
  4. whats is wrong with this app??
    By mrajan in forum New To Java
    Replies: 4
    Last Post: 06-09-2010, 11:56 PM
  5. Whats wrong with this code???
    By javanew in forum New To Java
    Replies: 4
    Last Post: 03-28-2010, 06:46 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
  •