Results 1 to 11 of 11
  1. #1
    NixasMuraki is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default Method won't work even though program compiles

    So I know I posted a very idiotic question about arrays before, and I feel really stupid for it, but now I have this program and it compiles, but after the user types in the number of students nothing else comes up and it's trying to compile it, but won't run theWholeClass method AT ALL. I'm very confused by this and would appreciate help! (I also created a different version where the method is in the parent class and its called in the runner class and it yields the same problem.)

    EDIT: I suppose it would be good to say that if I remove the code that has the user enter in an int that is the number of students, and I set the number of indices to a random number, the program works. The problem with this is realistically the number of students in a class varies. (Please keep in mind that I'm not planning on using this as anything official, I just need practice with arrays and coding in general)


    int intstudents;
    String[]names=new String[intstudents]; //this way the teacher will have enough elements for the number of students
    int[]age=new int[intstudents]; //these are parallel arrays
    //this is used instead of {10,14,27} etc, because there should be the exact number of array elements as there are students.


    public static void main(String[]args){

    System.out.println("Please type in the number of students in your class.");
    Scanner students=new Scanner(System.in);
    int intstudents=students.nextInt()-1; //subtract one because arrays start at 0


    String[]names=new String[intstudents]; //this way the teacher will have enough elements for the number of students
    int[]age=new int[intstudents]; //these are parallel arrays

    new Roster().theWholeClass();
    }

    public void theWholeClass(){

    Scanner scname=new Scanner(System.in);
    String strname=scname.nextLine();
    Scanner scage=new Scanner(System.in);
    int intage=scage.nextInt();
    for(int i=0; i<names.length;i++){
    System.out.println("Type in student's name");
    names[i]=strname;
    System.out.println("Type in student's age in integer format");
    age[i]=intage;
    }

    printData();

    }

    public void printData(){
    System.out.print("Here is your roster.");

    for(int i=0;i<names.length; i++){ //again the output of the array has to be less than its length
    System.out.println("Name "+names[i]+" Age " +age[i]+"\n"); //this ensures that the elements of the array printed on the roster are the names and ages of the student. It does one set of arrays at a time so that names and ages are not mixed up
    }
    }
    }
    Last edited by NixasMuraki; 02-14-2011 at 02:15 AM. Reason: More useful information that will help in solving problem

  2. #2
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Java Code:
    int intstudents=students.nextInt()-1; //subtract one because arrays start at 0
    String[]names=new String[intstudents]; //this way the teacher will have enough elements for the number of students
    Wrong!

    If you want an array that can hold 10 things then you make the array with a length of 10. The indicies will be 0 - 9 but it still can hold 10 things. If you subtract 1 as you do then the array will only be able to hold 9 things at indicies 0 - 8.

    More information about what your program does is needed. Does it just site there doing nothing? Then it is probably waiting for the user to input something. Or does it complete and go back to the command prompt?

  3. #3
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    One thing I noticed is that you have created 3 Scanner objects. Do not do that. Create one Scanner and use it everytime you want to get input.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    This code is challenging to read because it's written a bit sloppy(which is fine), and because it doesn't have code tags. Don't be afraid to ask stupid questions, just make sure you give it a try, sometimes the answer is right in front of you and you just can't see it. Your code seems oddly composed.
    ]
    It may be out of your experience right now, however a Map would be really nice for storing the students, or even creating another class called student.

    Reading this code I may be wrong on some parts its a bit challenging. I see you using scanner a few times for what seems like the same thing, why?

    Your code is overly confusing, you are declaring variables multiple times, if at the top you put int someItem you can store a value later with someItem = 5, or something similar, you don't need int someItem = 5.

    Next Step Id like to really address, create one single scanner
    Java Code:
    scanner scan = new Scanner(System.in);
    You can get all the input from the user through this single scanner.
    Java Code:
    prompt for something
    store variable as user input
    prompt again
    store another
    This can be done with the same scanner.
    Last edited by sunde887; 02-14-2011 at 02:10 AM.

  5. #5
    NixasMuraki is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Yes it does just sit after entering in the amount of students there are. You can just hit enter as much as you want and it just goes down but no new text shows up telling the user what to do as it should. It does work if the code that asks for the number of students isn't there and if I just go ahead and set the array indices to a random number then it works just fine, but realistically the number of students varies so I can't do that.

  6. #6
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Java Code:
    public void theWholeClass(){
    
    Scanner scname=new Scanner(System.in);
    String strname=scname.nextLine();
    Look at your code. It just tries and scans the next line without any prompts. If you want some nice information to be shown to the user then perhaps you should add code to do that.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ill give you pseudo code
    Java Code:
    declare int size, string array and int array
    create scanner
    prompt for amount of students
    loop
      prompt for name, store it in name array using the current loop number
      prompt for age, store the same way
    loop
      print each item

  8. #8
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    By the way the nextInt method does not consume a CR/LF after the integer input. this can lead to strange behaviour. To solve this problem simply add a call to nextLine immediately afterwards and do nothing with the returned value.

  9. #9
    NixasMuraki is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Thanks everyone for advice. Instead of scanners I just used JOptionpanes instead (like I had in the beginning) which makes everything easier, but still has the same problem.

  10. #10
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    :headdesk:

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Use a scanner, its simple enough and worth learning. Do what we said, create a SINGLE scanner object. Here is an example:

    Java Code:
    Scanner scan = new Scanner(System.in);
    int age;
    int id;
    String name;
    String major;
    
    System.out.println("Please enter your name:");
    name = scan.nextLine();
    System.out.println("Please enter your major:");
    major = scan.nextLine();
    System.out.println("Please enter your id:");
    id = scan.nextInt();
    System.out.println("Please enter your age:");
    age = scan.nextInt();
    
    System.out.println("Hello " + name ", id number: " + id + " age: " + age + " Major: " + major);
    this example will prompt for all the information and store the info in the correct variables, using a single scanner. Start from there, make sure you understand this before you move on.

    Try this out, post some code and if you are still way off, I will give you a piece to the solution.

Similar Threads

  1. compiles but dosent work...
    By darkflame5019 in forum New To Java
    Replies: 8
    Last Post: 01-01-2011, 07:22 PM
  2. Program compiles but wont run to text file...
    By marylanddem in forum New To Java
    Replies: 2
    Last Post: 12-05-2010, 04:05 PM
  3. This program compiles but doesnt run properly!
    By ErikD99 in forum New To Java
    Replies: 5
    Last Post: 12-03-2010, 08:44 PM
  4. Replies: 4
    Last Post: 01-01-2009, 03:58 AM
  5. Program Compiles but Buttons do not display
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-11-2008, 03:41 AM

Posting Permissions

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