Results 1 to 18 of 18
  1. #1
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default load external classes

    I have recently made a post about the JRE, but I changed my mind on keeping the program mentioned in that thread as text-based only. The program is supposed to calculate the value each of the three variables in the pythagorean theorem. The program is comprised of four java files. I have got the other three to compile, since the first java file requires them. However, I'm having troubles compiling the first. According to the terminal, it is the last half of this file that is giving troubles. The message says the following

    Rightri.java:68: ';' expected
    ChoiceA Firstchoice();
    ^
    Rightri.java:72: ';' expected
    ChoiceB Secondchoice();
    ^
    Rightri.java:76: ';' expected
    ChoiceC Thirdchoice();
    ^
    Rightri.java:92: reached end of file while parsing
    }
    ^
    4 errors
    The part it refers to is this

    Java Code:
    private class selectButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			// declare variables
    			String str;
    			
    			// get input
    			str = choice.getText();
    
                            // set conditional statements
    			boolean select1 = (str == 'a');
    			boolean select2 = (str == 'b');
    			boolean select3 = (str == 'c');
    			if (select1)
    			{
    				ChoiceA Firstchoice();
    			}
    			else if (select2)
    			{
    				ChoiceB Secondchoice();
    			}
    			else if (select3)
    			{
    				ChoiceC Thirdchoice();
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null, str+ " is not a valid variable.", "Invalid input", JOptionPane.ERROR_MESSAGE);
    		}
    	}
    }
    each of three classes bring up a Swing window that asks a certain set of questions. What do I need to do to correct this? I might try to guess and check, but I'll still like help.

  2. #2
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Well, what are ChoiceA and FirstChoice? You're treating ChoiceA as a type, and FirstChoice as a method call. We can't answer your question without knowing what it is you think ChoiceA FirstChoice() is supposed to do

    Also, you're comparing string references with character literals above, that ain't gonna work the way you hope, either

  3. #3
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by georgemc View Post
    Well, what are ChoiceA and FirstChoice? You're treating ChoiceA as a type, and FirstChoice as a method call. We can't answer your question without knowing what it is you think ChoiceA FirstChoice() is supposed to do

    Also, you're comparing string references with character literals above, that ain't gonna work the way you hope, either
    ChoiceA is the class (according to a recent lesson in my Java class this was what was needed to seperated things between different Java files) and Firstchoice refers to the swing window in that class. Each of the other classes are like that and they all look similar to this code

    Java Code:
    import javax.swing.*;
    import java.text.*;
    import java.awt.*;
    import java.lang.*;
    import java.awt.event.*;
    public class ChoiceA
    {
    
    // create interface
    class Firstchoice extends JFrame
    {
    		JLabel messageLabel;
    		JLabel messageLabel2;
    		JTextField num1;
    		JTextField num2;
    		JButton calcButton;
    		JPanel panel;
    	public Firstchoice()
    	{
    		// Window Title
    		super("Calculate the value of 'a'");
    
    		// Window size
    		setSize(400, 100);
    
    		// set default close method
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		// create Textfields and textfield labels
    		messageLabel = new JLabel("Enter the value of 'b':");
    		num1 = new JTextField(10);
    		messageLabel2 = new JLabel("Enter the value of 'c':");
    		num2 = new JTextField(10);
    
    		// create a button
    		calcButton = new JButton("Calculate");
    
    		// add Action Listener
    		calcButton.addActionListener(new CalcButtonListener());
    
    		// create panel
    		panel = new JPanel();
    
    		// add content to panel
    		panel.add(messageLabel);
    		panel.add(num1);
    		panel.add(messageLabel2);
    		panel.add(num2);
    		panel.add(calcButton);
    
    		// make visible
    		setVisible(true);
    
    		// create container
    		Container contentArea = getContentPane();
    		contentArea.setBackground(Color.white);
    
    		// add panel to content area and set content Pane
    		contentArea.add(panel);
    		setContentPane(contentArea);
    	}
    
    	// create Action Listener for button
    	private class CalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			// declare variables
    			String str1 =  num1.getText();  // get input 1
    			String str2 = num2.getText();  // get input 2
    			double point1 = Double.parseDouble(str1);  /* convert input 1 to double data */
    			double point2 = Double.parseDouble(str2);  /* convert input 2 to double */
    			double step1 = Math.pow(point2, 2) - Math.pow(point1, 2);
    			double results = Math.sqrt(step1);
    
    			// Display results
    			JOptionPane.showMessageDialog(null, "the value of 'b' is: " +str1 + " units\n" + "the value of 'c' is: " +str2 + " units\n" + "the value of 'a' is: " +results + " units");			
    		}
    	}
    }
    
    }
    Yeah, I made that change recently, so it should work fine, like in does in the text-based version.
    Last edited by gotenks05; 10-26-2008 at 08:50 PM.

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

    Default

    So you've fixed it (somehow I have to wonder)?

  5. #5
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    no, just the string problem that was mentioned. I still need help with the rest.

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

    Default

    So you are nesting this FirstChoice class inside of ChoiceA? Why?

  7. #7
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    So you are nesting this FirstChoice class inside of ChoiceA? Why?
    Because I want to keep the java files small and it tells me what one of three variables it is calculating (without needing to view the source, and scroll though many line of code that create three different swingwindows), since the steps to find variables 'a' and 'b' look exactly the same in Java.

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

    Default

    Because I want to keep the java files small and it tells me what one of three variables it is calculating (without needing to view the source, and scroll though many line of code that create three different swingwindows), since the steps to find variables 'a' and 'b' look exactly the same in Java.
    Huh? Sorry, but you have completely lost me here.

  9. #9
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    The problem is that you're guessing at syntax, and, with all due respect, not really explaining your intent very well. You want to create new instances of these XChoice classes? Then what?

  10. #10
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    When not doing professional programming, there is nothing wrong with guessing. I'll just explain how it is supposed to work. The user enters a variable in the pythagorean theorem that they want to find and click select. Using that input, the program opens another swing window that asks a set of question, depending on what the input was. For example, it will ask for the values of 'b' and 'c', if the user wants to find 'a' and so on. After entering the known values in the two textfields and clicking a button labelled calculate, a message dialog will appear displaying the values of all three variables. If none of the three variables are entered, an error message will show. This means ChoiceA.class will run, if the user types in 'a', and ChoiceB.class will run, if 'b' was typed in. Also, if the user put in 'c', it would run ChoiceC.class. That is essentially how it works in the text-based version, except for the fact that everything was in one java file instead of spread out among four seperate java files. Hopefully, this helps a bit.

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

    Default

    When not doing professional programming, there is nothing wrong with guessing.
    No, not really. Programming is very exacting and very unforgiving, and just guessing willy-nilly will never get you very far and could easily get you in hot water. You need to know what methods are available and how to call them. Thank god for the API, a tremendous reference that allows us to search for and inspect classes and their methods. When helping folks here, I try to tell them to never add bad code to good, meaning before adding new code, always compile and test the code that you have to make sure that it works, and if not fix it before trying to add any new code. Your code doesn't pass this rule as it's chock full of problems that prevent compilation.

    The way to fix your problem is to solve small problems one at a time and work your way to the larger ones. Break your task down into its constituent parts and work on each part one at a time. One major problem you need to solve is how to create and show a dialog on button press. I suggest that you create a small program that tries to solve just this issue. Once you have it figured out, you can add it to your larger program, but first get it to work in isolation. Next you can work on adding logic to this dialog and then passing the results to the main program. Note that I said dialog since you really don't want to show sub JFrames but instead want to show modal JDialogs.

    Much luck.

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

    Default

    Myself, I'd first have an enum class, say "Side" have 3 values, A, B, and C. I'd then make one class that allows you to get any two out of the three sides (and not three separate GUI classes here). When initializing this class, I'd pass it a Side value, and based on that value, I'd disable the corresponding JTextFields in a JPanel (they could be held in a Map<Side, JTextField>), and have the user enter the other two sides.

  13. #13
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, not really. Programming is very exacting and very unforgiving, and just guessing willy-nilly will never get you very far and could easily get you in hot water. You need to know what methods are available and how to call them. Thank god for the API, a tremendous reference that allows us to search for and inspect classes and their methods. When helping folks here, I try to tell them to never add bad code to good, meaning before adding new code, always compile and test the code that you have to make sure that it works, and if not fix it before trying to add any new code. Your code doesn't pass this rule as it's chock full of problems that prevent compilation.

    The way to fix your problem is to solve small problems one at a time and work your way to the larger ones. Break your task down into its constituent parts and work on each part one at a time. One major problem you need to solve is how to create and show a dialog on button press. I suggest that you create a small program that tries to solve just this issue. Once you have it figured out, you can add it to your larger program, but first get it to work in isolation. Next you can work on adding logic to this dialog and then passing the results to the main program. Note that I said dialog since you really don't want to show sub JFrames but instead want to show modal JDialogs.

    Much luck.
    Thanks for the tip. So far, I've created five gui apps and each one showed the a message dialog perfectly upon clicking a button. Two of them had conditional statements. Then again, those two were not looking for what this program wanted. Maybe, I'll attempt this again later. Also, guessing was how I got most of my programs completed and I tested and fixed until the source could be compiled.

  14. #14
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    When not doing professional programming, there is nothing wrong with guessing
    Well, there is, because it leads to problems such as you're having. Sorry, but if your attitude is that you can't be bothered to try and do things properly, not bother explaining wha'ts wrong, and then make excuses for doing so, I don't see why anybody should bother helping you. Lots of luck

  15. #15
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by georgemc View Post
    Well, there is, because it leads to problems such as you're having. Sorry, but if your attitude is that you can't be bothered to try and do things properly, not bother explaining wha'ts wrong, and then make excuses for doing so, I don't see why anybody should bother helping you. Lots of luck

    Sorry for the misunderstanding on your part, my attitude was willing to be helped. However, since there is not a lot from Sun, I do need to be guessing. Yes, creating dialogs came from Sun, but those API documents don't usually explain how to use them until the end and even then, it may not be the way I need them implemented. The terminal does explain what is wrong and I've got it down to only three errors and I may have an idea how to fix it. Sorry if sounds like I made excuses, but the guess and check method is my only option, while waiting.

  16. #16
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    However, since there is not a lot from Sun, I do need to be guessing.
    How does everybody else manage to write code without endlessly having to simply guess at syntax, then? I know this sounds like I'm trying not to help, but honestly, it's impossible to work out your intent from what you've posted so far. Either you want to create instances of classes, or call methods of some sort. We don't know

    "Guess and check" is never necessary, ever. There is plenty of online material to help you out, Sun's own java tutorials would be a start! That's your best bet, you'll start on that, and very quickly realise how to do what you're trying to do. Quicker than trying to explain it when you don't know the correct terminology, too, probably

  17. #17
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    Maybe, because they are more experienced. After all, I am still learning the language. Yes, I can see what your saying about the method or classes. Basically, I was trying to get the main java file to load "Firstchoice()" that was located in this

    Java Code:
    class Firstchoice extends JFrame
    {
    		JLabel messageLabel;
    		JLabel messageLabel2;
    		JTextField num1;
    		JTextField num2;
    		JButton calcButton;
    		JPanel panel;
    	public Firstchoice()
    However, no success came. I did admit that those tutorials helped some (like with the conditional statements and JOptionPane (figuring out how to get boolean variables to work correctly took a bit of time searching)), but did not go through all tutorials though. It was the API docs that were not giving me much help, because how things were used did not come up until the end of the document. Fubarable probably had the best suggestion. However, guess and check was necessary for me to get things to display correctly, such as getting the size of my swing window correct for the content. Maybe, I will go through Sun's tutorials more thoroughly since it is easier to understand than their API docs.

  18. #18
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    I think your trying to work with inner classes correct? The format is going to be like

    Outerclass.Innerclass.methodWithinInnerClass()

    so you'd want
    ChoiceA.FirstChoice.whatever method.
    If you want a constructor do
    ChoiceA.FirstChoice firstChoiceObject = ChoiceA.new FirstChoice();

Similar Threads

  1. executing external programs
    By Levish2002 in forum AWT / Swing
    Replies: 0
    Last Post: 09-10-2008, 08:04 PM
  2. Creating jarfile containing external jarfiles
    By gowry in forum Advanced Java
    Replies: 44
    Last Post: 04-08-2008, 04:18 AM
  3. External JavaScript files not loading
    By sajut in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 06:47 AM
  4. Using Runtime to execute external commands
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 10:14 AM
  5. Use a external file in my program
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:28 AM

Posting Permissions

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