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[9]; // array declaration
int input; // variable to hold data entered by the user
System.out.println("Please enter 10 numbers, repeats will not be accepted.");
for (int counter = 0; counter < array.length; counter++)
24 Replies - 804 Views - Last Post: 28 March 2010 - 10:57 AM
#1 Guest_Kyle*
help with this program
Posted 28 March 2010 - 06:07 AM
this program is supposed to use an array to take 10 digits from the user, but not take any duplicates, and then display the 10 numbers on the screen. I am stuck, here is my code so far, please help
Replies To: help with this program
#2 Guest_Kyle*
Re: help with this program
Posted 28 March 2010 - 06:28 AM
changed it a little, but still lost, please help
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[9]; // array declaration
int total = 0; // holds the total amount of numbers entered
JOptionPane.showInputDialog ("Please enter 10 numbers, repeats will not be accepted.");
for (int counter = 0; counter < digits.length; counter++)
total += digits[counter];
}// end run it
}// end numbers
#3
Re: help with this program
Posted 28 March 2010 - 06:29 AM
well this is not much you have written... make some effort!
here are couple of things to think of:
1. use a Scanner Object to get user's input
2. have a method that check if an inputted number is already exists in the array.
here are couple of things to think of:
1. use a Scanner Object to get user's input
2. have a method that check if an inputted number is already exists in the array.
#4 Guest_Kyle*
Re: help with this program
Posted 28 March 2010 - 06:42 AM
can't figure out the scanner with the array
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[9]; // array declaration
int total = 0; // holds the total amount of numbers entered
Scanner input = new Scanner(System.in); // scanner to take users input
JOptionPane.showInputDialog ("Please enter 10 numbers, repeats will not be accepted.");
digits = input.nextInt();
for (int counter = 0; counter < digits.length; counter++)
total += digits[counter];
#5 Guest_Kyle*
Re: help with this program
Posted 28 March 2010 - 06:50 AM
added a 1 to the array and now it compiles, but only takes one number, i want to get it to take all 10 numbers before I worry about the checking phase
#6
Re: help with this program
Posted 28 March 2010 - 06:57 AM
do you have to get 10 different numbers? or do you have to scan 10 numbers and print only unique numbers within the array?
that is different.
as for getting the input, get the numbers within the for loop, so you will get 10 numbers.
also, digits is an array of int values, not a single int value.
so this is wrong:
you will have to assign values to a certian cell of the array, as:
digits[0] = //...
digits[1] = //...
or better inside the loop,
digits[counter] = //...
that is different.
as for getting the input, get the numbers within the for loop, so you will get 10 numbers.
also, digits is an array of int values, not a single int value.
so this is wrong:
digits = input.nextInt();
you will have to assign values to a certian cell of the array, as:
digits[0] = //...
digits[1] = //...
or better inside the loop,
digits[counter] = //...
for (int counter = 0; counter < digits.length; counter++)
digits[counter] = input.nextInt();
total += digits[counter];
#7 Guest_Kyle*
Re: help with this program
Posted 28 March 2010 - 07:06 AM
japanir, on 28 March 2010 - 05:57 AM, said:
do you have to get 10 different numbers? or do you have to scan 10 numbers and print only unique numbers within the array?
that is different.
as for getting the input, get the numbers within the for loop, so you will get 10 numbers.
also, digits is an array of int values, not a single int value.
so this is wrong:
you will have to assign values to a certian cell of the array, as:
digits[0] = //...
digits[1] = //...
or better inside the loop,
digits[counter] = //...
that is different.
as for getting the input, get the numbers within the for loop, so you will get 10 numbers.
also, digits is an array of int values, not a single int value.
so this is wrong:
digits = input.nextInt();
you will have to assign values to a certian cell of the array, as:
digits[0] = //...
digits[1] = //...
or better inside the loop,
digits[counter] = //...
for (int counter = 0; counter < digits.length; counter++)
digits[counter] = input.nextInt();
total += digits[counter];
I have to check for each number entered, and out of the 10 numbers none can be duplicates
and I tried adding that part of the code, but it threw an error here:
total += digits[counter];
#8 Guest_Kyle*
Re: help with this program
Posted 28 March 2010 - 07:11 AM
fixed it, but the asking for a number only shows up once and then it quits well the program stays running ???
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[9]; // array declaration
int total = 0; // holds the total amount of numbers entered
Scanner input = new Scanner(System.in); // scanner to take users input
JOptionPane.showInputDialog ("Please enter 10 numbers, repeats will not be accepted.");
for (int counter = 0; counter < digits.length; counter++)
{digits[counter] = input.nextInt();
total += digits[counter];
}
}// end run it
}// end numbers
#9
Re: help with this program
Posted 28 March 2010 - 07:16 AM
well the "Asking for numbers" should also be inserted to the for loop block.
but just a thought, why are you using swing's JOptionPane and a Scanner Object for an input?
you can do all the coding just in the console, or just in swing.
anyways, here is how the code should be updated,:
hint for the part of non duplicate numbers:
for every new inputted number, go over the already inputted numbers in the array.
if the number is not already in, add it. else, post an error messege and ask for a new input.
but just a thought, why are you using swing's JOptionPane and a Scanner Object for an input?
you can do all the coding just in the console, or just in swing.
anyways, here is how the code should be updated,:
for (int counter = 0; counter < digits.length; counter++)
JOptionPane.showInputDialog ("Please enter 10 numbers, repeats will not be accepted.");
{digits[counter] = input.nextInt();
total += digits[counter];
}
hint for the part of non duplicate numbers:
for every new inputted number, go over the already inputted numbers in the array.
if the number is not already in, add it. else, post an error messege and ask for a new input.
#10 Guest_Kyle*
Re: help with this program
Posted 28 March 2010 - 07:23 AM
fixed that, but I can still only get it to ask twice now, and how do I get it to display the numbers?
would it be:
System.out.println(digits[1])
System.out.println(digits[2]) ... and so on???
would it be:
System.out.println(digits[1])
System.out.println(digits[2]) ... and so on???
#11 Guest_Kyle*
Re: help with this program
Posted 28 March 2010 - 07:52 AM
#12
Re: help with this program
Posted 28 March 2010 - 08:13 AM
well the JOptionPane part should be inside the for loop:
for (int counter = 0; counter < digits.length; counter++){
JOptionPane.showInputDialog ("Please enter 10 numbers, repeats will not be accepted.");
digits[counter] = input.nextInt();
total += digits[counter];
}
#13 Guest_Kyle*
Re: help with this program
Posted 28 March 2010 - 08:44 AM
the joption part is in the loop already, here is what I have, it only asks twice, and won't display anything
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[9]; // array declaration
int total = 0; // holds the total amount of numbers entered
Scanner input = new Scanner(System.in); // scanner to take users input
JOptionPane.showInputDialog ("Please enter 10 numbers, repeats will not be accepted.");
for (int counter = 0; counter < digits.length; counter++){
JOptionPane.showInputDialog ("Please enter 10 numbers, repeats will not be accepted.");
digits[counter] = input.nextInt();
total += digits[counter];
}
System.out.println(digits[1]);
}// end run it
}// end numbers
#14
Re: help with this program
Posted 28 March 2010 - 08:58 AM
anyone? I still need help with this.... it is frustrating me so much lol
#15
Re: help with this program
Posted 28 March 2010 - 09:26 AM
if you declare an array of 10 int values declare it as:
int[] digits = new int[10];
array's indexes are numbers between 0 - length - 1.
in your case 0 - 9 (which is 10 values).
declaring it as you did (int[9] will create an array of 9 int values (indexes of 0 - 8).
again, if you use swing's JoptionPane, why also get input from the console?
first decide what you prefer. Console, or JOptionPane.
in case of using the console do it as:
or else, if you prefer using JOptionPane, do it as follows:
notice that when using a JOptionPane, no Scanner Object is needed. you get the input through the JOptionPane.
also note that you have to parse the returned value of the JOptionPane, as it returns a String Object, and you want int values.
make sure that when inputting numbers you input ONLY int values, or else you will get an exception.
int[] digits = new int[10];
array's indexes are numbers between 0 - length - 1.
in your case 0 - 9 (which is 10 values).
declaring it as you did (int[9] will create an array of 9 int values (indexes of 0 - 8).
again, if you use swing's JoptionPane, why also get input from the console?
first decide what you prefer. Console, or JOptionPane.
in case of using the console do it as:
private void Runit()
{
int[] digits = new int[10]; // array declaration
int total = 0; // holds the total amount of numbers entered
Scanner input = new Scanner(System.in); // scanner to take users input
System.out.println ("Please enter 10 numbers, repeats will not be accepted.");
for (int counter = 0; counter < digits.length; counter++){
System.out.println("enter a number");
digits[counter] = input.nextInt();
total += digits[counter];
}
System.out.println(digits[1]);
}// end run it
}// end numbers
or else, if you prefer using JOptionPane, do it as follows:
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++){
digits[counter] = Integer.parseInt(JOptionPane.showInputDialog ("Please enter a number."));
total += digits[counter];
}
System.out.println(digits[1]);
}// end run it
}// end numbers
notice that when using a JOptionPane, no Scanner Object is needed. you get the input through the JOptionPane.
also note that you have to parse the returned value of the JOptionPane, as it returns a String Object, and you want int values.
make sure that when inputting numbers you input ONLY int values, or else you will get an exception.
|
|

New Topic/Question
Reply
MultiQuote







|