i am having trouble putting together all the pieces of the puzzle to work together in harmony. My final project is simple,i have a jlabel with the question of how much does your dog weigh, I have a button and a jtextfield, the user enters the amount of there dog, and the program will tell them what size dog there's is... ( e.g. small, large )
I cant figure out what works with what, i have tried things from if statements but they don't work with strings, to get source, but i have a problem with errors... What should i use to get the outcome i want...? here is my starting code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Final2 extends JFrame implements ActionListener

{

JLabel question = new JLabel("How much does your dog weigh?");
Font bigFont = new Font("Arial", Font.BOLD, 16);
JTextField answer = new JTextField(10);
JButton pressMe = new JButton("press me");
JLabel greeting = new JLabel("");
final int WIDTH = 300;
final int HEIGHT = 300;
Container con = getContentPane();
public Final2()

{

super("Final Project");
setSize(WIDTH,HEIGHT);
setLayout(new FlowLayout());
question.setFont(bigFont);
greeting.setFont(bigFont);
greeting.setForeground(Color.WHITE);
question.setForeground(Color.WHITE);
add(question);
add(answer);
add(pressMe);
add(greeting);
pressMe.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.setBackground(Color.BLUE);

}

public void actionPerformed(ActionEvent e)

{

String breed = answer.getText();
String greet = "Your dog is of " + breed + " breed.";
greeting.setText(greet);
}
public static void main(String[] args)

{


Final2 frame = new Final2();


}




}

Recommended Answers

All 29 Replies

I'm not clear what the issue is, just that there are problems.

I did notice you said you couldn't use if statements with strings. Maybe this could help.

// you have a string dogtype
if(dogtype.equals("large"))
size = 3;
if(dogtype.equals.("medium"))
size = 2;
if(dogtype.equals.("small"))
size = 1;

StringName.equals("matching text") is the method you use to compare a string to something.

Mike

The issue lies in lines 42 - 44. My button works, and my jtestfield picks up the answer but i am having trouble comparing the answer for example: they enter there weight: 33
i am trying to set up some sort of array,int,string, something that works with what i have on there now, small would = 1lb - 30lb, medium =31lb- 60lb, so that when the user enter the number it will select the proper size and return it. Sorry for the confussion,

I'm not clear what the issue is, just that there are problems.

I did notice you said you couldn't use if statements with strings. Maybe this could help.

// you have a string dogtype
if(dogtype.equals("large"))
size = 3;
if(dogtype.equals.("medium"))
size = 2;
if(dogtype.equals.("small"))
size = 1;

StringName.equals("matching text") is the method you use to compare a string to something.

Mike

thanks a bunch, this could save me a night of sleep.. hahah

i have tried in many ways but cant seem to get this to work at all.. i know its vague, but any suggestions?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Final4 extends JFrame implements ActionListener

{
int lw = 30;
int mw = 31;
int mw2 = 60;
int b = 61;
int b2 = 90;
int br;


JLabel question = new JLabel("How much does your dog weigh?");
Font bigFont = new Font("Arial", Font.BOLD, 16);
JTextField answer = new JTextField(10);
JButton pressMe = new JButton("press me");
JLabel greeting = new JLabel("");
final int WIDTH = 300;
final int HEIGHT = 300;
Container con = getContentPane();
public Final4()

{

super("Final Project");
setSize(WIDTH,HEIGHT);
setLayout(new FlowLayout());
question.setFont(bigFont);
greeting.setFont(bigFont);
greeting.setForeground(Color.WHITE);
question.setForeground(Color.WHITE);
add(question);
add(answer);
add(pressMe);
add(greeting);
pressMe.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.setBackground(Color.BLUE);

}

public void actionPerformed(ActionEvent e)

{
br = Integer.valueOf(greeting);
String breed = answer.getText();
String greet = "Your dog is of " + breed + " breed.";
greeting.setText(greet);

if(breed < lw )
{
System.out.print("Your dog is of a small breed");

}
else
if(breed > 30 && breed < 61)
{
System.out.print("Your dog is of a medium breed");
}
else
if(breed > 60 && breed < 91)
{
System.out.print("Your dog is of a large breed");

}
else
if(breed > 90 && breed < 121)
{
System.out.print("Your dog is of a xtra large breed");

}

}

public static void main(String[] args)

{


Final4 frame = new Final4();


}


}

br = Integer.valueOf(greeting);
String breed = answer.getText();
String greet = "Your dog is of " + breed + " breed.";
greeting.setText(greet);

if(breed < lw )
{
System.out.print("Your dog is of a small breed");

}

in looking at this block of code. I see you using Integer which is like double and two things come to mind.

One i think the when you do converstions, which you are doing with the constructor, you have to place it in a try.

like

try
{
Integer br = new Integer(greeting);
}
catch(Exception e)
{
// could get here if they enter 'z' or 33.3 not an integer
}

Also below you use breed like its a number. your number that i see is br not breed. breed appears to be a string so all your less than or greater than is happening on a string which leaves you where you were.


Integer and Double are actually classes and not the same as int or double. They are objects that have methods and constructors and one field or variable as well, the actual int or double value they contain.

If you make an Integer or Double object you may want to make right after an int or double if it didn't fail the try by getting the number value in the object and storing it in an int or double.

so you maybe turn a string into an Integer and then find an Integer method to get the underlying int, then use it as normal.
Mike

also your code readability would go up if you used variable names that made sense always.

br = Integer.valueOf(greeting);
String breed = answer.getText();
String greet = "Your dog is of " + breed + " breed.";
greeting.setText(greet);

in your first line you are treeting greeing like its a string that contains a number. If its a number why not a variable name that indicates it's a number.

later you are doing a set text like you want to talk back to them. Everything seems very jumbled like i'm never sure what a variable is suppose to do or if it's doing the right thing.

Mike

I was wondering if you could help me with one last thing, instead of trying to turn something into a int or double.... i want to limit my users to only entering in numbers into my jlabel when the question is asked..... ("000-999")

if you want it in the form of 000 or 999 and in between you can use string operations to validate they enter it this way.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

charAt(index) for example ( first you do check the string lenght and make sure its 3 or if your accepting 2 or 1 one of those). you can check if each of your chars is '0' through '9'. but i think you need to at least turn it into an integer to do > <. The java swing components all use strings i think so you cant ask the component to take numbers i dont think. Validating the form of a data entry, is common, at work we had an issue were in a field a phone number had to be entered as 10 digits 0-9 but no '-'s or spaces or anything. the above string methods can be used to check stuff like that.

I tried using if statements to do this, i was sure i had it, but then i got some errors: ( im not sure what else to do, i have been at this for a while now.)

Final4.java:39: operator >= cannot be applied to javax.swing.JTextField,int
if(answer >= 1)
          ^
Final4.java:42: operator <= cannot be applied to javax.swing.JTextField,int
if(answer <= 999)
          ^
Final4.java:59: cannot find symbol
symbol  : variable weight
location: class Final4
String greet = "Your dog is of " + weight + " breed.";
                                   ^
Final4.java:62: operator < cannot be applied to javax.swing.JTextField,int
if(answer < lw )
          ^
Final4.java:68: operator > cannot be applied to java.lang.String,int
if(breed > 30 && breed < 61)
         ^
Final4.java:68: operator < cannot be applied to java.lang.String,int
if(breed > 30 && breed < 61)
                       ^
Final4.java:73: operator > cannot be applied to java.lang.String,int
if(breed > 60 && breed < 91)
         ^
Final4.java:73: operator < cannot be applied to java.lang.String,int
if(breed > 60 && breed < 91)
                       ^
Final4.java:79: operator > cannot be applied to java.lang.String,int
if(breed > 90 && breed < 121)
         ^
Final4.java:79: operator < cannot be applied to java.lang.String,int
if(breed > 90 && breed < 121)
                       ^


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Final4 extends JFrame implements ActionListener
{
int lw = 30;
int mw = 31;
int mw2 = 60;
int b = 61;
int b2 = 90;
int breed;
JLabel question = new JLabel("How much does your dog weigh?");
Font bigFont = new Font("Arial", Font.BOLD, 16);
JTextField answer = new JTextField(10);
JButton pressMe = new JButton("press me");
JLabel greeting = new JLabel("");
final int WIDTH = 300;
final int HEIGHT = 300;
Container con = getContentPane();
public Final4()
{
super("Final Project");
setSize(WIDTH,HEIGHT);
setLayout(new FlowLayout());
question.setFont(bigFont);
greeting.setFont(bigFont);
greeting.setForeground(Color.WHITE);
question.setForeground(Color.WHITE);
boolean validWeight = false;
add(question);
add(pressMe);
add(answer);
if(answer >= 1)
validWeight = true;
else
if(answer <= 999)
validWeight = true;
else
if(validWeight = false)
System.out.println("Sorry, please enter a number between 0-999");
add(greeting);
pressMe.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.setBackground(Color.BLUE);
}
public void actionPerformed(ActionEvent e)
{
String breed = answer.getText();
String greet = "Your dog is of " + weight + " breed.";
greeting.setText(greet);
if(answer < lw )
{
System.out.print("Your dog is of a small breed");
}
else
if(breed > 30 && breed < 61)
{
System.out.print("Your dog is of a medium breed");
}
else
if(breed > 60 && breed < 91)
{
System.out.print("Your dog is of a large breed");
}
else
if(breed > 90 && breed < 121)
{
System.out.print("Your dog is of a xtra large breed");
}
}
public static void main(String[] args)
{
Final4 frame = new Final4();
}
}

you can only use a variable in the way it allows itself to be used. Answer is a JTextField, it looks like. comparisons like is Answer > 1 , probably dont make sense on a JTextField. In java you go from on variable to getting information about it in another variable ( the one it allows you in this case you can use some type of 'get' method to get the string that is answer), And when you got the String you can use the Integer method to get an integer. You cant really just decide to use a JTextField as a number. What it contains is just one of its many properties which is why it resists your attempt to stereotype it as mainly somehow a number. It has methods to change the color of its field, other things, it doesnt think of itself as a number and thats probably why you are getting errors.

I tried using if statements to do this, i was sure i had it, but then i got some errors: ( im not sure what else to do, i have been at this for a while now.)
Final4.java:39: operator >= cannot be applied to javax.swing.JTextField,int
if(answer >= 1)
^
Final4.java:42: operator <= cannot be applied to javax.swing.JTextField,int
if(answer <= 999)
^
Final4.java:59: cannot find symbol
symbol : variable weight
location: class Final4
String greet = "Your dog is of " + weight + " breed.";
^
Final4.java:62: operator < cannot be applied to javax.swing.JTextField,int
if(answer < lw )
^
Final4.java:68: operator > cannot be applied to java.lang.String,int
if(breed > 30 && breed < 61)
^
Final4.java:68: operator < cannot be applied to java.lang.String,int
if(breed > 30 && breed < 61)
^
Final4.java:73: operator > cannot be applied to java.lang.String,int
if(breed > 60 && breed < 91)
^
Final4.java:73: operator < cannot be applied to java.lang.String,int
if(breed > 60 && breed < 91)
^
Final4.java:79: operator > cannot be applied to java.lang.String,int
if(breed > 90 && breed < 121)
^
Final4.java:79: operator < cannot be applied to java.lang.String,int
if(breed > 90 && breed < 121)
^


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Final4 extends JFrame implements ActionListener
{
int lw = 30;
int mw = 31;
int mw2 = 60;
int b = 61;
int b2 = 90;
int breed;
JLabel question = new JLabel("How much does your dog weigh?");
Font bigFont = new Font("Arial", Font.BOLD, 16);
JTextField answer = new JTextField(10);
JButton pressMe = new JButton("press me");
JLabel greeting = new JLabel("");
final int WIDTH = 300;
final int HEIGHT = 300;
Container con = getContentPane();
public Final4()
{
super("Final Project");
setSize(WIDTH,HEIGHT);
setLayout(new FlowLayout());
question.setFont(bigFont);
greeting.setFont(bigFont);
greeting.setForeground(Color.WHITE);
question.setForeground(Color.WHITE);
boolean validWeight = false;
add(question);
add(pressMe);
add(answer);
if(answer >= 1)
validWeight = true;
else
if(answer <= 999)
validWeight = true;
else
if(validWeight = false)
System.out.println("Sorry, please enter a number between 0-999");
add(greeting);
pressMe.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.setBackground(Color.BLUE);
}
public void actionPerformed(ActionEvent e)
{
String breed = answer.getText();
String greet = "Your dog is of " + weight + " breed.";
greeting.setText(greet);
if(answer < lw )
{
System.out.print("Your dog is of a small breed");
}
else
if(breed > 30 && breed < 61)
{
System.out.print("Your dog is of a medium breed");
}
else
if(breed > 60 && breed < 91)
{
System.out.print("Your dog is of a large breed");
}
else
if(breed > 90 && breed < 121)
{
System.out.print("Your dog is of a xtra large breed");
}
}
public static void main(String[] args)
{
Final4 frame = new Final4();
}
}

thank you im going to try and use the get method, hopefully i wont have to ask another question....hahah

typically the exception is where you hanlde the please enter a number between 0-999.

try {
Integer myint = new Integer(answer.getText()); // whatever the method is to get the string in the text field getText?
int mynewint=myint.intValue();

if(mynewint < 0 || mynewint > 999)
{
// caugth they entering a bad number
}
else
{
// your catch will tell you if they didnt enter a nubmer you still need to validate that the number , if your here it a numer, is from 0-999
}
}
catch
{
System.out.println("HA ! thought i wouldnt notice that that wasnt a number from 0-999")
}

Would i enter that in the actionlistener(actionevent e) {}?
or would that go in the method?

Would i enter that in the actionlistener(actionevent e) {}?
or would that go in the method?

i added an else to the code. that goes in action listener. but you may have an issue there as well. I think that might be called on every keystroke. so perhaps do nothing unless they enter a string of 3 characters. otherwise you are going to have to listen for more than just a keystrock event and maybe listen for them hitting enter

didn't read your code carefully enough. the action listener is on the button so you are ok.

I have a silly question, i finally got my code to compile as well as run, but i want to change my print out from printing out on the bottom of the software to being able to print it out on the screen..... my mind is drawing a blank as to what to put in the System.out's place.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Final4 extends JFrame implements ActionListener
{
int lw = 30;
int mw = 31;
int mw2 = 60;
int b = 61;
int b2 = 90;
JLabel question = new JLabel("How much does your dog weigh?");
Font bigFont = new Font("Arial", Font.BOLD, 16);
JTextField answer = new JTextField(10);
JButton pressMe = new JButton("press me");
JLabel greeting = new JLabel("");
final int WIDTH = 300;
final int HEIGHT = 300;
Container con = getContentPane();
public Final4()
{
super("Final Project");
setSize(WIDTH,HEIGHT);
setLayout(new FlowLayout());
question.setFont(bigFont);
greeting.setFont(bigFont);
greeting.setForeground(Color.WHITE);
question.setForeground(Color.WHITE);
boolean validWeight = false;
add(question);
add(pressMe);
add(answer);
add(greeting);
answer.addActionListener(this);
pressMe.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.setBackground(Color.BLUE);
}
public void actionPerformed(ActionEvent e)
{
String breed = answer.getText();
int breedInt = Integer.parseInt(breed);
if(breedInt < lw )
{
System.out.print("Your dog is of a small breed");
}
else
if(breedInt > 30 && breedInt < 61)
{
System.out.print("Your dog is of a medium breed");
}
else
if(breedInt > 60 && breedInt < 91)
{
System.out.print("Your dog is of a large breed");
}
else
if(breedInt > 90)
{
System.out.print("Your dog is of a xtra large breed");
}
}
public static void main(String[] args)
{
Final4 frame = new Final4();
frame.setVisible(true);
}
}

glad you are having some progress. Sounds like you want to create one or more Jlabel for example. that you can write the output of the program that you are putting in system.out now. you allready use a jlable, maybe google 'java jlable' and look up the method to set the text it displays on the fly.

Mike

I ran your program. it is a nice program for what it's doing. i think the jlabel additon shouldnt be so hard to add. One thing though. do you know what an exception is? Covered it in your course work?

If i type zeb for what does my dog weight, and i click press me, your program responds in the console with an error that an uncaught number format exception was thrown. That is why i recommended placing the integer conversion in a try block. You can catch all kinds of exceptions such as NumberFormatException ( if that is the typing out of it's name) but you can also, if you choose always catch the root exception, Exception, i.e.

try {

some code

}
catch(Exception e)
{
// you got here cause they typed zeb
}

read up on exceptions here if you dont know much about them.

http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

mike

Thanks, right as i replyed i found the spot, that says setText()...hahaha a couple more steps and im complete. the teacher better give me a A..haha

you have in that action event method

int breedInt = Integer.parseInt(breed);
if(breedInt < lw )
{
System.out.print("Your dog is of a small breed");
}
else
if(breedInt > 30 && breedInt < 61)
{
System.out.print("Your dog is of a medium breed");
}
else
if(breedInt > 60 && breedInt < 91)
{
System.out.print("Your dog is of a large breed");
}
else
if(breedInt > 90)
{
System.out.print("Your dog is of a xtra large breed");
}

all you really need to do is surround the above block of code with a try like

try {

the above block of code

}
catch(Exception e)
{
// do nothing or System.out.println("i dont think that was a number");
}

at a basic level exceptions are not hard to use. if the integer conversion fails, they type zebra to be malicious and hit pressme, it skips the rest of the code in the try block and executes the code in the catch. If the integer conversion succeeds, it continues down the code in the try block and never does the catch.

In both cases once it does the try and if neccesary the catch, the program continues on as normal on the line below it all which in the above case happens to be the end of the method so nothing more is done.

Mike

my finals specifications.....

Comments Enough so another programmer will know what the program is doing without even reading your descriptionAlso, include your name, date, and program name at the top of the program
Selection structure Include either If…Then logic or a case structure
Repetition structure Include either Do…Loop or For…Next loop
Array Any type of an array
Components Button, TextField, and Label
Theme There must be some point to your program, not just a collection of components
Simple Animation and/or Graphic
Color
Receives user input
Performs output
You will design a JFrame Application that contains at least 2 of the 3, (selection, loop or array) plus all of the others mentioned above:-

do you have any ideas as to what to use a loop for? i can add a random graphics....lol

This is my final program, so i have to add a couple more of specifications then i would like to add the exception. where would i add the exception at?

my above post where i post your code in the method action event lays out exactly where to put the exception. read it carefully. basiclaly your block of code in that method needs to have at top of it, first line of method,

try {

all your code here in that method

then at bottom of your code in that method
close the try and do the catch
}
catch(Exception e)
{
System.out.println("not a valid input"); or something
}
}// close of method, this line is allready in your code

this might get complicated but instead of 'how much does your dog weight"

do

"enter how much your dog weights, hit press me, then repeat for as many dogs as you want to enter" // thats the idea

then store each weight in an array, and sum the array in another output on each entry and give average weight of all dogs entered.

mike
// just an idea :)

i would love to put that in here, but i just need some simple for loop for now, just so it meets requirements, then ill add that as well as some other ideas for fun...hahah

and for the record what counts as an array.

Include either Do…Loop or For…Next loop i can you any of these.

although i dont think i have a array in mine right now, which i need i think we may have to go with your idea...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.