divisible by two

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 6353 Views - Last Post: 06 May 2007 - 04:56 PM Rate Topic: -----

#1 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

divisible by two

Posted 03 May 2007 - 03:36 PM

I have two J text feilds and 1 scroll pane and a Jbutton for my applet assignment I have to make a program where you put a beginning number and an ending number into the J text feilds and click the Jbutton and in the scoll pane is to display all the numbers disivisible by 2 between the 2 numbers. How would I start?
Is This A Good Question/Topic? 0
  • +

Replies To: divisible by two

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: divisible by two

Posted 03 May 2007 - 05:23 PM

with an applet base such as the one here

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.
Was This Post Helpful? 0
  • +
  • -

#3 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

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

Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: divisible by two

Posted 04 May 2007 - 01:09 AM

Try to copy&paste the error, because I am sure, that no legit compiler wouls say something like "int.java.lang.String"!
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.
Was This Post Helpful? 0
  • +
  • -

#5 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 46
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: divisible by two

Posted 04 May 2007 - 09:29 AM

I am gonna show the code:

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?
Was This Post Helpful? 0
  • +
  • -

#6 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

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

Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: divisible by two

Posted 04 May 2007 - 10:28 AM

It appears as if start and end have been declared as string, but you're trying to use them in a comparisons to numerical values...you'll have to convert them into numbers.
Was This Post Helpful? 0
  • +
  • -

#8 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

Re: divisible by two

Posted 04 May 2007 - 10:44 AM

I guess that means I cant do it that way then? So do I need to write a loop so that when I hit my button the Jtextfeilds loop themselves into the scrollpane or do I have to do somthing else to make that happen?
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: divisible by two

Posted 04 May 2007 - 05:49 PM

You are taking the numbers start and end via a text area. You are string them as text. Before using them in mathematical operations or in comparisons with numbers, you have to convert the text version of the numbers into actual numbers. Check the Integer.parseInt() method.
Was This Post Helpful? 0
  • +
  • -

#10 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 46
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: divisible by two

Posted 04 May 2007 - 07:19 PM

View PostAmadeus, on 4 May, 2007 - 08:49 PM, said:

You are taking the numbers start and end via a text area. You are string them as text. Before using them in mathematical operations or in comparisons with numbers, you have to convert the text version of the numbers into actual numbers. Check the Integer.parseInt() method.


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
Was This Post Helpful? 0
  • +
  • -

#11 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

Re: divisible by two

Posted 04 May 2007 - 10:36 PM

I added that to my code to get the Text of my beginning and ending numbers but my loop errors wont make it run I copyied the loop up above as u asked
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

Was This Post Helpful? 0
  • +
  • -

#12 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 46
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: divisible by two

Posted 05 May 2007 - 09:19 AM

View Postrockstar4cs, on 5 May, 2007 - 01:36 AM, said:

I added that to my code to get the Text of my beginning and ending numbers but my loop errors wont make it run I copyied the loop up above as u asked
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

Was This Post Helpful? 0
  • +
  • -

#13 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

Re: divisible by two

Posted 05 May 2007 - 03:46 PM

I added your loop in place of my loop and am still getting the same errors from the compiler I didnt understand the word concat in your loop I figuered that was where I wanted my loop to display at so I declared that display instead witch didnt change anything at all but heres all I have. All of my issues seem to be with the loop from the 3 errors I'm getting I will mark them to show you.


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

Was This Post Helpful? 0
  • +
  • -

#14 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 46
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: divisible by two

Posted 05 May 2007 - 10:08 PM

View Postrockstar4cs, on 5 May, 2007 - 06:46 PM, said:

I added your loop in place of my loop and am still getting the same errors from the compiler I didnt understand the word concat in your loop I figuered that was where I wanted my loop to display at so I declared that display instead witch didnt change anything at all but heres all I have. All of my issues seem to be with the loop from the 3 errors I'm getting I will mark them to show you.


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.
Was This Post Helpful? 0
  • +
  • -

#15 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

Re: divisible by two

Posted 06 May 2007 - 04:55 PM

That worked thank you sir
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2