Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default Help with Scanner Class

    Hi all, I am new to the forum, and to Java; I am a first year CS student in London.

    Basically, I have to take a series of inputs from the user, and then transfer the information to an int[] Array. Thing is, it is specified that I must use the Scanner Class.

    Any help would be much appreciated.
    Thanks.

  2. #2
    lonegreyride is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Have you learned about the Scanner class at all yet? You need to import it first, create a Scanner object, then take your input:
    Java Code:
    import java.util.Scanner; //import
    
    Scanner keyboard = new Scanner(System.in);  //new scanner object
    
    System.out.print("Enter input");
    
    someVariable[0] = keyboard.nextInt(); //save input
    I left out the details, including your int[] array, but hopefully this helps some.

  3. #3
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default

    Thanks for the response, I have done something similar in another Method (I think), I will try that, but i'll include the Class Source Code to give you a better insight.

    import java.util.Scanner;

    public class MelodyPlayer
    {
    private Synthesizer synth;
    private int[] notes;

    public MelodyPlayer()
    {
    synth = new Synthesizer();
    notes = new int[4];
    notes[0] = 13;
    notes[1] = 23;
    notes[2] = 5;
    notes[3] = 88;
    }

    public void testSynth()
    {
    synth.useSawWaveSound();
    synth.playSound(23);
    }

    public void playNote(int index)
    {
    synth.playSound(notes[index]);
    }

    public void playSequence()
    {
    int wait;
    int runs;
    runs = 0;
    wait = 500;
    while(runs < 8)
    {
    playNote(2);
    synth.wait(wait);
    playNote(2);
    synth.wait(wait);
    playNote(0);
    synth.wait(wait);
    playNote(3);
    synth.wait(wait);
    playNote(1);
    synth.wait(wait);
    playNote(1);
    synth.wait(wait);
    playNote(2);
    synth.wait(wait);
    playNote(1);
    synth.wait(wait);
    runs++;
    }
    synth.stopSound();
    }

    public void playUserNote()
    {
    Scanner scan = new Scanner(System.in);
    System.out.println( "Enter a number between 1 and 127: " );
    int note = scan.nextInt();
    synth.playSound(note);
    }
    }

  4. #4
    lonegreyride is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Yup, that's exactly what you're doing at the bottom there. I'm a little surprised you're wondering about this problem when you've already done that one. I should also warn you that I'm new myself, so hopefully one of the more experienced members won't need to correct anything. This is pretty simple stuff though. I only started a few months ago, but I've used the Scanner class in this way probably 30 to 40 times already.

  5. #5
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default

    Thanks for the response, I have adapted the code, and i get the following error:
    ArrayIndexOutOfBoundsException:
    6

    Thanks, I am also very new.

    Java Code:
    import java.util.Scanner;
    
    public class MelodyPlayer
    {
        private Synthesizer synth;
        private int[] notes;
        
        public MelodyPlayer()
        {
            synth = new Synthesizer();
            notes = new int[4];
            notes[0] = 13;
            notes[1] = 23;
            notes[2] = 5;
            notes[3] = 88;
        }
        
        public void testSynth()
        {
            synth.useSawWaveSound();
            synth.playSound(23);
        }
        
        public void playNote(int index)
        {
            synth.playSound(notes[index]); //This line has the error.
        }
        
        public void playSequence()
        {
            int wait;
            int runs;
            runs = 0;
            wait = 500;
            while(runs < 8)
            {
                playNote(2);
                    synth.wait(wait);
                playNote(2);
                    synth.wait(wait);
                playNote(0);
                    synth.wait(wait);
                playNote(3);
                    synth.wait(wait);
                playNote(1);
                    synth.wait(wait);
                playNote(1);
                    synth.wait(wait);
                playNote(2);
                    synth.wait(wait);
                playNote(1);
                    synth.wait(wait);
                runs++;
            }
            synth.stopSound();
        }
        
        public void playUserNote()
        {
            Scanner scan = new Scanner(System.in);
            System.out.println( "Enter a number between 1 and 127: " );
            int note = scan.nextInt();
            synth.playSound(note);
        }
        
        public void populateArray()
        {
            Scanner sequenceScanner = new Scanner(System.in);
            int wait;
            wait = 500;
            
            System.out.println("Please enter the first note that wou wish to be played: ");
                notes[0] = sequenceScanner.nextInt();
            System.out.println("Please enter the second note that wou wish to be played: ");
                notes[1] = sequenceScanner.nextInt();
            System.out.println("Please enter the third note that wou wish to be played: ");
                notes[2] = sequenceScanner.nextInt();
            System.out.println("Please enter the fourth note that wou wish to be played: ");
                notes[3] = sequenceScanner.nextInt();
            
            playNote(notes[0]);
                synth.wait(wait);
            playNote(notes[1]);
                synth.wait(wait);
            playNote(notes[2]);
                synth.wait(wait);
            playNote(notes[3]);
                synth.wait(wait);
        }
    }

  6. #6
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default

    Ok, I noticed my silly error. Thanks.

  7. #7
    lonegreyride is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Well it looks like you're passing an int between 1 and 127 as the index of notes[], but notes only has 4 elements.

    Edit: beat me to it.

  8. #8
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default

    Yeah, I posted the second code, then noticed it, silly me.

    Thanks a lot.

Similar Threads

  1. Using Scanner class in GUI
    By Indegon in forum New To Java
    Replies: 13
    Last Post: 11-12-2010, 10:50 PM
  2. Using the scanner class
    By danielwestjr in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 10:49 AM
  3. Scanner Class...
    By TheRocket in forum New To Java
    Replies: 5
    Last Post: 12-05-2008, 09:48 AM
  4. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 07:01 AM
  5. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 06:55 PM

Tags for this Thread

Posting Permissions

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