Results 1 to 8 of 8
Thread: Help with Scanner Class
- 11-17-2010, 01:36 PM #1
Member
- Join Date
- Nov 2010
- Location
- New Cross, London, UK
- Posts
- 18
- Rep Power
- 0
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.
- 11-17-2010, 02:20 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Have you learned about the Scanner class at all yet? You need to import it first, create a Scanner object, then take your input:
I left out the details, including your int[] array, but hopefully this helps some.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
- 11-17-2010, 02:23 PM #3
Member
- Join Date
- Nov 2010
- Location
- New Cross, London, UK
- Posts
- 18
- Rep Power
- 0
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);
}
}
- 11-17-2010, 02:39 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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.
- 11-17-2010, 02:45 PM #5
Member
- Join Date
- Nov 2010
- Location
- New Cross, London, UK
- Posts
- 18
- Rep Power
- 0
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); } }
- 11-17-2010, 02:50 PM #6
Member
- Join Date
- Nov 2010
- Location
- New Cross, London, UK
- Posts
- 18
- Rep Power
- 0
Ok, I noticed my silly error. Thanks.
- 11-17-2010, 02:52 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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.
- 11-17-2010, 02:53 PM #8
Member
- Join Date
- Nov 2010
- Location
- New Cross, London, UK
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Using Scanner class in GUI
By Indegon in forum New To JavaReplies: 13Last Post: 11-12-2010, 10:50 PM -
Using the scanner class
By danielwestjr in forum New To JavaReplies: 1Last Post: 03-13-2009, 10:49 AM -
Scanner Class...
By TheRocket in forum New To JavaReplies: 5Last Post: 12-05-2008, 09:48 AM -
Scanner class
By ajaymenon.k in forum Advanced JavaReplies: 1Last Post: 11-26-2007, 07:01 AM -
JDK 5.0 Scanner Class
By Sircedric88 in forum New To JavaReplies: 3Last Post: 07-27-2007, 06:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks