what do I use for using the JOptionPane.(what do I put here) for it to show up without the input box?
24 Replies - 810 Views - Last Post: 28 March 2010 - 10:57 AM
#17
Re: help with this program
Posted 28 March 2010 - 09:46 AM
use MessegeDialog
JOptionPane.showMessageDialog(null, "Hi");
#18
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?
#19
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:
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());
#20
Re: help with this program
Posted 28 March 2010 - 10:01 AM
japanir, 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:
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
#21
Re: help with this program
Posted 28 March 2010 - 10:10 AM
you can format it inside the StringBuffer:
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:
then in the Runnit method, whenever a number was inputted by the number do so:
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;
}
#22
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
#23
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.
the code for the method is in my prev. post...
also, for next time, post the exact error messeges you get.
#24
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:
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)
#25
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;
}
}
|
|

New Topic/Question
Reply





MultiQuote


|