9 Replies - 4823 Views - Last Post: 20 March 2012 - 09:49 PM Rate Topic: -----

#1 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

java one dimensional array help

Posted 15 March 2012 - 06:17 AM

Use a one-dimensional array to solve the
following problem: Write an application
that inputs five numbers, each between 10
and 100, inclusive. As each number is read,
display it only if it's not a duplicate of a
number already read. Provide for the
"worst case" in which all five numbers are
different. Use the smallest possible array to
solve this problem.

Basically this is my dimensional array project
i need to take my userNumber and add it to the array?
while looping i want to check the arrays with arrays[i] not sure if i did it right?
how to properly use a boolean searchArray?


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Scanner; //import 

public class DimensionalArrays
{
    // instance variables - replace the example below with your own
    int[] userNumber = new int[5];
    
Scanner scan = new Scanner(System.in);    

your add method should do ONLY 1 thing
add a number
you can call that method many times
BUT, you do not always want to add it to the array

//add method    ** gets a number as a parameter and adds to array 
public void add(int num) { 
  int i = 0;
  while (i < 5) {
      num = num[i];
      answers[i] = num;
      
      System.out.println(answers[i]);
      i = i +1;
    }
}

//for loop to search the array **
for (int i=0; i< array.length; i++){}
then while looping over you gotta check the array
with array[i]

public static void main(String args[]){
        
 //for loop to ask person to add digit to array
 for (int index = 0; index < userNumber.length; index++){
 userNumbers[index]= i; //store number as value in array
 }

   


Is This A Good Question/Topic? 0
  • +

Replies To: java one dimensional array help

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: java one dimensional array help

Posted 15 March 2012 - 06:41 AM

Quote

while looping i want to check the arrays with arrays[i] not sure if i did it right?

Start by running your code. Do you get the output you expect?

Some logic to consider:
-You should store an index variable as an instance variable. This will track the position in the array where you added the last element. So when you go to add an element, loop from 0 to index. If the element does exists (think about how you compare an array element to the number you got from input), just return. Nothing will be added and the method will terminate. After the loop, add the element to the index+1 position, making sure you increment index afterwards.
Was This Post Helpful? 1
  • +
  • -

#3 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: java one dimensional array help

Posted 15 March 2012 - 07:18 AM

when i run the program i get public boolean search error
i can add numbers inside bracket right??
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: java one dimensional array help

Posted 15 March 2012 - 07:22 AM

Yes, you can add numbers inside of the brackets.
Was This Post Helpful? 0
  • +
  • -

#5 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: java one dimensional array help

Posted 15 March 2012 - 07:32 AM

public boolean searchArray(int userNumber);
//for loop to search the array **
for (int i=0; i< array.length; i++){
1,2,3,4,5}

display as illegal start on bluej
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: java one dimensional array help

Posted 15 March 2012 - 07:35 AM

I don't mean to be rude, but there is so much wrong with that snippet of code that I think you need to go back and do some reading on arrays and methods. We have some good tutorials on Dream.in.Code that you may find helpful.

TriggaMike's Arrays tutorial
xor-logic's Arrays tutorial
Was This Post Helpful? 3
  • +
  • -

#7 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: java one dimensional array help

Posted 15 March 2012 - 07:43 AM

okay ill take a look at them
Was This Post Helpful? 0
  • +
  • -

#8 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: java one dimensional array help

Posted 15 March 2012 - 08:38 AM

i decided to do a boolean search and told me a class is expected before system.out.println
import java.util.Scanner;
 
public class userNumber{
 
public void getNumbers() {
  Scanner input = new Scanner(System.in);
	int[] array = new int[5];
 
	int count = 0; // number of uniques read
	int entered = 0; // number of entered numbers
	int num = 0;
 
	while (entered < array.length) 
		{
		System.out.println("Enter number: ");
		num = input.nextInt();
 
		if ((num >= 10) && (num <= 100)) {
			boolean containsNumber = false;
			entered++;
 
			for (int i = 0; i < array.length; i++) 
			 {   if (array[i] == num)		
			     containsNumber = true;
			 }	
 
			if (!containsNumber) {
 
			array[count] = num;
 
			count++;
 
			} // end if
 
			else
 
			System.out.printf("%d has already been entered\n", num);
 
	     	      } // end if
 
		  else
		   System.out.println("number must be between 10 and 100");
 
		// Print the contents of the Array :
		for (int i =0;  i< entered; i++) {
 
		System.out.print(array[i] + " ");
		}
 
                                  System.out.println();
	} // end while
            } // end method getNumbers
} // end class Unique


This post has been edited by macosxnerd101: 15 March 2012 - 09:57 AM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#9 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: java one dimensional array help

Posted 20 March 2012 - 09:09 PM

Can an expert programmer check this program i added my compareto statement that suppose to compare 2 array elements. the smallest array[0] to array[1] in order to display unique arrays?

it should eliminate copies of 5 arrays number the user will input.
how can i use a loop output for these? to make the program look nicer

if(Array[0]>=10 && Array[0]<=100)
{
System.out.printf("\n%d", Array[0]);
}

if(Array[1]>=10 && Array[1]<=100)
{
if(Array[1]!=Array[0])
{
System.out.printf("\n%d", Array[1]);
}
}

if(Array[2]>=10 && Array[2]<=100)
{
if(Array[2]!=Array[0] && Array[2]!=Array[1])
{
System.out.printf("\n%d", Array[2]);
}
}

if(Array[3]>=10 && Array[3]<=100)
{
if(Array[3]!=Array[0] && Array[3]!=Array[1] && Array[3]!=Array[2])
{
System.out.printf("\n%d", Array[3]);
}
}


if(Array[4]>=10 && Array[4]<=100)
{
if(Array[4]!=Array[0] && Array[4]!=Array[1] && Array[4]!=Array[2] && Array[4]!=Array[3])
{
System.out.printf("\n%d", Array[4]);

import java.util.Scanner;

public class SingleDimArrays
{
    public static void main(String[] args)
    {
        // instance variables - replace the example below with your own
        Scanner input = new Scanner(System.in);
        int[] Array; //declare array
        Array = new int[5]; // 5 int - 0,1,2,3,4
        int[] b;
          
        System.out.print("Please input five numbers between 10 and 100?   \n");        
         
        for(int i = 0; i < 5; i++)//loop 5 times
        {
          Array[i] = input.nextInt();//input the 5 values
        }
 
        System.out.print("Output:");
        //loop to output
        // 
        //boolean statement 
        // only if statement 

        
    }
   
    //good comparable method to compare array[0] to array[1]
    public static void insertionSort(Comparable[] array){
		int Array, B;
		Comparable newValue;
		for (Array = 1; Array < array.length; Array++) {
			newValue = array[Array];
			B = Array;
			while (B > 0 && (array[B - 1].compareTo(newValue)>0)) {
				array[B] = array[B - 1];
				B--;
			}
			array[B] = newValue;

	
        if(Array[0]>=10 && Array[0]<=100)
        {
            System.out.printf("\n%d", Array[0]);
        }
         
        if(Array[1]>=10 && Array[1]<=100)
        {
            if(Array[1]!=Array[0])
            {
                System.out.printf("\n%d", Array[1]);
            }
        }
 
        if(Array[2]>=10 && Array[2]<=100)
        {
            if(Array[2]!=Array[0] && Array[2]!=Array[1])
            {
                System.out.printf("\n%d", Array[2]);
            }
        }
         
        if(Array[3]>=10 && Array[3]<=100)
        {
            if(Array[3]!=Array[0] && Array[3]!=Array[1] && Array[3]!=Array[2])
            {
                System.out.printf("\n%d", Array[3]);
            }
        }
         
         
        if(Array[4]>=10 && Array[4]<=100)
        {
            if(Array[4]!=Array[0] && Array[4]!=Array[1] && Array[4]!=Array[2] && Array[4]!=Array[3])
            {
                System.out.printf("\n%d", Array[4]);
            }
        }
    }
      
}}}
        



Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java one dimensional array help

Posted 20 March 2012 - 09:49 PM

The break and continue statement were not invented simply as subjects for PhD thesis :)

	public void getNumbers() {
		Scanner input = new Scanner(System.in);
		int[] array = new int[5];

		int entered = 0; // number of entered numbers

		while (entered < array.length) 
		{
			System.out.println("Enter number: ");
			int num = input.nextInt();

			if ((num < 10) || (num > 100)) {
				System.out.println("Number " + num + " is invalid should be betwwen 10 and 100");
				continue;
			}
 
			int i;
			for(i = 0; i < entered; ++i) {
				if(num == array[i]) {
					System.out.println("Number is a duplicate");
					break;
				}
			}
			
			if(i == entered)
				continue;
			
			array[entered++] = num;
	} // end while
} // end method getNumbers


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1