Variable not found Error,Help please

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1018 Views - Last Post: 02 October 2011 - 09:13 PM Rate Topic: -----

#1 Darklordshivam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-October 11

Variable not found Error,Help please

Posted 02 October 2011 - 03:54 AM

Hello,
Ive made a program with a simple login system with some product caluclation.Now Ive created a separate encrpytion function so as to encrpyt my password,because if I store my password in a string anyone can easily read it,basically I just want to score some marks in my project so I used this system.I made into a seperate class,now in the Admin class when I try to compare the password entered by the user to y2 i.e the new encrypted password it says Variable not found for y2,maybe because I cant extend multiple classes,JFrame is already extended and I cant extend "encrypts" class,This should be the main problem as far as I guess.Any other possible way of overcoming this problem?
The encrypts function is at the last just in case if you are lazy.
The code goes here :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Admin extends JFrame implements ActionListener
{
public static String Password;
JTextField jft1,jft2;
JButton b1,b2;
Container cont;

Admin()
{
cont = getContentPane();
cont.setLayout(null);

JLabel jl1=new JLabel("Username:");
cont.add(jl1);
jl1.setBounds(20,30,100,20);

jft1=new JTextField();
cont.add(jft1);
jft1.setBounds(100,30,200,30);

JLabel jl2=new JLabel("Password:");
cont.add(jl2);
jl2.setBounds(20,80,100,20);

jft2=new JTextField();
cont.add(jft2);
jft2.setBounds(100,80,200,30);

b1=new JButton("Sign In");
cont.add(b1);
b1.setBounds(75,150,100,30);
b1.addActionListener(this);

b2=new JButton("Cancel");
cont.add(b2);
b2.setBounds(200,150,100,30);
b2.addActionListener(this);
}
public static void main(String args[])
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == b2)
System.exit(0);

String user=jft1.getText();
String password=jft2.getText();

if(user.equals("Shivam")&&password.equals(y2))
{
Admin2 jf=new Admin2();
jf.setSize(400,300);
jf.setVisible(true);
}
else
JOptionPane.showMessageDialog(cont,"Invalid Details","Shivam's Program",

JOptionPane.ERROR_MESSAGE);
}
}

class Admin2 extends JFrame implements ActionListener,ItemListener
{
JComboBox jc;
JTextField ft1,ft2,ft3;
JButton B1;
String z,t;
Admin2()
{
Container cont=getContentPane();
cont.setLayout(null);

JLabel l1=new JLabel("Product:");
cont.add(l1);
l1.setBounds(20,30,100,20);

jc=new JComboBox();
jc.addItem("product1");
jc.addItem("product2");
jc.addItem("product3");
jc.addItem("product4");
jc.addItem("product5");
cont.add(jc);
jc.setBounds(80,30,200,20);
jc.addItemListener(this);

JLabel l2=new JLabel("Rate:");
cont.add(l2);
l2.setBounds(20,60,50,20);

ft1=new JTextField();
cont.add(ft1);
ft1.setEditable(false);
ft1.setBounds(80,60,50,20);

JLabel l3=new JLabel("Quantity:");
cont.add(l3);
l3.setBounds(20,90,100,20);

ft2=new JTextField();
cont.add(ft2);
ft2.setBounds(80,90,50,20);

B1=new JButton("Enter");
cont.add(B1);
B1.setBounds(150,150,100,30);
B1.addActionListener(this);
}

public void actionPerformed(ActionEvent ne)
{
String m=ft1.getText();
int a=Integer.parseInt(m);
String n=ft2.getText();
int b=Integer.parseInt(n);
z=jc.getSelectedItem().toString();

int tot=a*b;
t = String.valueOf(tot);

Admin3 admn3=new Admin3(z,m,n,t);
admn3.setSize(400,400);
admn3.setVisible(true);
}

public void itemStateChanged(ItemEvent me)
{
z=jc.getSelectedItem().toString();
if(z.equals("product1"))
ft1.setText("150");
else if(z.equals("product2"))
ft1.setText("250");
else if(z.equals("product3"))
ft1.setText("350");
else if(z.equals("product4"))
ft1.setText("450");
else if(z.equals("product5"))
ft1.setText("550");
}
}

class Admin3 extends JFrame implements ActionListener
{
JTextField ftf1,ftf2,ftf3,ftf0;
String x,y,z,q;
int l,m,n,tot1;
JMenuBar bar= new JMenuBar();
JMenuItem it1,it2;

Admin3(String l,String m,String n,String t)
{
Container cont=getContentPane();
cont.setLayout(null);

JMenu file = new JMenu ("File");
it1 = new JMenuItem("New");
it2 = new JMenuItem("Exit");
file.add(it1);
file.add(it2);

it1.addActionListener(this);
it2.addActionListener(this);

bar.add(file);
setJMenuBar(bar);

JLabel lb1=new JLabel("Product:");
cont.add(lb1);
lb1.setBounds(20,30,100,20);

ftf0=new JTextField();
ftf0.setEditable(false);
ftf0.setBounds(80,30,200,20);
cont.add(ftf0);
ftf0.setText(l);

JLabel lb2=new JLabel("Rate:");
cont.add(lb2);
lb2.setBounds(20,60,50,20);

ftf1=new JTextField();
cont.add(ftf1);
ftf1.setEditable(false);
ftf1.setBounds(80,60,50,20);
ftf1.setText(m);

JLabel lb3=new JLabel("Quantity:");
cont.add(lb3);
lb3.setBounds(20,90,100,20);

ftf2=new JTextField();
cont.add(ftf2);
ftf2.setEditable(false);
ftf2.setBounds(80,90,50,20);
ftf2.setText(n);

JLabel lb4=new JLabel("Total Amt");
cont.add(lb4);
lb4.setBounds(20,120,100,20);

ftf3=new JTextField();
cont.add(ftf3);
ftf3.setEditable(false);
ftf3.setBounds(80,120,100,20);
ftf3.setText(t);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==it1)
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}

if(ae.getSource()==it2)
System.exit(0);
}
}
    class encrypt
{                                 
    static String y1,y2;
    encrypt()
    {
    y1=y2=encrypts();
    }
    public static String encrypts()
                                            {
                                                    String res="";
                                                        String Password="qfgtkpmaixx";
                                                        
                                                        
                                                          for(int i=0;i<=10;i++)
                                                    {
                                                        char ChrPass=Password.charAt(i);
                                                        int aInt = (int)ChrPass;
                                                        aInt=aInt+2; 
                                                        char naya = (char)aInt;                                                       
                                                        res=res+naya;
                                                       
                                    }
                                     return(res);
                                }                          
                                
                                                            }
                                                            


This post has been edited by Darklordshivam: 02 October 2011 - 03:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Variable not found Error,Help please

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Variable not found Error,Help please

Posted 02 October 2011 - 04:11 AM

y2 is defined in class encrypt (class names should begin with capital letters) so is not visible where your error message is occurring.

Do you need encrypt to be a class? It looks more like a method to me, but I didn't analyze your whole design to understand it so I could be wrong. If you can change it to a method, then you could then return y2 (the encrypted password?) to be used as needed.
Was This Post Helpful? 0
  • +
  • -

#3 Darklordshivam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-October 11

Re: Variable not found Error,Help please

Posted 02 October 2011 - 04:22 AM

View PostGregBrannon, on 02 October 2011 - 04:11 AM, said:

y2 is defined in class encrypt (class names should begin with capital letters) so is not visible where your error message is occurring.

Do you need encrypt to be a class? It looks more like a method to me, but I didn't analyze your whole design to understand it so I could be wrong. If you can change it to a method, then you could then return y2 (the encrypted password?) to be used as needed.

After changing it to a method it says "Cannot find symbol y1 and y2"
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Variable not found Error,Help please

Posted 02 October 2011 - 04:30 AM

May be you should post your updated code, and please try to format them for easy reading. Just paste them in NEtBeans(or any IDE) select all and click format at menu lists
Was This Post Helpful? 0
  • +
  • -

#5 Darklordshivam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-October 11

Re: Variable not found Error,Help please

Posted 02 October 2011 - 04:40 AM

View Postsmohd, on 02 October 2011 - 04:30 AM, said:

May be you should post your updated code, and please try to format them for easy reading. Just paste them in NEtBeans(or any IDE) select all and click format at menu lists

Sorry BlueJ doesn't offer formating :(
Updated code goes here:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Admin extends JFrame implements ActionListener
{
public static String Password;
JTextField jft1,jft2;
JButton b1,b2;
Container cont;
static String y1,y2;
Admin()
{
cont = getContentPane();
cont.setLayout(null);

JLabel jl1=new JLabel("Username:");
cont.add(jl1);
jl1.setBounds(20,30,100,20);

jft1=new JTextField();
cont.add(jft1);
jft1.setBounds(100,30,200,30);

JLabel jl2=new JLabel("Password:");
cont.add(jl2);
jl2.setBounds(20,80,100,20);

jft2=new JTextField();
cont.add(jft2);
jft2.setBounds(100,80,200,30);

b1=new JButton("Sign In");
cont.add(b1);
b1.setBounds(75,150,100,30);
b1.addActionListener(this);

b2=new JButton("Cancel");
cont.add(b2);
b2.setBounds(200,150,100,30);
b2.addActionListener(this);
}
public static void main(String args[])
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == b2)
System.exit(0);

String user=jft1.getText();
String password=jft2.getText();

if(user.equals("Shivam")&&password.equals(y2))
{
Admin2 jf=new Admin2();
jf.setSize(400,300);
jf.setVisible(true);
}
else
JOptionPane.showMessageDialog(cont,"Invalid Details","Shivam's Program",

JOptionPane.ERROR_MESSAGE);
}
}

class Admin2 extends JFrame implements ActionListener,ItemListener
{
JComboBox jc;
JTextField ft1,ft2,ft3;
JButton B1;
String z,t;
Admin2()
{
Container cont=getContentPane();
cont.setLayout(null);

JLabel l1=new JLabel("Product:");
cont.add(l1);
l1.setBounds(20,30,100,20);

jc=new JComboBox();
jc.addItem("product1");
jc.addItem("product2");
jc.addItem("product3");
jc.addItem("product4");
jc.addItem("product5");
cont.add(jc);
jc.setBounds(80,30,200,20);
jc.addItemListener(this);

JLabel l2=new JLabel("Rate:");
cont.add(l2);
l2.setBounds(20,60,50,20);

ft1=new JTextField();
cont.add(ft1);
ft1.setEditable(false);
ft1.setBounds(80,60,50,20);

JLabel l3=new JLabel("Quantity:");
cont.add(l3);
l3.setBounds(20,90,100,20);

ft2=new JTextField();
cont.add(ft2);
ft2.setBounds(80,90,50,20);

B1=new JButton("Enter");
cont.add(B1);
B1.setBounds(150,150,100,30);
B1.addActionListener(this);
}

public void actionPerformed(ActionEvent ne)
{
String m=ft1.getText();
int a=Integer.parseInt(m);
String n=ft2.getText();
int b=Integer.parseInt(n);
z=jc.getSelectedItem().toString();

int tot=a*b;
t = String.valueOf(tot);

Admin3 admn3=new Admin3(z,m,n,t);
admn3.setSize(400,400);
admn3.setVisible(true);
}

public void itemStateChanged(ItemEvent me)
{
z=jc.getSelectedItem().toString();
if(z.equals("product1"))
ft1.setText("150");
else if(z.equals("product2"))
ft1.setText("250");
else if(z.equals("product3"))
ft1.setText("350");
else if(z.equals("product4"))
ft1.setText("450");
else if(z.equals("product5"))
ft1.setText("550");
}
}

class Admin3 extends JFrame implements ActionListener
{
JTextField ftf1,ftf2,ftf3,ftf0;
String x,y,z,q;
int l,m,n,tot1;
JMenuBar bar= new JMenuBar();
JMenuItem it1,it2;

Admin3(String l,String m,String n,String t)
{
Container cont=getContentPane();
cont.setLayout(null);

JMenu file = new JMenu ("File");
it1 = new JMenuItem("New");
it2 = new JMenuItem("Exit");
file.add(it1);
file.add(it2);

it1.addActionListener(this);
it2.addActionListener(this);

bar.add(file);
setJMenuBar(bar);

JLabel lb1=new JLabel("Product:");
cont.add(lb1);
lb1.setBounds(20,30,100,20);

ftf0=new JTextField();
ftf0.setEditable(false);
ftf0.setBounds(80,30,200,20);
cont.add(ftf0);
ftf0.setText(l);

JLabel lb2=new JLabel("Rate:");
cont.add(lb2);
lb2.setBounds(20,60,50,20);

ftf1=new JTextField();
cont.add(ftf1);
ftf1.setEditable(false);
ftf1.setBounds(80,60,50,20);
ftf1.setText(m);

JLabel lb3=new JLabel("Quantity:");
cont.add(lb3);
lb3.setBounds(20,90,100,20);

ftf2=new JTextField();
cont.add(ftf2);
ftf2.setEditable(false);
ftf2.setBounds(80,90,50,20);
ftf2.setText(n);

JLabel lb4=new JLabel("Total Amt");
cont.add(lb4);
lb4.setBounds(20,120,100,20);

ftf3=new JTextField();
cont.add(ftf3);
ftf3.setEditable(false);
ftf3.setBounds(80,120,100,20);
ftf3.setText(t);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==it1)
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}

if(ae.getSource()==it2)
System.exit(0);
}

public static String encrypts()
{
         y1=y2=encrypts();

                                           
                                                    String res="";
                                                        String Password="qfgtkpmaixx";
                                                        
                                                        
                                                          for(int i=0;i<=10;i++)
                                                    {
                                                        char ChrPass=Password.charAt(i);
                                                        int aInt = (int)ChrPass;
                                                        aInt=aInt+2; 
                                                        char naya = (char)aInt;                                                       
                                                        res=res+naya;
                                                       
                                    }
                                     return(res);
                                }                          
                                
                                                            }
                                                                                                                    


Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Variable not found Error,Help please

Posted 02 October 2011 - 04:56 AM

In your admin3 class, no where variable are declared so the you use undeclared variable in that class:
    public static String encrypts() {
        y1 = y2 = encrypts();


These variables has been defined in Admin class like static String y1, y2; so cannot be accessed in another class,

This post has been edited by smohd: 02 October 2011 - 04:58 AM

Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Variable not found Error,Help please

Posted 02 October 2011 - 05:02 AM

BlueJ might not offer code formatting, but you might. In fact, there is an extension for BlueJ that does provide code formatting.

What is y2 at line 58? I see you added a line 11 that now defines y1 and y2 for the class Admin, but what are they?

Where does the method encrypts() belong? You added it to Admin3, but you appear to be trying to use its function in Admin. Shouldn't it be in Admin? Or do you intend to use it in Admin3?

I imagined you'd use the new method to return an encrypted password, but you don't call the method anywhere, from any of the Admin classes.

BTW - You surely could come up with better class names. Try to pick names that describe what they do.
Was This Post Helpful? 0
  • +
  • -

#8 Darklordshivam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-October 11

Re: Variable not found Error,Help please

Posted 02 October 2011 - 05:06 AM

View Postsmohd, on 02 October 2011 - 04:56 AM, said:

In your admin3 class, no where variable are declared so the you use undeclared variable in that class:
    public static String encrypts() {
        y1 = y2 = encrypts();


These variables has been defined in Admin class like static String y1, y2; so cannot be accessed in another class,

Same error again : Cannot find symbol-variable y2
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Admin extends JFrame implements ActionListener
{
public static String Password;
JTextField jft1,jft2;
JButton b1,b2;
Container cont;
Admin()
{
cont = getContentPane();
cont.setLayout(null);

JLabel jl1=new JLabel("Username:");
cont.add(jl1);
jl1.setBounds(20,30,100,20);

jft1=new JTextField();
cont.add(jft1);
jft1.setBounds(100,30,200,30);

JLabel jl2=new JLabel("Password:");
cont.add(jl2);
jl2.setBounds(20,80,100,20);

jft2=new JTextField();
cont.add(jft2);
jft2.setBounds(100,80,200,30);

b1=new JButton("Sign In");
cont.add(b1);
b1.setBounds(75,150,100,30);
b1.addActionListener(this);

b2=new JButton("Cancel");
cont.add(b2);
b2.setBounds(200,150,100,30);
b2.addActionListener(this);
}
public static void main(String args[])
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == b2)
System.exit(0);

String user=jft1.getText();
String password=jft2.getText();

if(user.equals("Shivam")&&password.equals(y2))
{
Admin2 jf=new Admin2();
jf.setSize(400,300);
jf.setVisible(true);
}
else
JOptionPane.showMessageDialog(cont,"Invalid Details","Shivam's Program",

JOptionPane.ERROR_MESSAGE);
}
}

class Admin2 extends JFrame implements ActionListener,ItemListener
{
JComboBox jc;
JTextField ft1,ft2,ft3;
JButton B1;
String z,t;
Admin2()
{
Container cont=getContentPane();
cont.setLayout(null);

JLabel l1=new JLabel("Product:");
cont.add(l1);
l1.setBounds(20,30,100,20);

jc=new JComboBox();
jc.addItem("product1");
jc.addItem("product2");
jc.addItem("product3");
jc.addItem("product4");
jc.addItem("product5");
cont.add(jc);
jc.setBounds(80,30,200,20);
jc.addItemListener(this);

JLabel l2=new JLabel("Rate:");
cont.add(l2);
l2.setBounds(20,60,50,20);

ft1=new JTextField();
cont.add(ft1);
ft1.setEditable(false);
ft1.setBounds(80,60,50,20);

JLabel l3=new JLabel("Quantity:");
cont.add(l3);
l3.setBounds(20,90,100,20);

ft2=new JTextField();
cont.add(ft2);
ft2.setBounds(80,90,50,20);

B1=new JButton("Enter");
cont.add(B1);
B1.setBounds(150,150,100,30);
B1.addActionListener(this);
}

public void actionPerformed(ActionEvent ne)
{
String m=ft1.getText();
int a=Integer.parseInt(m);
String n=ft2.getText();
int b=Integer.parseInt(n);
z=jc.getSelectedItem().toString();

int tot=a*b;
t = String.valueOf(tot);

Admin3 admn3=new Admin3(z,m,n,t);
admn3.setSize(400,400);
admn3.setVisible(true);
}

public void itemStateChanged(ItemEvent me)
{
z=jc.getSelectedItem().toString();
if(z.equals("product1"))
ft1.setText("150");
else if(z.equals("product2"))
ft1.setText("250");
else if(z.equals("product3"))
ft1.setText("350");
else if(z.equals("product4"))
ft1.setText("450");
else if(z.equals("product5"))
ft1.setText("550");
}
}

class Admin3 extends JFrame implements ActionListener
{

static String y1,y2;
JTextField ftf1,ftf2,ftf3,ftf0;
String x,y,z,q;
int l,m,n,tot1;
JMenuBar bar= new JMenuBar();
JMenuItem it1,it2;

Admin3(String l,String m,String n,String t)
{
y1 = y2 = encrypts();
Container cont=getContentPane();
cont.setLayout(null);

JMenu file = new JMenu ("File");
it1 = new JMenuItem("New");
it2 = new JMenuItem("Exit");
file.add(it1);
file.add(it2);

it1.addActionListener(this);
it2.addActionListener(this);

bar.add(file);
setJMenuBar(bar);

JLabel lb1=new JLabel("Product:");
cont.add(lb1);
lb1.setBounds(20,30,100,20);

ftf0=new JTextField();
ftf0.setEditable(false);
ftf0.setBounds(80,30,200,20);
cont.add(ftf0);
ftf0.setText(l);

JLabel lb2=new JLabel("Rate:");
cont.add(lb2);
lb2.setBounds(20,60,50,20);

ftf1=new JTextField();
cont.add(ftf1);
ftf1.setEditable(false);
ftf1.setBounds(80,60,50,20);
ftf1.setText(m);

JLabel lb3=new JLabel("Quantity:");
cont.add(lb3);
lb3.setBounds(20,90,100,20);

ftf2=new JTextField();
cont.add(ftf2);
ftf2.setEditable(false);
ftf2.setBounds(80,90,50,20);
ftf2.setText(n);

JLabel lb4=new JLabel("Total Amt");
cont.add(lb4);
lb4.setBounds(20,120,100,20);

ftf3=new JTextField();
cont.add(ftf3);
ftf3.setEditable(false);
ftf3.setBounds(80,120,100,20);
ftf3.setText(t);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==it1)
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}

if(ae.getSource()==it2)
System.exit(0);
}

public static String encrypts()
 {
                                                    String res="";
                                                        String Password="qfgtkpmaixx";
                                                        
                                                        
                                                          for(int i=0;i<=10;i++)
                                                    {
                                                        char ChrPass=Password.charAt(i);
                                                        int aInt = (int)ChrPass;
                                                        aInt=aInt+2; 
                                                        char naya = (char)aInt;                                                       
                                                        res=res+naya;
                                                       
                                    }
                                     return(res);
                                }                          
                                
                                                            }
                                                                                                                    


Was This Post Helpful? 0
  • +
  • -

#9 Darklordshivam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-October 11

Re: Variable not found Error,Help please

Posted 02 October 2011 - 05:15 AM

View PostGregBrannon, on 02 October 2011 - 05:02 AM, said:

BlueJ might not offer code formatting, but you might. In fact, there is an extension for BlueJ that does provide code formatting.

What is y2 at line 58? I see you added a line 11 that now defines y1 and y2 for the class Admin, but what are they?

Where does the method encrypts() belong? You added it to Admin3, but you appear to be trying to use its function in Admin. Shouldn't it be in Admin? Or do you intend to use it in Admin3?

I imagined you'd use the new method to return an encrypted password, but you don't call the method anywhere, from any of the Admin classes.

BTW - You surely could come up with better class names. Try to pick names that describe what they do.

At line 58 im testing the password entered by the user with the real generated password which is stored in y2 String
Ill surely change the class names in some time.If I somehow manage to extend the Admin class with encrpyts everything should be perfect but I Cant :( .
Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Variable not found Error,Help please

Posted 02 October 2011 - 05:20 AM

Because you totally remove the declaration of y2 in your Admin design and you try to use it in the same class here
 if (user.equals("Shivam") && password.equals(y2)) {


As GregBrannon said, you really have to think of the design again, what are those classes designed to do and what every variable is for. This will help you to come with better design, may be we can help you if we know what you mean to do with those classes(also think to use inheritance tree)
Was This Post Helpful? 0
  • +
  • -

#11 Darklordshivam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-October 11

Re: Variable not found Error,Help please

Posted 02 October 2011 - 05:51 AM

View Postsmohd, on 02 October 2011 - 05:20 AM, said:

Because you totally remove the declaration of y2 in your Admin design and you try to use it in the same class here
 if (user.equals("Shivam") && password.equals(y2)) {


As GregBrannon said, you really have to think of the design again, what are those classes designed to do and what every variable is for. This will help you to come with better design, may be we can help you if we know what you mean to do with those classes(also think to use inheritance tree)

I managed to compile the class but it declares the password as wrong
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Admin extends JFrame implements ActionListener
{
static String res;

public static String Password;
JTextField jft1,jft2;
JButton b1,b2;
Container cont;
String y2;

Admin()
{
cont = getContentPane();
cont.setLayout(null);

JLabel jl1=new JLabel("Username:");
cont.add(jl1);
jl1.setBounds(20,30,100,20);

jft1=new JTextField();
cont.add(jft1);
jft1.setBounds(100,30,200,30);

JLabel jl2=new JLabel("Password:");
cont.add(jl2);
jl2.setBounds(20,80,100,20);

jft2=new JTextField();
cont.add(jft2);
jft2.setBounds(100,80,200,30);

b1=new JButton("Sign In");
cont.add(b1);
b1.setBounds(75,150,100,30);
b1.addActionListener(this);

b2=new JButton("Cancel");
cont.add(b2);
b2.setBounds(200,150,100,30);
b2.addActionListener(this);
}
public static void main(String args[])
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}
public static String encrypts()
 {
                                                                                                      
                                                        String Password="qfgtkpmaixx";
                                                        
                                                                                             System.out.println(res);
                                                          for(int i=0;i<=10;i++)
                                                    {
                                                        char ChrPass=Password.charAt(i);
                                                        int aInt = (int)ChrPass;
                                                        aInt=aInt+2; 
                                                        char naya = (char)aInt;                                                       
                                                        res=res+naya;
                                                       
                                    }
                                     return(res);

                                }           
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == b2)
System.exit(0);

String user=jft1.getText();
String password=jft2.getText();

if(user.equals("Shivam")&&password.equals(res))
{
Admin2 jf=new Admin2();
jf.setSize(400,300);
jf.setVisible(true);
}
else
JOptionPane.showMessageDialog(cont,"Invalid Details","Shivam's Program",

JOptionPane.ERROR_MESSAGE);
}
               
                                
                                                            }
                                                                                                                    


class Admin2 extends JFrame implements ActionListener,ItemListener
{
JComboBox jc;
JTextField ft1,ft2,ft3;
JButton B1;
String z,t;
Admin2()
{
Container cont=getContentPane();
cont.setLayout(null);

JLabel l1=new JLabel("Product:");
cont.add(l1);
l1.setBounds(20,30,100,20);

jc=new JComboBox();
jc.addItem("product1");
jc.addItem("product2");
jc.addItem("product3");
jc.addItem("product4");
jc.addItem("product5");
cont.add(jc);
jc.setBounds(80,30,200,20);
jc.addItemListener(this);

JLabel l2=new JLabel("Rate:");
cont.add(l2);
l2.setBounds(20,60,50,20);

ft1=new JTextField();
cont.add(ft1);
ft1.setEditable(false);
ft1.setBounds(80,60,50,20);

JLabel l3=new JLabel("Quantity:");
cont.add(l3);
l3.setBounds(20,90,100,20);

ft2=new JTextField();
cont.add(ft2);
ft2.setBounds(80,90,50,20);

B1=new JButton("Enter");
cont.add(B1);
B1.setBounds(150,150,100,30);
B1.addActionListener(this);
}

public void actionPerformed(ActionEvent ne)
{
String m=ft1.getText();
int a=Integer.parseInt(m);
String n=ft2.getText();
int b=Integer.parseInt(n);
z=jc.getSelectedItem().toString();

int tot=a*b;
t = String.valueOf(tot);

Admin3 admn3=new Admin3(z,m,n,t);
admn3.setSize(400,400);
admn3.setVisible(true);
}

public void itemStateChanged(ItemEvent me)
{
z=jc.getSelectedItem().toString();
if(z.equals("product1"))
ft1.setText("150");
else if(z.equals("product2"))
ft1.setText("250");
else if(z.equals("product3"))
ft1.setText("350");
else if(z.equals("product4"))
ft1.setText("450");
else if(z.equals("product5"))
ft1.setText("550");
}
}

class Admin3 extends JFrame implements ActionListener
{

static String y1,y2;
JTextField ftf1,ftf2,ftf3,ftf0;
String x,y,z,q;
int l,m,n,tot1;
JMenuBar bar= new JMenuBar();
JMenuItem it1,it2;

Admin3(String l,String m,String n,String t)
{
Container cont=getContentPane();
cont.setLayout(null);

JMenu file = new JMenu ("File");
it1 = new JMenuItem("New");
it2 = new JMenuItem("Exit");
file.add(it1);
file.add(it2);

it1.addActionListener(this);
it2.addActionListener(this);

bar.add(file);
setJMenuBar(bar);

JLabel lb1=new JLabel("Product:");
cont.add(lb1);
lb1.setBounds(20,30,100,20);

ftf0=new JTextField();
ftf0.setEditable(false);
ftf0.setBounds(80,30,200,20);
cont.add(ftf0);
ftf0.setText(l);

JLabel lb2=new JLabel("Rate:");
cont.add(lb2);
lb2.setBounds(20,60,50,20);

ftf1=new JTextField();
cont.add(ftf1);
ftf1.setEditable(false);
ftf1.setBounds(80,60,50,20);
ftf1.setText(m);

JLabel lb3=new JLabel("Quantity:");
cont.add(lb3);
lb3.setBounds(20,90,100,20);

ftf2=new JTextField();
cont.add(ftf2);
ftf2.setEditable(false);
ftf2.setBounds(80,90,50,20);
ftf2.setText(n);

JLabel lb4=new JLabel("Total Amt");
cont.add(lb4);
lb4.setBounds(20,120,100,20);

ftf3=new JTextField();
cont.add(ftf3);
ftf3.setEditable(false);
ftf3.setBounds(80,120,100,20);
ftf3.setText(t);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==it1)
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}

if(ae.getSource()==it2)
System.exit(0);
}
}



This post has been edited by Darklordshivam: 02 October 2011 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Variable not found Error,Help please

Posted 02 October 2011 - 08:53 AM

If you encrypt, then you have to decrypt before comparing with user input> look
if (user.equals("Shivam") && password.equals(res))
you are checking with the value which is empty because encrypts() method was called nowhere. And even if it will be called, the value of res will be encrypted value which should be decrypted before comparing
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Variable not found Error,Help please

Posted 02 October 2011 - 07:52 PM

Please avoid null layout and setBounds(). The Java LayoutManagers are much better for organizing your GUI and in more manageable code. Hence the name.

Quote

Sorry BlueJ doesn't offer formating :(

You should still be able to properly indent and space your code yourself.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Variable not found Error,Help please

Posted 02 October 2011 - 08:04 PM

Not that difficult, and beleive me, correctly indenting your code will save you from a lot of trouble
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Admin extends JFrame implements ActionListener
{
	static String res;

	public static String Password;
	JTextField jft1,jft2;
	JButton b1,b2;
	Container cont;
	String y2;

	Admin()
	{
		cont = getContentPane();
		cont.setLayout(null);

		JLabel jl1=new JLabel("Username:");
		cont.add(jl1);
		jl1.setBounds(20,30,100,20);

		jft1=new JTextField();
		cont.add(jft1);
		jft1.setBounds(100,30,200,30);

		JLabel jl2=new JLabel("Password:");
		cont.add(jl2);
		jl2.setBounds(20,80,100,20);

		jft2=new JTextField();
		cont.add(jft2);
		jft2.setBounds(100,80,200,30);

		b1=new JButton("Sign In");
		cont.add(b1);
		b1.setBounds(75,150,100,30);
		b1.addActionListener(this);

		b2=new JButton("Cancel");
		cont.add(b2);
		b2.setBounds(200,150,100,30);
		b2.addActionListener(this);
	}
	public static void main(String args[])
	{
		Admin admn=new Admin();
		admn.setSize(400,400);
		admn.setVisible(true);
	}
	public static String encrypts()
	{

		String Password="qfgtkpmaixx";

		System.out.println(res);
		for(int i=0;i<=10;i++)
		{
			char ChrPass=Password.charAt(i);
			int aInt = (int)ChrPass;
			aInt=aInt+2; 
			char naya = (char)aInt;                                                       
			res=res+naya;

		}
		return(res);

	}           
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource() == b2)
			System.exit(0);

		String user=jft1.getText();
		String password=jft2.getText();

		if(user.equals("Shivam")&&password.equals(res))
		{
			Admin2 jf=new Admin2();
			jf.setSize(400,300);
			jf.setVisible(true);
		}
		else
			JOptionPane.showMessageDialog(cont,"Invalid Details","Shivam's Program",

					JOptionPane.ERROR_MESSAGE);
	}


}



class Admin2 extends JFrame implements ActionListener,ItemListener
{
	JComboBox jc;
	JTextField ft1,ft2,ft3;
	JButton B1;
	String z,t;
	Admin2()
	{
		Container cont=getContentPane();
		cont.setLayout(null);

		JLabel l1=new JLabel("Product:");
		cont.add(l1);
		l1.setBounds(20,30,100,20);

		jc=new JComboBox();
		jc.addItem("product1");
		jc.addItem("product2");
		jc.addItem("product3");
		jc.addItem("product4");
		jc.addItem("product5");
		cont.add(jc);
		jc.setBounds(80,30,200,20);
		jc.addItemListener(this);

		JLabel l2=new JLabel("Rate:");
		cont.add(l2);
		l2.setBounds(20,60,50,20);

		ft1=new JTextField();
		cont.add(ft1);
		ft1.setEditable(false);
		ft1.setBounds(80,60,50,20);

		JLabel l3=new JLabel("Quantity:");
		cont.add(l3);
		l3.setBounds(20,90,100,20);

		ft2=new JTextField();
		cont.add(ft2);
		ft2.setBounds(80,90,50,20);

		B1=new JButton("Enter");
		cont.add(B1);
		B1.setBounds(150,150,100,30);
		B1.addActionListener(this);
	}

	public void actionPerformed(ActionEvent ne)
	{
		String m=ft1.getText();
		int a=Integer.parseInt(m);
		String n=ft2.getText();
		int b=Integer.parseInt(n);
		z=jc.getSelectedItem().toString();

		int tot=a*b;
		t = String.valueOf(tot);

		Admin3 admn3=new Admin3(z,m,n,t);
		admn3.setSize(400,400);
		admn3.setVisible(true);
	}

	public void itemStateChanged(ItemEvent me)
	{
		z=jc.getSelectedItem().toString();
		if(z.equals("product1"))
			ft1.setText("150");
		else if(z.equals("product2"))
			ft1.setText("250");
		else if(z.equals("product3"))
			ft1.setText("350");
		else if(z.equals("product4"))
			ft1.setText("450");
		else if(z.equals("product5"))
			ft1.setText("550");
	}
}

class Admin3 extends JFrame implements ActionListener
{

	static String y1,y2;
	JTextField ftf1,ftf2,ftf3,ftf0;
	String x,y,z,q;
	int l,m,n,tot1;
	JMenuBar bar= new JMenuBar();
	JMenuItem it1,it2;

	Admin3(String l,String m,String n,String t)
	{
		Container cont=getContentPane();
		cont.setLayout(null);

		JMenu file = new JMenu ("File");
		it1 = new JMenuItem("New");
		it2 = new JMenuItem("Exit");
		file.add(it1);
		file.add(it2);

		it1.addActionListener(this);
		it2.addActionListener(this);

		bar.add(file);
		setJMenuBar(bar);

		JLabel lb1=new JLabel("Product:");
		cont.add(lb1);
		lb1.setBounds(20,30,100,20);

		ftf0=new JTextField();
		ftf0.setEditable(false);
		ftf0.setBounds(80,30,200,20);
		cont.add(ftf0);
		ftf0.setText(l);

		JLabel lb2=new JLabel("Rate:");
		cont.add(lb2);
		lb2.setBounds(20,60,50,20);

		ftf1=new JTextField();
		cont.add(ftf1);
		ftf1.setEditable(false);
		ftf1.setBounds(80,60,50,20);
		ftf1.setText(m);

		JLabel lb3=new JLabel("Quantity:");
		cont.add(lb3);
		lb3.setBounds(20,90,100,20);

		ftf2=new JTextField();
		cont.add(ftf2);
		ftf2.setEditable(false);
		ftf2.setBounds(80,90,50,20);
		ftf2.setText(n);

		JLabel lb4=new JLabel("Total Amt");
		cont.add(lb4);
		lb4.setBounds(20,120,100,20);

		ftf3=new JTextField();
		cont.add(ftf3);
		ftf3.setEditable(false);
		ftf3.setBounds(80,120,100,20);
		ftf3.setText(t);
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==it1)
		{
			Admin admn=new Admin();
			admn.setSize(400,400);
			admn.setVisible(true);
		}

		if(ae.getSource()==it2)
			System.exit(0);
	}
}





P.S. I dd it with Notepad in 3 min 20 sec
Was This Post Helpful? 1
  • +
  • -

#15 Darklordshivam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-October 11

Re: Variable not found Error,Help please

Posted 02 October 2011 - 08:22 PM

View Postpbl, on 02 October 2011 - 08:04 PM, said:

Not that difficult, and beleive me, correctly indenting your code will save you from a lot of trouble
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Admin extends JFrame implements ActionListener
{
	static String res;

	public static String Password;
	JTextField jft1,jft2;
	JButton b1,b2;
	Container cont;
	String y2;

	Admin()
	{
		cont = getContentPane();
		cont.setLayout(null);

		JLabel jl1=new JLabel("Username:");
		cont.add(jl1);
		jl1.setBounds(20,30,100,20);

		jft1=new JTextField();
		cont.add(jft1);
		jft1.setBounds(100,30,200,30);

		JLabel jl2=new JLabel("Password:");
		cont.add(jl2);
		jl2.setBounds(20,80,100,20);

		jft2=new JTextField();
		cont.add(jft2);
		jft2.setBounds(100,80,200,30);

		b1=new JButton("Sign In");
		cont.add(b1);
		b1.setBounds(75,150,100,30);
		b1.addActionListener(this);

		b2=new JButton("Cancel");
		cont.add(b2);
		b2.setBounds(200,150,100,30);
		b2.addActionListener(this);
	}
	public static void main(String args[])
	{
		Admin admn=new Admin();
		admn.setSize(400,400);
		admn.setVisible(true);
	}
	public static String encrypts()
	{

		String Password="qfgtkpmaixx";

		System.out.println(res);
		for(int i=0;i<=10;i++)
		{
			char ChrPass=Password.charAt(i);
			int aInt = (int)ChrPass;
			aInt=aInt+2; 
			char naya = (char)aInt;                                                       
			res=res+naya;

		}
		return(res);

	}           
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource() == b2)
			System.exit(0);

		String user=jft1.getText();
		String password=jft2.getText();

		if(user.equals("Shivam")&&password.equals(res))
		{
			Admin2 jf=new Admin2();
			jf.setSize(400,300);
			jf.setVisible(true);
		}
		else
			JOptionPane.showMessageDialog(cont,"Invalid Details","Shivam's Program",

					JOptionPane.ERROR_MESSAGE);
	}


}



class Admin2 extends JFrame implements ActionListener,ItemListener
{
	JComboBox jc;
	JTextField ft1,ft2,ft3;
	JButton B1;
	String z,t;
	Admin2()
	{
		Container cont=getContentPane();
		cont.setLayout(null);

		JLabel l1=new JLabel("Product:");
		cont.add(l1);
		l1.setBounds(20,30,100,20);

		jc=new JComboBox();
		jc.addItem("product1");
		jc.addItem("product2");
		jc.addItem("product3");
		jc.addItem("product4");
		jc.addItem("product5");
		cont.add(jc);
		jc.setBounds(80,30,200,20);
		jc.addItemListener(this);

		JLabel l2=new JLabel("Rate:");
		cont.add(l2);
		l2.setBounds(20,60,50,20);

		ft1=new JTextField();
		cont.add(ft1);
		ft1.setEditable(false);
		ft1.setBounds(80,60,50,20);

		JLabel l3=new JLabel("Quantity:");
		cont.add(l3);
		l3.setBounds(20,90,100,20);

		ft2=new JTextField();
		cont.add(ft2);
		ft2.setBounds(80,90,50,20);

		B1=new JButton("Enter");
		cont.add(B1);
		B1.setBounds(150,150,100,30);
		B1.addActionListener(this);
	}

	public void actionPerformed(ActionEvent ne)
	{
		String m=ft1.getText();
		int a=Integer.parseInt(m);
		String n=ft2.getText();
		int b=Integer.parseInt(n);
		z=jc.getSelectedItem().toString();

		int tot=a*b;
		t = String.valueOf(tot);

		Admin3 admn3=new Admin3(z,m,n,t);
		admn3.setSize(400,400);
		admn3.setVisible(true);
	}

	public void itemStateChanged(ItemEvent me)
	{
		z=jc.getSelectedItem().toString();
		if(z.equals("product1"))
			ft1.setText("150");
		else if(z.equals("product2"))
			ft1.setText("250");
		else if(z.equals("product3"))
			ft1.setText("350");
		else if(z.equals("product4"))
			ft1.setText("450");
		else if(z.equals("product5"))
			ft1.setText("550");
	}
}

class Admin3 extends JFrame implements ActionListener
{

	static String y1,y2;
	JTextField ftf1,ftf2,ftf3,ftf0;
	String x,y,z,q;
	int l,m,n,tot1;
	JMenuBar bar= new JMenuBar();
	JMenuItem it1,it2;

	Admin3(String l,String m,String n,String t)
	{
		Container cont=getContentPane();
		cont.setLayout(null);

		JMenu file = new JMenu ("File");
		it1 = new JMenuItem("New");
		it2 = new JMenuItem("Exit");
		file.add(it1);
		file.add(it2);

		it1.addActionListener(this);
		it2.addActionListener(this);

		bar.add(file);
		setJMenuBar(bar);

		JLabel lb1=new JLabel("Product:");
		cont.add(lb1);
		lb1.setBounds(20,30,100,20);

		ftf0=new JTextField();
		ftf0.setEditable(false);
		ftf0.setBounds(80,30,200,20);
		cont.add(ftf0);
		ftf0.setText(l);

		JLabel lb2=new JLabel("Rate:");
		cont.add(lb2);
		lb2.setBounds(20,60,50,20);

		ftf1=new JTextField();
		cont.add(ftf1);
		ftf1.setEditable(false);
		ftf1.setBounds(80,60,50,20);
		ftf1.setText(m);

		JLabel lb3=new JLabel("Quantity:");
		cont.add(lb3);
		lb3.setBounds(20,90,100,20);

		ftf2=new JTextField();
		cont.add(ftf2);
		ftf2.setEditable(false);
		ftf2.setBounds(80,90,50,20);
		ftf2.setText(n);

		JLabel lb4=new JLabel("Total Amt");
		cont.add(lb4);
		lb4.setBounds(20,120,100,20);

		ftf3=new JTextField();
		cont.add(ftf3);
		ftf3.setEditable(false);
		ftf3.setBounds(80,120,100,20);
		ftf3.setText(t);
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==it1)
		{
			Admin admn=new Admin();
			admn.setSize(400,400);
			admn.setVisible(true);
		}

		if(ae.getSource()==it2)
			System.exit(0);
	}
}





P.S. I dd it with Notepad in 3 min 20 sec

Thanks for the try but it says invalid password,thou the password is supposed to be "shivmrockzz" it says its wrong :/

View Postsmohd, on 02 October 2011 - 08:53 AM, said:

If you encrypt, then you have to decrypt before comparing with user input> look
if (user.equals("Shivam") && password.equals(res))
you are checking with the value which is empty because encrypts() method was called nowhere. And even if it will be called, the value of res will be encrypted value which should be decrypted before comparing

What are you talking about the decrypted pass is stored in "res" string itself?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2