15 Replies - 4825 Views - Last Post: 06 May 2007 - 04:56 PM
#1
divisible by two
Posted 03 May 2007 - 03:36 PM
Replies To: divisible by two
#2
Re: divisible by two
Posted 03 May 2007 - 05:23 PM
then start adding your methods and items into the gui, as a seperate class or contained in this one.
You must show some effort before you recieve help. We are more than happy to solve algorithm troubles, display issues, errors, and more, but you must give us something to work with, we will not do your homework for you.
#3
Re: divisible by two
Posted 03 May 2007 - 06:57 PM
for(int i = start; i <= end; i++){ //1
if(i % 2 == 0)
System.out.printf("\t\t%5d\n", i);
thats my loop I'm getting two errors one says incompatible types for 1 and 2 operator <= cannot be applied to int.java.lang.String
whats that mean?
This post has been edited by rockstar4cs: 03 May 2007 - 10:30 PM
#4
Re: divisible by two
Posted 04 May 2007 - 01:09 AM
And also, paste a bitmore of your code (at least the declaration of variables present in the problematic area), since from this snippet we can't see the type of end, but I guess it's a String. So first you have to convert it to int.
#5
Re: divisible by two
Posted 04 May 2007 - 09:29 AM
int begin = Integer.parseInt(txtAreaBegin.getText()); //Get start number
int end = Integer.parseInt(txtAreaEnd.getText()); //Get end number
for (int i=begin; i<= end; i++){ //Loop through all numbers
if (i%2 == 0){ //If divisible by 2
System.out.print(i + " "); //Print that number
}
}
Is that what you were looking for?
#6
Re: divisible by two
Posted 04 May 2007 - 10:24 AM
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DivisibleByTwo extends JApplet {
private JButton center;
private JPanel p,p2,p3;
private JTextArea display;
private JScrollPane scrollPane;
private JLabel endlabel,startlabel;
private JTextField end,start;
public void init() {
JTextField t1,t2;
startlabel=new JLabel("Enter starting number");
endlabel = new JLabel("Enter ending number");
setLayout(new FlowLayout());
center = new JButton("Show numbers divisible by 2");
end = new JTextField(10);
start = new JTextField(10);
p = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
display = new JTextArea(5,30);
display.setEditable(false);
end.setText("0");
start.setText("0");
scrollPane = new JScrollPane(display);
p.setLayout(new GridLayout (3,1));
p.add(startlabel);
p.add(start);
p.add(endlabel);
p.add(end);
add(p);
p2.setLayout(new GridLayout (4,0));
p2.add(center);
add(p2);
p3.setLayout(new GridLayout (0,1));
p3.add(display);
add(p3);
String start = display.getText();
String end = display.getText();
for(int i = start; i <= end; i++){
if(i % 2 == 0)
System.out.print("\t\t%5d\n", i);
}}
thats what I have I cant paste my errors for some reason when I copy it it says the name of the file divisiblebytwo
I dont actully know what I need I dont understand loops well at all but if somone could give me an example to look at or anything I can use to output my numbers from my textfeilds to my scoll pane I'd appreate it
This post has been edited by rockstar4cs: 04 May 2007 - 10:28 AM
#7
Re: divisible by two
Posted 04 May 2007 - 10:28 AM
#8
Re: divisible by two
Posted 04 May 2007 - 10:44 AM
#9
Re: divisible by two
Posted 04 May 2007 - 05:49 PM
#10
Re: divisible by two
Posted 04 May 2007 - 07:19 PM
Amadeus, on 4 May, 2007 - 08:49 PM, said:
The Integer.parseInt(txtBegin.getText()) returns the int value of the text field, and it can be used in your code. Just take a loop at the snippet I submitted a few posts upper
#11
Re: divisible by two
Posted 04 May 2007 - 10:36 PM
public void actionPerformed(ActionEvent event) {
String str = "";
if(event.getSource() == center) {
for (int i=start; i<= end; i++){ // first and 2nd error
if (i%2 == 0){
str += i + "\n";
thats what my button is going to preform center is my Jbutton my first error says incompadible types my 2nd error says operator can not be applied to int.javax.swing.JtextFeild whats that mean?
This post has been edited by rockstar4cs: 04 May 2007 - 10:40 PM
#12
Re: divisible by two
Posted 05 May 2007 - 09:19 AM
rockstar4cs, on 5 May, 2007 - 01:36 AM, said:
public void actionPerformed(ActionEvent event) {
String str = "";
if(event.getSource() == center) {
for (int i=start; i<= end; i++){ // first and 2nd error
if (i%2 == 0){
str += i + "\n";
thats what my button is going to preform center is my Jbutton my first error says incompadible types my 2nd error says operator can not be applied to int.javax.swing.JtextFeild whats that mean?
Try this:
public void actionPerformed(ActionEvent event) {
String str = "";
if(event.getSource() == center) {
int begin = Integer.parseInt(txtAreaBegin.getText()); //Get start number
int end = Integer.parseInt(txtAreaEnd.getText()); //Get end number
for (int i=begin; i<= end; i++){ //Loop through all numbers
if (i%2 == 0){ //If divisible by 2
str = str.concat(i + " "); //Add to string
}
}
}
}
This post has been edited by alpha02: 05 May 2007 - 09:21 AM
#13
Re: divisible by two
Posted 05 May 2007 - 03:46 PM
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DivisibleByTwo extends JApplet implements ActionListener{
private JButton center;
private JPanel p,p2,p3;
private JTextArea display;
private JScrollPane scrollPane;
private JLabel endlabel,startlabel;
private JTextField end,start;
public void init() {
JTextField t1,t2;
startlabel=new JLabel("Enter starting number");
endlabel = new JLabel("Enter ending number");
center = new JButton("Show numbers divisible by 2");
center.addActionListener(this);
end = new JTextField(10);
start = new JTextField(10);
JTextArea display;
display = new JTextArea(5,30);
display.setEditable(false);
end.setText("0");
start.setText("0");
scrollPane = new JScrollPane(display);
Container con = getContentPane();
JPanel up = new JPanel();
JPanel middle = new JPanel();
JPanel down = new JPanel();
setLayout(new GridLayout(0,5));
setLayout(new FlowLayout());
up.setLayout(new GridLayout(1,2));
up.add(startlabel);
up.add(start);
up.add(endlabel);
up.add(end);
middle.setLayout(new GridLayout(1,1));
middle.add(center);
down.setLayout(new GridLayout(1,1));
down.add(display);
con.add(up);
con.add(middle);
con.add(down);
center.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
String str = "";
if(event.getSource() == center) {
int begin = Integer.parseInt(start.getText());
int end = Integer.parseInt(end.getText()); // error 1
for (int i=start; i<= end; i++){ // error 2
if (i%2 == 0){
str = str.display(i + " "); // error 3
}
}
}
}
}
//error 1 = int cannot be differenced
//error 2 = incompatible types
//error 3 = cannot find symbol method display
This post has been edited by rockstar4cs: 05 May 2007 - 03:47 PM
#14
Re: divisible by two
Posted 05 May 2007 - 10:08 PM
rockstar4cs, on 5 May, 2007 - 06:46 PM, said:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DivisibleByTwo extends JApplet implements ActionListener{
private JButton center;
private JPanel p,p2,p3;
private JTextArea display;
private JScrollPane scrollPane;
private JLabel endlabel,startlabel;
private JTextField end,start;
public void init() {
JTextField t1,t2;
startlabel=new JLabel("Enter starting number");
endlabel = new JLabel("Enter ending number");
center = new JButton("Show numbers divisible by 2");
center.addActionListener(this);
end = new JTextField(10);
start = new JTextField(10);
JTextArea display;
display = new JTextArea(5,30);
display.setEditable(false);
end.setText("0");
start.setText("0");
scrollPane = new JScrollPane(display);
Container con = getContentPane();
JPanel up = new JPanel();
JPanel middle = new JPanel();
JPanel down = new JPanel();
setLayout(new GridLayout(0,5));
setLayout(new FlowLayout());
up.setLayout(new GridLayout(1,2));
up.add(startlabel);
up.add(start);
up.add(endlabel);
up.add(end);
middle.setLayout(new GridLayout(1,1));
middle.add(center);
down.setLayout(new GridLayout(1,1));
down.add(display);
con.add(up);
con.add(middle);
con.add(down);
center.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
String str = "";
if(event.getSource() == center) {
int begin = Integer.parseInt(start.getText());
int end = Integer.parseInt(end.getText()); // error 1
for (int i=start; i<= end; i++){ // error 2
if (i%2 == 0){
str = str.display(i + " "); // error 3
}
}
}
}
}
//error 1 = int cannot be differenced
//error 2 = incompatible types
//error 3 = cannot find symbol method display
Error 1:
In the line:
int end = Integer.parseInt(end.getText()); // error 1
You use the variable end twice and for different purposes, change the name!
Error 2:
In the line:
for (int i=start; i<= end; i++){ // error 2
You used end as the name of the textfield (this is associated with the error 1). You also used start, which is the textbox name.
Error 3:
The variable i is not associated to an integer, but a textfield.
All your errors are linked together.
|
|

New Topic/Question
Reply




MultiQuote






|