Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 08-17-2010, 01:40 PM   #1
In Runtime
 
hockeygoalie5's Avatar
 
Join Date: Dec 2009
Location: United States
Posts: 297
Default Java Problems

Okay, I've programmed a Java application in Eclipse. It was working fine but now the JFrame won't show. After investigating, I found two errors that Eclipse wasn't giving me by compiling it manually.

Code:
AutoTyper.java:16: cannot find symbol
symbol  : class SmartRobot
location: class org.smartrobot.AutoTyper
                SmartRobot robot = new SmartRobot();
                ^

AutoTyper.java:16: cannot find symbol
symbol  : class SmartRobot
location: class org.smartrobot.AutoTyper
                SmartRobot robot = new SmartRobot();
                                       ^
SmartRobot is a class in the same directory and package as AutoTyper.
__________________

hockeygoalie5 is offline   Reply With Quote
Old 08-17-2010, 02:52 PM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Problems

Is SmartRobot definitely being compiled ok without any errors?
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 08-17-2010, 04:24 PM   #3
In Runtime
 
hockeygoalie5's Avatar
 
Join Date: Dec 2009
Location: United States
Posts: 297
Default Re: Java Problems

Yes. What I gather is that for some reason, just on that line, it cannot find 's'?
hockeygoalie5 is offline   Reply With Quote
Old 08-17-2010, 04:47 PM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Problems

Quote:
Yes. What I gather is that for some reason, just on that line, it cannot find 's'?
Nah, it definitely means SmartRobot as a whole (which is what it means by symbol) it just points to the first letter of it. Compiler errors aren't always the easiest things to follow but you get used to them after a while.

I'd really need to see the project to help further - if the SmartRobot class has no errors, is being compiled ok and is located in the same package and folder as the referring class (or the appropriate import statement is used) I don't see any reason why it'll come up with that error.
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 08-17-2010, 06:46 PM   #5
Daemon Poster
 
PokerDegenerate's Avatar
 
Join Date: Dec 2008
Posts: 1,465
Default Re: Java Problems

Quote:
Originally Posted by hockeygoalie5 View Post

Code:
AutoTyper.java:16: cannot find symbol
symbol  : class SmartRobot
location: class org.smartrobot.AutoTyper
                SmartRobot robot = new SmartRobot();
                ^

AutoTyper.java:16: cannot find symbol
symbol  : class SmartRobot
location: class org.smartrobot.AutoTyper
                SmartRobot robot = new SmartRobot();
                                       ^
SmartRobot is a class in the same directory and package as AutoTyper.
Did you import the package?
__________________
Raidmax Smilodon Extreme Black Case -|-AMD Phenom II x4 955 BE @ 3829.50 Mhz (18.5 x 207, vcore 1.424, NB 2600 mhz, HT link 2200 mhz) -|- Xigmatek Dark Knight S1283V -|- Gigabyte MA790XT-UD4P Mobo -|- G. Skill 6Gb (3 x 2Gb) DDR3 1333 RAM 7-7-7-18-2t -|- WD Caviar 7200 RPM 1 TB HDD -|- OCZ GameXtreme 700W -|- Radeon 4870 1Gb @ 780/1050 -|- Asus VW202NR 20" wide-screen -|- Windows 7 Professional 64 Bit -|- Saitek Cyborg Keyboard -|- Razer Lachesis Mouse
PokerDegenerate is offline   Reply With Quote
Old 08-17-2010, 07:28 PM   #6
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Problems

Quote:
Did you import the package?
Quote:
SmartRobot is a class in the same directory and package as AutoTyper.
If the classes are both in the same package there's no need to import it.
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 08-18-2010, 09:13 AM   #7
In Runtime
 
hockeygoalie5's Avatar
 
Join Date: Dec 2009
Location: United States
Posts: 297
Default Re: Java Problems

Okay, I removed that line and it did compile perfect and fine, but the frame still isn't showing! Yes, I set visible to true.

AutoTyper
Code:
package org.smartrobot;

import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class AutoTyper {
    protected static boolean On = false;
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(250, 100);
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextField field = new JTextField();
        JButton start = new JButton("Start");
        JButton stop = new JButton ("Stop");
        field.setColumns(20);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                On = true;
            }
        });
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                On = false;
            }
        });
        for(int s = 0; s < 1;) {
            if(On) {
                for (int i = 0; i < 35000;) {
                    i++;
                    if(i == 35000) {
                        i = 0;
                    }
                }
            }
        }
            frame.add(field);
            frame.add(start);
            frame.add(stop);
            frame.setVisible(true);
    }
}
You may notice it doesn't do what its name suggests, I'm not done yet.

SmartRobot
Code:
package org.smartrobot;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;


public class SmartRobot extends Robot {

 public SmartRobot() throws AWTException {
  super();
 }

 public void keyType(int keyCode) {
  keyPress(keyCode);
  delay(50);
  keyRelease(keyCode);
 }
 
 public void keyType(int keyCode, int keyCodeModifier) {
  keyPress(keyCodeModifier);
  keyPress(keyCode);
  delay(50);
  keyRelease(keyCode);
  keyRelease(keyCodeModifier);
 }

 
 public void type(String text) {
  String textUpper = text.toUpperCase();

  for (int i=0; i<text.length(); ++i) {
   typeChar(textUpper.charAt(i));
  }  
 }
 
 private void typeChar(char c) {
  boolean shift = true;
  int keyCode;
  
  switch (c) {
  case '~':
   keyCode = (int)'`';
   break;
  case '!':
   keyCode = (int)'1';
   break;
  case '@':
   keyCode = (int)'2';
   break;
  case '#':
   keyCode = (int)'3';
   break;
  case '$':
   keyCode = (int)'4';
   break;
  case '%':
   keyCode = (int)'5';
   break;
  case '^':
   keyCode = (int)'6';
   break;
  case '&':
   keyCode = (int)'7';
   break;
  case '*':
   keyCode = (int)'8';
   break;
  case '(':
   keyCode = (int)'9';
   break;
  case ')':
   keyCode = (int)'0';
   break;
  case ':':
   keyCode = (int)';';
   break;
  case '_':
   keyCode = (int)'-';
   break;
  case '+':
   keyCode = (int)'=';
   break;
  case '|':
   keyCode = (int)'\\';
   break;
//  case '"':

//   keyCode = (int)'\'';

//   break;

  case '?':
   keyCode = (int)'/';
   break;
  case '{':
   keyCode = (int)'[';
   break;
  case '}':
   keyCode = (int)']';
   break;
  case '<':
   keyCode = (int)',';
   break;
  case '>':
   keyCode = (int)'.';
   break;
  default:
   keyCode = (int)c;
   shift = false;
  }
  if (shift)
   keyType(keyCode, KeyEvent.VK_SHIFT);
  else
   keyType(keyCode);
 }
 
 @SuppressWarnings("unused")
private int charToKeyCode(char c) {
  switch (c) {
  case ':':
   return ';';
  }
  return (int)c;
 }
}
It was working fine before, with the same code.
hockeygoalie5 is offline   Reply With Quote
Old 08-18-2010, 09:42 AM   #8
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Problems

Quote:
It was working fine before, with the same code.
I don't see how it was!

The problem is here:
Code:
        
for(int s = 0; s < 1;) {
    if(On) {
        for (int i = 0; i < 35000;) {
            i++;
            if(i == 35000) {
                i = 0;
            }
        }
    }
}
frame.add(field);
frame.add(start);
frame.add(stop);
frame.setVisible(true);
What you've got going there is an infinite loop (though you've done it in a bit of an odd way, you don't need either statement there - for(;; ) will do the same thing. while(true) is a better way of doing it though, far better to read.)

Since you never break out of that loop, all the statements below it (including the setVisible() method call) never get executed, thus the frame is never shown.
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 08-18-2010, 09:55 AM   #9
In Runtime
 
hockeygoalie5's Avatar
 
Join Date: Dec 2009
Location: United States
Posts: 297
Default Re: Java Problems

Okay, so that works, but I just can't figure out how to make it constantly check for On without a loop!
hockeygoalie5 is offline   Reply With Quote
Old 08-18-2010, 10:07 AM   #10
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Problems

Code:
for(int s = 0; s < 1;) {
    if(On) {
        for (int i = 0; i < 35000;) {
            i++;
            if(i == 35000) {
                i = 0;
            }
        }
    }
}
What's the above code doing at present, or rather what do you want it to do?
It's just doing some pointless counting at present.

Generally speaking, infinite loops are a bit of a code smell - if you've got one something's probably not right!
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 12:13 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0