Results 1 to 11 of 11
- 02-14-2011, 01:57 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
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
- 02-14-2011, 02:05 AM #2
Wrong!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
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?
- 02-14-2011, 02:07 AM #3
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.
- 02-14-2011, 02:08 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
You can get all the input from the user through this single scanner.Java Code:scanner scan = new Scanner(System.in);
This can be done with the same scanner.Java Code:prompt for something store variable as user input prompt again store another
Last edited by sunde887; 02-14-2011 at 02:10 AM.
- 02-14-2011, 02:10 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
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.
- 02-14-2011, 02:12 AM #6
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.Java Code:public void theWholeClass(){ Scanner scname=new Scanner(System.in); String strname=scname.nextLine();
- 02-14-2011, 02:13 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
- 02-14-2011, 02:17 AM #8
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.
- 02-14-2011, 02:39 AM #9
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
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.
- 02-14-2011, 02:53 AM #10
:headdesk:
- 02-14-2011, 09:39 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Use a scanner, its simple enough and worth learning. Do what we said, create a SINGLE scanner object. Here is an example:
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.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);
Try this out, post some code and if you are still way off, I will give you a piece to the solution.
Similar Threads
-
compiles but dosent work...
By darkflame5019 in forum New To JavaReplies: 8Last Post: 01-01-2011, 07:22 PM -
Program compiles but wont run to text file...
By marylanddem in forum New To JavaReplies: 2Last Post: 12-05-2010, 04:05 PM -
This program compiles but doesnt run properly!
By ErikD99 in forum New To JavaReplies: 5Last Post: 12-03-2010, 08:44 PM -
Simple program compiles but main class is not found-- not sure why
By damong in forum New To JavaReplies: 4Last Post: 01-01-2009, 03:58 AM -
Program Compiles but Buttons do not display
By ljk8950 in forum AWT / SwingReplies: 8Last Post: 08-11-2008, 03:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks