help with this program

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1124 Views - Last Post: 28 March 2010 - 10:57 AM Rate Topic: -----

#16 kburton89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

Re: help with this program

Posted 28 March 2010 - 09:38 AM

what do I use for using the JOptionPane.(what do I put here) for it to show up without the input box?
Was This Post Helpful? 0
  • +
  • -

#17 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: help with this program

Posted 28 March 2010 - 09:46 AM

use MessegeDialog
JOptionPane.showMessageDialog(null, "Hi");

Was This Post Helpful? 1
  • +
  • -

#18 kburton89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

Re: help with this program

Posted 28 March 2010 - 09:51 AM

JOptionPane.showMessageDialog(null,digits[0]); 


can I change that code some how to show all of the contents of the digit array like digits[1], digits[2] ... ect?
Was This Post Helpful? 0
  • +
  • -

#19 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: help with this program

Posted 28 March 2010 - 09:56 AM

yeah.
just change the String "hi" in my example, to hold all the values you want. better use a StringBuffer Object:

StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < digits.length; i++){
sb.append(digits[i]));
}

JOptionPane.showMessageDialog(null,sb.toString()); 

Was This Post Helpful? 1
  • +
  • -

#20 kburton89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

Re: help with this program

Posted 28 March 2010 - 10:01 AM

View Postjapanir, on 28 March 2010 - 08:56 AM, said:

yeah.
just change the String "hi" in my example, to hold all the values you want. better use a StringBuffer Object:

StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < digits.length; i++){
sb.append(digits[i]));
}

JOptionPane.showMessageDialog(null,sb.toString()); 


wow, that works well, is there anyway to add a comma between each of the numbers or seperate them a little so it doesn't look like one long number?

and I am a little confused as where to start with the part to check for duplicate numbers, I know I would add it in the loop somewhere, but how do I get it to check through all of the previous numbers?

thanks again

Kyle
Was This Post Helpful? 0
  • +
  • -

#21 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: help with this program

Posted 28 March 2010 - 10:10 AM

you can format it inside the StringBuffer:
StringBuffer sb = new StringBuffer(); 
for(int i = 0 ; i < digits.length; i++){ 
sb.append(digits[i]));
sb.append(" ");//add space
} 
 
JOptionPane.showMessageDialog(null,sb.toString()); 


as for checking duplicates, it depends. you can use a method to do it. a boolean method, returns true if a number is inside an array, or false if not:

private boolean isInArray(int[] arr, int num){
     for(int i = 0 ; i < arr.length; i++){
           //if the num is found in the array, return true
           if(arr[i] == num){
                return true;
           }
     }
     //the number num is not in the array, return false
     return false;
}


then in the Runnit method, whenever a number was inputted by the number do so:
for (int counter = 0; counter < digits.length; counter++){ 
        int tempNum = Integer.parseInt(JOptionPane.showInputDialog ("Please enter a number."));
        //while the number is in the array, ask for a new number
        while(isInArray(digits, tempNum){
                int tempNum = Integer.parseInt(JOptionPane.showInputDialog ("The number is alreaqdy in the array, please try again."));
         }
        //the method isInArray returned false, the number is not in the array, add it to the array:
        digits[counter] = tempNumber;
}

Was This Post Helpful? 0
  • +
  • -

#22 kburton89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

Re: help with this program

Posted 28 March 2010 - 10:22 AM

thought I had it right, but still have errors??? whats wrong?


import javax.swing.JOptionPane;
import java.util.*;

public class numbers
{

numbers()
{}

public static void main(String[]args)
{
numbers assign = new numbers();
assign.Runit();
} // end main

private void Runit() 
{ 
        int[] digits = new int[10]; // array declaration 
        int total = 0; // holds the total amount of numbers entered 
        
        for (int counter = 0; counter < digits.length; counter++){ 
            int tempNum = Integer.parseInt(JOptionPane.showInputDialog ("Please enter a number."));
            //while the number is in the array, ask for a new number
            while(isInArray(digits, tempNum){
                    int tempNum = Integer.parseInt(JOptionPane.showInputDialog ("The number is alreaqdy in the array, please try again."));
             }
            //the method isInArray returned false, the number is not in the array, add it to the array:
            digits[counter] = tempNumber;
    }
         
        StringBuffer sb = new StringBuffer(); 
        for(int i = 0 ; i < digits.length; i++){ 
        sb.append(digits[i]);
        sb.append(" ");//add space
        } 
         
        JOptionPane.showMessageDialog(null,sb.toString()); 


                 
}// end run it 
}// end  numbers

Was This Post Helpful? 0
  • +
  • -

#23 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: help with this program

Posted 28 March 2010 - 10:35 AM

well you call isInArray method, but it does not exist inside your class.
the code for the method is in my prev. post...

also, for next time, post the exact error messeges you get.
Was This Post Helpful? 0
  • +
  • -

#24 kburton89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

Re: help with this program

Posted 28 March 2010 - 10:48 AM

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method isInArray(int[], int) is undefined for the type numbers
Syntax error, insert ") Statement" to complete WhileStatement
Duplicate local variable tempNum
Illegal modifier for the variable isInArray; only final is permitted
Syntax error on token "(", ; expected
Syntax error on token ",", ; expected
Syntax error on token ")", ; expected
Void methods cannot return a value
Void methods cannot return a value
tempNumber cannot be resolved

at numbers.Runit(numbers.java:26)
at numbers.main(numbers.java:13)

these are the errors I am still getting after adding that code. maybe I added it in the wrong place?

here is my code:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	The method isInArray(int[], int) is undefined for the type numbers
	Syntax error, insert ") Statement" to complete WhileStatement
	Duplicate local variable tempNum
	Illegal modifier for the variable isInArray; only final is permitted
	Syntax error on token "(", ; expected
	Syntax error on token ",", ; expected
	Syntax error on token ")", ; expected
	Void methods cannot return a value
	Void methods cannot return a value
	tempNumber cannot be resolved

	at numbers.Runit(numbers.java:26)
	at numbers.main(numbers.java:13)

Was This Post Helpful? 0
  • +
  • -

#25 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: help with this program

Posted 28 March 2010 - 10:57 AM

this is the full code.. it should work
import javax.swing.JOptionPane; 
import java.util.*; 
 
public class numbers 
{ 
 
numbers() 
{} 
 
public static void main(String[]args) 
{ 
numbers assign = new numbers(); 
assign.Runit(); 
} // end main 


private void Runit(){
int[] digits = new int[10]; // array declaration  
        int total = 0; // holds the total amount of numbers entered  
         
        for (int counter = 0; counter < digits.length; counter++){  
            int tempNum = Integer.parseInt(JOptionPane.showInputDialog ("Please enter a number.")); 
            //while the number is in the array, ask for a new number 
            while(isInArray(digits, tempNum)){ 
                    tempNum = Integer.parseInt(JOptionPane.showInputDialog ("The number is alreaqdy in the array, please try again.")); 
             } 
            //the method isInArray returned false, the number is not in the array, add it to the array: 
            digits[counter] = tempNum; 
    } 
          
        StringBuffer sb = new StringBuffer();  
        for(int i = 0 ; i < digits.length; i++){  
        sb.append(digits[i]); 
        sb.append(" ");//add space 
        }  
          
        JOptionPane.showMessageDialog(null,sb.toString()); 
}


private boolean isInArray(int[] arr, int num){ 
     for(int i = 0 ; i < arr.length; i++){ 
           //if the num is found in the array, return true 
           if(arr[i] == num){ 
                return true; 
           } 
     } 
     //the number num is not in the array, return false 
     return false; 
}
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2