Results 1 to 11 of 11
  1. #1
    Johnny68 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default Input stream error

    I am getting an error when compiling. My focus is to generate 2 random numbers.. ex: (2*2) and have the user guess the answer. If the answer is correct, it will say correct and close out. If they are incorrect, It should say try again.. Right now I am stuck at the code below. Here is my error:

    java:27: cannot find symbol
    symbol : method DataInputStream(java.io.InputStream)
    location: class MathProblemJavaClass
    DataInputStream stdin = DataInputStream (System.in);

    Java Code:
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import java.util.Scanner;
    import java.util.Random;
    import javax.swing.SwingConstants;
    
    public class MathProblemJavaClass implements ActionListener
    {
    
    int number1;
    int number2;
    int answer;
    
    DataInputStream stdin = DataInputStream (System.in);
    Random generator = new Random();
    
    //Get Numbers
    number1 = generator.nextInt(15);
    number2 = generator.nextInt(15);
    
    //Label,Textfield,Jbutton,JFrame creation
    JLabel label=new JLabel("What is "+ number1 + "*" + number2,SwingConstants.CENTER);
    
    JTextField guessTF=new JTextField();
    
    JButton button=new JButton("Click Here");
    
    JFrame frame=new JFrame("Math Problems 101");
    
    public MathProblemJavaClass()
    {
    
    //Layout
    frame.setLayout(new GridLayout(3,1));
    frame.add(label);
    frame.add(guessTF);
    frame.add(button);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,100);
    frame.setVisible(true);
    
    
    //Add action listener
    button.addActionListener(this);
    
    }
    
    
    
    	public void actionPerformed(ActionEvent event)
    	{
    		if(event.getSource()==button)
    		{
    		JOptionPane.showMessageDialog(frame,"Your correct the answer is: "+guessTF.getText());
    	
    		}
    }
    
    
    public static void main(String[] args)throws IOException
    {
    
    	MathProblemJavaClass math=new MathProblemJavaClass();
    	}
    }
    Last edited by Fubarable; 08-05-2010 at 04:46 PM. Reason: moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Johnny68 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    ok Got that resolved, tnx... Now I get this from the random numbers:

    <identifier> expected
    number1 = generator.nextInt(10);
    ^
    MathProblemJavaClass.java:33: <identifier> expected
    number2 = generator.nextInt(10);
    ^
    MathProblemJavaClass.java:35: <identifier> expected
    str = stdin.readLine();
    ^
    3 errors

    public class MathProblemJavaClass implements ActionListener
    {

    int number1;
    int number2;
    int answer;


    DataInputStream stdin = new DataInputStream (System.in);
    Random generator = new Random(15);

    //Get Numbers
    number1 = generator.nextInt(10);
    number2 = generator.nextInt(10);

    //str = stdin.readLine();

    //Label,Textfield,Jbutton,JFrame creation
    JLabel label=new JLabel("What is "+ number1 + "*" + number2,SwingConstants.CENTER);

    JTextField guessTF=new JTextField();

    JButton button=new JButton("Click Here");

    JFrame frame=new JFrame("Math Problems 101");

    public MathProblemJavaClass()
    {

    //Layout
    frame.setLayout(new GridLayout(3,1));
    frame.add(label);
    frame.add(guessTF);
    frame.add(button);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(200,100);
    frame.setVisible(true);


    //Add action listener
    button.addActionListener(this);

    }



    public void actionPerformed(ActionEvent event)
    {
    if(event.getSource()==button)
    {
    JOptionPane.showMessageDialog(frame,"Your correct the answer is: "+guessTF.getText());

    }
    }

  4. #4
    Johnny68 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    I compile everytime I add new code. And when I see an error I fix it. So how do I declare it if its a random number?

    number1=();

    like this?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're trying to make method calls outside of a method or constructor. What method or constructor is this code in?:
    Java Code:
    //Get Numbers
    number1 = generator.nextInt(10);
    number2 = generator.nextInt(10);
    
    //str = stdin.readLine();
    
    //Label,Textfield,Jbutton,JFrame creation
    JLabel label=new JLabel("What is "+ number1 + "*" + number2,SwingConstants.CENTER);
    
    JTextField guessTF=new JTextField();
    
    JButton button=new JButton("Click Here");
    
    JFrame frame=new JFrame("Math Problems 101");
    More importantly, you may wish to change your style of coding. Rather than type in a bunch of code and then wade through many errors, why not compile after adding a small amount of code and compile frequently. Then you can fix any compile errors before adding any new code. It will save you a world of grief.

    Also, please look at my signature link regarding use of code tags.

    Luck

  6. #6
    Johnny68 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    Please explain? Am I just missing the } after:

    public class MathProblemJavaClass implements ActionListener
    {

    int number1;
    int number2;
    int answer;


    DataInputStream stdin = new DataInputStream (System.in);
    Random generator = new Random();

    //Get Numbers
    number1 = generator.nextInt(10);
    number2 = generator.nextInt(10);


    //Label,Textfield,Jbutton,JFrame creation
    JLabel label=new JLabel("What is "+ number1 + "*" + number2,SwingConstants.CENTER);


    JTextField guessTF=new JTextField();

    JButton button=new JButton("Click Here");

    JFrame frame=new JFrame("Math Problems 101");
    }

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,013
    Rep Power
    20

    Default

    You can declare attributes outside of a method (I'll ignore static blocks for now), but you can't do anything with them.

    So these:
    Java Code:
    int number1;
    int number2;
    int answer;
    
    DataInputStream stdin = new DataInputStream (System.in);
    Random generator = new Random();
    are OK.

    But these:
    Java Code:
    number1 = generator.nextInt(10);
    number2 = generator.nextInt(10);
    are not, since you are not declaring the number attributes (they're already declared) and you're using the generator attribute.

    These should be in the constructor, or in a method that is then called when you need to assign random numbers to them.

  8. #8
    Johnny68 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    I have tried to put that code elsewhere also. I have been working on this for over a week, and have changed the coding at least 5 times. This is the best I have, and Im still lost. I tried adding it under:

    public MathProblemJavaClass()
    {

    but I still get the same error. What am I not seeing?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Johnny68 View Post
    I have tried to put that code elsewhere also. I have been working on this for over a week, and have changed the coding at least 5 times. This is the best I have, and Im still lost. I tried adding it under:

    public MathProblemJavaClass()
    {

    but I still get the same error. What am I not seeing?
    I don't know. If you have made changes to your code, and are getting new errors, the only way we can help you is if you post the new code and the new errors. Again, please use code tags so that your code is readable, again, please see the link in my signature to see how to do this.

  10. #10
    Johnny68 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    Ok I moved them, and now get this error:

    java:43: class, interface, or enum expected
    public MathProblemJavaClass()
    ^
    MathProblemJavaClass.java:50: class, interface, or enum expected
    number2 = generator.nextInt(10);
    ^
    MathProblemJavaClass.java:52: class, interface, or enum expected
    answer = (number1 * number2);
    ^
    MathProblemJavaClass.java:55: class, interface, or enum expected
    frame.setLayout(new GridLayout(3,1));
    ^
    MathProblemJavaClass.java:56: class, interface, or enum expected
    frame.add(label);
    ^
    MathProblemJavaClass.java:57: class, interface, or enum expected
    frame.add(guessTF);
    ^
    MathProblemJavaClass.java:58: class, interface, or enum expected
    frame.add(button);
    ^
    MathProblemJavaClass.java:59: class, interface, or enum expected
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    ^
    MathProblemJavaClass.java:60: class, interface, or enum expected
    frame.setSize(200,100);
    ^
    MathProblemJavaClass.java:61: class, interface, or enum expected
    frame.setVisible(true);
    ^
    MathProblemJavaClass.java:65: class, interface, or enum expected
    button.addActionListener(this);
    ^
    MathProblemJavaClass.java:67: class, interface, or enum expected
    }
    ^
    MathProblemJavaClass.java:71: class, interface, or enum expected
    public void actionPerformed(ActionEvent event)
    ^
    MathProblemJavaClass.java:77: class, interface, or enum expected
    }
    ^
    MathProblemJavaClass.java:81: class, interface, or enum expected
    public static void main(String[] args)throws IOException
    ^
    MathProblemJavaClass.java:85: class, interface, or enum expected
    }
    ^
    16 errors

    Java Code:
    public class MathProblemJavaClass implements ActionListener
    {
    
    int number1;
    int number2;
    int answer;
    
    
    DataInputStream stdin = new DataInputStream (System.in);
    Random generator = new Random();
    
    //Label,Textfield,Jbutton,JFrame creation
    JLabel label=new JLabel("What is "+ number1 + "*" + number2,SwingConstants.CENTER);
    
    
    JTextField guessTF=new JTextField();
    
    JButton button=new JButton("Click Here");
    
    JFrame frame=new JFrame("Math Problems 101");
    }
    
    
    public MathProblemJavaClass()
    {
    
    //Get Numbers
    number1 = generator.nextInt(10);
    number2 = generator.nextInt(10);
    
    answer = (number1 * number2);
    
    //Layout
    frame.setLayout(new GridLayout(3,1));
    frame.add(label);
    frame.add(guessTF);
    frame.add(button);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,100);
    frame.setVisible(true);
    
    
    //Add action listener
    button.addActionListener(this);
    
    }
    
    
    
    	public void actionPerformed(ActionEvent event)
    	{
    		if(event.getSource()==button)
    		{
    		JOptionPane.showMessageDialog(frame,"Your correct the answer is: "+guessTF.getText());
    	
    		}
    }
    
    
    public static void main(String[] args)throws IOException
    {
    
    	MathProblemJavaClass math=new MathProblemJavaClass();
    	}
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. what is "standard error stream"
    By Cylab in forum New To Java
    Replies: 11
    Last Post: 07-15-2010, 08:29 AM
  2. Stream closed on a ClassLoader input
    By RaistlinMajeren in forum Advanced Java
    Replies: 15
    Last Post: 06-03-2010, 07:18 AM
  3. standard input stream storing to a generic method?
    By vendetta in forum New To Java
    Replies: 3
    Last Post: 01-29-2010, 08:13 PM
  4. Error Message when reading an input file.
    By Deluyxe in forum New To Java
    Replies: 8
    Last Post: 04-26-2009, 04:02 PM
  5. Replies: 2
    Last Post: 08-07-2007, 03:50 PM

Posting Permissions

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