Jump to content

only one output...

- - - - -

  • Please log in to reply
6 replies to this topic

#1
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
just made something like this:

public static void main (String args[]) {

    	int a;

    	int[]b=new int [10];

    	for (a=0; a<b.length; a++){

    		String c=JOptionPane.showInputDialog ("Enter a number:");

    		int d=Integer.parseInt ©;

    	}

    	

    	String searchId=JOptionPane.showInputDialog ("Enter number search:");

    	int f=Integer.parseInt (searchId);

    	int e;

    	for (e=0; e<b.length; e++){

    		if (b[e]==f)

    			break;

    		if (e!=f){

    		System.out.println ("Can't find number search "+f);

    		}

    		else{

    			System.out.println("Found number search "+f);

    		}

    	}

and i want to know how to make the output just:

"Cant find number search .. " for integers not on the array and
"Found number search.." for found integers in array... and NOT:

Can't find number search 7
Can't find number search 7
Can't find number search 7
Can't find number search 7
Can't find number search 7
Can't find number search 7
Can't find number search 7
Found number search 7
Can't find number search 7
Can't find number search 7....


just one output...
help please...
thanks again....

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
First of all, you can put int e; inside the for-loop:

I don't think you understand the if-statements you wrote...
        for (int e=0; e<b.length; e++){ 
            if (b[e]==f)  //If the number in the array(b[e]) equals the searchnumber
                break;     //Here you should print you've found it!  Then do an [B]else [/B]saying yuo didn't found it.
            if (e!=f){     //if the number of the loop is not equal to the searchnumber
            System.out.println ("Can't find number search "+f);   
            } 
            else{ 
                System.out.println("Found number search "+f); 
            } 
        }  


#3
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
i did what you suggested.... and i made it into sumething like this...

 public static void main (String args[]) {

    	int a;

    	int[]b=new int [10];

    	for (a=0; a<b.length; a++){

    		String c=JOptionPane.showInputDialog ("Enter a number:");

    		int d=Integer.parseInt ©;

    	}


    	String searchId=JOptionPane.showInputDialog ("Enter number search:");

    	int f=Integer.parseInt (searchId);

    	int e;

    	for (e=0; e<b.length; e++){

    		if (b[e]==f){

    		System.out.println("Found number search "+f);

    		break;

    		}

    		else{

    		System.out.println ("Can't find number search "+f);


    		}

    		if (b[e]!=f){

    			System.out.println ("Can't find number search "+f);

    			break;

    		}

    		else {

    			System.out.println("Found number search "+f);

    		}


    	}

    }



}

and it displays only the can't find message... even when i'm sure i entered the number i searched...
*sigh* i'm really stupid....

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

for (a=0; a<b.length; a++){

String c=JOptionPane.showInputDialog ("Enter a number:");

int d=Integer.parseInt (c);

}

You don't put the number in the array there.
Add b[a] = d;


the 2nd if-statement is the same as the else-statement of the first if.
This

if (b[e]==f){

System.out.println("Found number search "+f);

break;

}

[B][COLOR="red"]else[/COLOR][/B]{

System.out.println ("Can't find number search "+f);

Is the same as:

if (b[e]==f){

System.out.println("Found number search "+f);

break;

}

[B][COLOR="red"]if (b[e]!=f)[/COLOR][/B]{

System.out.println ("Can't find number search "+f);

}


so remove this part

if (b[e]!=f){

System.out.println ("Can't find number search "+f);

break;

}

else {

System.out.println("Found number search "+f);

}


And finally. remove all the break; lines. It will exit the for-loop if the pc gets there.

#5
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
somebody went and made something like this one...(problem is similar to the one i have mentioned earlier;;;;

public static void main(String args[]) {
    	int rows=3;
    	int cols=4;
    	int [][] tablerray=new int [rows][cols];
    		for (int a=0; a<rows; a++){
    			for (int b=0; b<cols; b++){
    				tablerray [a][b]=a*b+1;
    			}
    		}
    		for (int i=0;i<cols; i++){
    			tableArray[rows][i]=i;
    			for (i=0; i<cols; i++){
    				System.out.print (tableArray[rows][i]);
    				System.out.println();
    			}
    		}
    		
    		String searchId=JOptionPane.showInputDialog ("Enter number search:");
    		int c=Integer.parseInt (searchId);
    			for (int j=0; j<rows; j++){
    				for (int k=0; k<cols; k++){
    					if (tableArray[j][k]==c){
    						break;
    					}
    					if (j==c){
    						if (k==c){
    							System.out.println ("Can't find "+c);
    						}
    					}
    					else{
    						System.out.println ("Found "+c);
    					}
    					String delete=JOptionPane.showInputDialog("Enter number to delete:");
    					int d=Integer.parseInt (delete);
    					for (j=0; j<rows; j++){
    						for (k=0; k<cols; k++){
    							if (tableArray[rows][j]==d){
    								break;
    							}
    							for (int l=j; l<rows; l++){
    								for (int m=k; m<cols; m++){
    									tableArray[l][cols]-tableArray[l+1][cols];
    									tableArray[rows][m]-tableArray[rows][m+1];
    									rows--;
    									cols--;
    								}
    							}
    						}
    					}
    					for (j=0; j<rows; j++){
    						for (k=0; k<cols; k++){
    							System.out.print (tableArray[j][k]+" ");
    							System.out.println (" ");
    						}
    					}
    				}
    			}
    }
    
    
}

and it was saying cannot resolve symbol variable tableArray all the way.... can anyone help??? thanks...
oh and thanks by the way to oxano for his help... i wasn't able to thank him earlier because i had to go out for a while...:thumbup:

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Because you named it tablerray without the A:
int [][] tablerray=new int [rows][cols];


#7
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
lol... how idiotic of me...
thanks for the help!!!!:thumbup:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users