Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Try, catch? array help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Try, catch? array help

    import java.util.*;
    import java.util.Random; 
     
    public class arraychange{
    	//Scanner - declare it globally so method can see it
    	static Scanner input = new Scanner(System.in);
     
    	public static void main(String[] args) {
     
    		System.out.print("What size array would you like to initialize? (at least 4) ");
    		//ask for the input but make sure it is an integer
    		int size = input.nextInt();
     
    		//declare array
    		int[] array = new int[ size ];
     
     
    		Random randomNumbers = new Random();
     
    		for (int num = 0; num <array.length; num++ )  
    			array[num] = randomNumbers.nextInt(50);
     
    		System.out.printf("The values in the array are:\n");
    		for (int value = 0; value < array.length; value++ )	 \
    			System.out.printf("value[ %2d ] =%4d\n", value, array[ value ]);  
     
    		System.out.printf("Enter the index of the value you would like to change: ");  
    		int edit = input.nextInt();
    		array[ edit ] = edit;
     
    		[B]System.out.printf("The value sent is%4d", array[ edit ]);  [/B]
    		System.out.printf("\nEnter a new value (1-50) :");	
     
    		int change = input.nextInt();
    		array[ edit ] = change;
     
    		System.out.printf("The values in the array are:\n");
    		for (int value = 0; value < array.length; value++ )	
    			System.out.printf("value[ %2d ] =%4d\n", value, array[ value ]);	
    	}
    }

    Why does the value sent display the index and not the random number for that index? How would you make it do that? Also how/where would I add a try, catch statement to make sure the array size at the beginning is an int greater then 4 and not a double?


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Try, catch? array help

    Hi alkamenes,

    Just quickly popped on and noticed that noone had replyed yet, I don't have long, but hopefully I can give you a quick answer.

    Looking at you code, I'm not entirely sure I understand your question. You are using a for loop and using the value of the variable "value" to access to index of an array. This means you already have the index which the random number is at (and are printing it out). Do you mean you wish to provide a number and it finds the index which the number is at?

    e.g.
    Output:
    value[ 0 ] = 28
    value[ 1 ] = 1
    value[ 2 ] = 10
    value[ 3 ] = 15

    user enters "10" and 2 is displayed? If so, you will need to use a collection, such as an ArrayList. A collection is an object and has built in methods attached to it, such as indexOf(Object) (just cast/wrap the int as an Integer). - ArrayList (Java 2 Platform SE 5.0)

    ArrayList <Integer> myArray = new ArrayList<Integer>();

    Use the .add method to add a new entry, the ArrayList will grow dynamically as you add more enteries.


    As for validating the users input, I would read the input into a validation method which receives a String and returns an int. Inside this method you can do conversions and check that it is greater than 4. Using a try/catch statement would handle (albeit not very nicely) the user putting in anything other than an int, but not the size being less than 4, as it does not look like this will cause your code to throw an error. As your code is, you would need to either wrap the whole thing in a try block because other exceptions would be thrown further down because the variable size has not been initialized, or just around the section where you set size to the user's input and set size to a default value in catch. e.g

    int size = 4; //default value
    try{
      int size = input.nextInt();
    }catch (Exception e){}
     
    if (size < 4){
      size = 4;
    }

    (How do you put tabs in a code block??)

    Though I say it again, a validation method would be a lot nicer.


    Anyway, I hope this quick reply at least will help point you in the right direction, I'll check back once I get home and can actually sit down for more than 2 mins.

    P.S. Don't forget to start your class name with a capital!

    public class ArrayChange{
    Last edited by Tsarin; October 14th, 2011 at 11:15 PM.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Try, catch? array help

    P.P.S, To update a specified index in the array list, use .set(int index, Object element)

Similar Threads

  1. how do i try/catch this?
    By safra in forum Exceptions
    Replies: 6
    Last Post: October 29th, 2011, 11:14 AM
  2. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM
  3. try/catch..help needed soon as possible
    By safra in forum Exceptions
    Replies: 1
    Last Post: September 19th, 2010, 08:46 AM
  4. catch all
    By silverspoon34 in forum Exceptions
    Replies: 1
    Last Post: November 29th, 2009, 02:18 PM
  5. try/catch
    By rsala004 in forum Exceptions
    Replies: 5
    Last Post: July 19th, 2009, 03:20 PM