- Anphear
- Jan 20, 2008
-
|
So I have a minor test tomorrow that require me to write a memorised piece of code in 25 minutes, which I'm finishing now. How ever I cannot for the life of me remember how to get my dam scanner class working. NB programming is not my strong point, which leads me to missing out the most simple things...
Code;
code:package lab08;
import java.util.Scanner;
public class TestApp{
public static void main (String[] args){
NumberList n = new NumberList(10);
Scanner s = new Scanner(System.in);
n.add(s.nextInt);
if(!n.isEmpty()){
System.out.println("The average is " + n.calcAverage());
}
System.out.println(n.toString());
}
public static int readInt(String message){
Scanner sc = new Scanner(System.in);
return sc.nextInt();
}
}
The problem that I'm currently having is the scanner class isn't 'working right' and that my array remains empty. With the toString method simply printing [] and if I remove the isEmpty() method I get a NaN return.
code:package lab08;
public class NumberList{
private int[] nList;
private int count = 0;
public NumberList(int maxsize){
nList=new int[maxsize];
}
public void add(int item){
if(count < nList.length){
nList[count]=item;
count++;
}
}
[i]rest of class[/i]
Heres the add array method.
|
#
¿
Mar 27, 2008 09:35
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
Apr 29, 2024 05:27
|
|
- Anphear
- Jan 20, 2008
-
|
triplekungfu posted:
code:while (s.hasNextInt()) {
n.add(s.nextInt());
}
What's this method for?
Edit: I'm going to bed, but I wrote this while looking at Scanner; it might help, or it may just confuse matters further.
Thanks Triple, With the while loop and a pastbin code I got mine working. Also when I visually compared, my getAv and add methods they were pretty much the same.
|
#
¿
Mar 27, 2008 20:45
|
|