Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
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.

Adbot
ADBOT LOVES YOU

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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply