Archived

This topic is now archived and is closed to further replies.

Kix482

[java] Array help....

Recommended Posts

I''m trying to put some code together for class, and it''s not completed but from what I see it should at least compile and be functional. However the compiler doesn''t like my for loop. So if anyone could take a look and point out why it doesn''t like what I''m giving it I''d appreciate it. import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Assign4_3097 extends JApplet implements ActionListener { //Declare references to GUI components JTextField input, output; JLabel prompt; public void init() { int array[]; //declare reference to array array = new int[ 20 ]; //allocate array //Create input object input = new JTextField( 5); //Register a listener to pay attention to text input field input.addActionListener( this ); //Create a label object prompt = new JLabel( "Enter Number:" ); //Set output field, not editable output = new JTextField( 30 ); output.setEditable(false); //Obtain a content pane Container c = getContentPane(); //Set layout to FlowLayout c.setLayout( new FlowLayout() ); //Add Gui components to container c.add( prompt ); c.add( input ); c.add( output ); } //end init public void actionPerformed( ActionEvent e ) { //Get input from input field int temp =Integer.parseInt( input.getText() ); if ( temp < 10 ) showStatus("Invalid Entry!!!"); return; if ( temp > 100 ) showStatus("Invalid Entry!!!"); return; for ( int counter = 0; counter < array.length; counter++ ) array[ counter ] = temp; //More code needed here.... //Reset input field to blank to prepare for next input input.setText(""); } //end actionPerformed } //end class

Share this post


Link to post
Share on other sites
--Smacks forehead-- Must stop posting these things so quickly. I saw that five seconds after I posted. I seem to screw up my variable position alot. Sometimes I wonder why I''m programming....

Share this post


Link to post
Share on other sites
I know I''m being a bother but I have one more question. Why isn''t this working? It''s supposed to take the input, check for duplicates in the array and if there are none print it to the output field. It''s not doing that. My pleas for help goes out one last time....

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Assign4_3097
extends JApplet implements ActionListener {

//Declare references to GUI components
JTextField input, output;
JLabel prompt;

public void init()
{

//Create input object
input = new JTextField( 5 );

//Register a listener to pay attention to text input field
input.addActionListener( this );

//Create a label object
prompt = new JLabel( "Enter Number:" );

//Set output field, not editable
output = new JTextField( 30 );
output.setEditable(false);

//Obtain a content pane
Container c = getContentPane();

//Set layout to FlowLayout
c.setLayout( new FlowLayout() );

//Add Gui components to container
c.add( prompt );
c.add( input );
c.add( output );

} //end init

public void actionPerformed( ActionEvent e )
{

int array[]; //declare reference to array

array = new int[ 20 ]; //allocate array


//Get input from input field
int temp =Integer.parseInt( input.getText() );

if ( temp < 10 )
{
showStatus("Invalid Entry!!!");
return;
}
if ( temp > 100 )
{
showStatus("Invalid Entry!!!");
return;
}
for ( int counter = 0; counter < array.length; counter++ )
{
array[ counter ] = temp;
for ( int counter2 = 0; counter2 < array.length; counter2++ )
{
if (array[ counter2 ] == temp)
{
return;
}
else
{
output.setText( temp + "," );
}
}
}
//Reset input field to blank to prepare for next input
input.setText("");


} //end actionPerformed

} //end class

Share this post


Link to post
Share on other sites
Here''s what those for loops are doing, every time, regardless of input:

(Start first loop)
array[0] = tmp;
(Start second loop)
if (array[0] == tmp) return;

It will always return after the first iteration of the inner loop. Also, if you want to keep the array around between calls to actionPerformed() you need to declare it outside of that function.

Keep trying, you''ll get it!

"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool
------------------------------

Share this post


Link to post
Share on other sites
--Still kinda confused-- Maybe a change in major...or maybe I could burn the Java text book to relieve stress....or stop waiting to the last minute to do things....anyway....--looks at code-- I now looks like this....still won''t work...I''m about to just give up and take a grade on this one....

for ( int counter = 0; counter < array.length; counter++ )
{
array[ counter ] = temp;
for ( int counter2 = 0; counter2 < array.length; counter2++ )
{
if (array[ counter2 ] == temp)
{
output.setText("");
}
else
{
output.setText( temp + "," );
}
}
}

Share this post


Link to post
Share on other sites
quote:
Original post by Kix482
--Still kinda confused-- Maybe a change in major...or maybe I could burn the Java text book to relieve stress....or stop waiting to the last minute to do things....anyway....--looks at code-- I now looks like this....still won''t work...I''m about to just give up and take a grade on this one....



Not really too confusing, you just need more work on the basics. We were all there at one point. :-)

Think your loop through. Here is what you have:


for ( int counter = 0; counter < array.length; counter++ )
{
array[ counter ] = temp;
for ( int counter2 = 0; counter2 < array.length; counter2++ )
{
if (array[ counter2 ] == temp)
{
output.setText("");
}
else
{
output.setText( temp + "," );
}
}
}


First of all, you are setting up every element in the array to temp with your first for loop. Then you check the array with your inner loop to see what is equal to temp.

I know it will be hard to do early on in your programming life, but usually you want to do a prototype first. Think your problem out in pseudo-code methods:

-getInput()
-checkDuplicates()
-displayIfNotInArray()


Now how do these work? Pseudo-code some more:


-getInput() // works
-checkDuplicates() // doesn''t work. how should it work?
-check each element in the array to see if temp is there -- return true or false
-displayIfNotInArray() // doesn''t work. how should it work?
-if (checkDuplicates()) // already there, do nothing
-else // not there, add it


Try making some pseudo-code and see if it helps. :-)

-Steven

Share this post


Link to post
Share on other sites