array

  • 13 years ago

    Hi everyone,  
    I am new to java.  
    I have a question?  
    How can I store in to an array the values that the user enter, so later I can compare the two sets of array that the user enter.

    Ok, the user needs to enter two sets, then main will call another class named IntergerSet where both sets are going to be compered.
    Therefore I need to store the sets of values in two different sets.
     
    This is what I have till know:
     
    import java.util.Scanner;
    public class UniversalSet  
    {
        public static void main(String[] args)  
        {
              Scanner input = new Scanner (System.in);
     
              System.out.println("Welcome to the Universal Set\n"); 
       
              System.out.print("This program is going to find the union "  
                       + "or intersection of any two sets you enter"  
                       + "\nPlease enter how many values on set 1: ");
              int value1 = input.nextInt();
         
              System.out.print("Now please enter the first set: "); 
             
    for(int i=0; i<=value1 ;i++) 
              {
    //error msg: cannot find symbol method parseInt(int)->  
                    int userSetArray1[] = Integer.parseInt(userSetArray1[i]); 
              }
         
            System.out.print("Please how many values on set 2: ");
                    int value2 = input.nextInt();
         
            System.out.print("Now enter the second set: ");
            for(int i=0; i<=value2 ;i++) 
            {
    //error msg: cannot find symbol method parseInt(int)->
                   int userSetArray2[] = Integer.parseInt(userSetArray2[i]);
            }
         
    //error msg: cannot find symbol variable userSetArray1 and userSetArray1->    
            IntergerSet elementSet = new IntergerSet(userSetArray1, userSetArray2);
        }
    }


    Thanks
  • 13 years ago

    try

              System.out.print("Now please enter the first set: "); 

    int[] userSetArray1=new int[value1];
              for(int i=0; i<value1 ;i++) 
              {   
                    userSetArray1[i] = input.nextInt();

      }

    and pay attention to the < use in the loop and not <= that would generate a bounds exception. since if the user entered 5 it would actually try to make 6 loops, java is 0 based.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous