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 7 of 7

Thread: ArrayList

  1. #1
    Junior Member
    Join Date
    Sep 2022
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList

    ArrayList cannot be resolved to a type IS THE ERROR THAT I'M GETTING


    import java.util.List;
    import java.util.Scanner;

    public class Second {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int n;
    int status = 1;
    int num = 3;
    //object of the ArrayList class
    List<Integer>primelist = new ArrayList<Integer>();
    //object of the Scanner class
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the value of n: ");
    //reading the value of n from the user
    n = scanner.nextInt();

    if(n>=1 ) {
    System.out.println("First "+n+" prime number are: ");
    //2 is a known prime number
    System.out.println(2);
    //adding 2 in the ArrayList
    primelist.add(2);
    }
    for(int i=2;i<=n {
    for(int j=2;j<Math.sqrt(num);j++) {
    if(num%j==0) {
    status=0;
    break;
    }
    }
    if(status!=0) {
    System.out.println(num);
    //adding numbers to the ArrayList
    primelist.add(num);
    i++;
    }
    status=1;
    num++;
    }
    System.out.println("Alternate prime numbers are: ");
    //loop for printing the alternate prime numbers
    for(int k=0;k<primelist.size();k++) {
    if((k%2)==0)
    System.out.println(" "+primelist.get(k));
    }

    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList

    ArrayList cannot be resolved to a type
    The compiler can not find a declaration for the ArrayList class using the import statements. Check what package it is in and make sure there is an import statement for it.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2022
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList

    Quote Originally Posted by Norm View Post
    The compiler can not find a declaration for the ArrayList class using the import statements. Check what package it is in and make sure there is an import statement for it.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Thanks

  4. #4
    Junior Member
    Join Date
    Sep 2022
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList

    import java.util.List;
    import java.util.Arrays;
    import java.util.Collections;

    public class ThirdLargestInArrayExample2 {
    public static int getThirdLargest(Integer [] a,int total) {
    List<Integer> list = Arrays.asList(a);
    Collections.sort(list);
    int element=list.get(total-3);
    return element;
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Integer a[] = {1,2,5,6,3,2};
    Integer b[] = {44,66,99,77,33,22,55};
    System.out.println("ThirdLargest: "+getThirdLargest(a,6));
    System.out.println("Third Largest: "+getThirdLargest(b,7));

    }

    }
    GETTING AN ERROR ON LINE 7

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList

    GETTING AN ERROR ON LINE 7
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2022
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList

    import java.util.List;
    import java.util.Arrays;
    import java.util.Collections;
     
    public class ThirdLargestInArrayExample2 {
    	public static int getThirdLargest(Integer [] a,int total) {
    		List<Integer> list = Arrays.asList(a);
    		Collections.sort(list);
    		int element=list.get(total-3);
    		return element;
    	}
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    			Integer a[] = {1,2,5,6,3,2};
    			Integer b[] = {44,66,99,77,33,22,55};
    			System.out.println("ThirdLargest: "+getThirdLargest(a,6));
    			System.out.println("Third Largest: "+getThirdLargest(b,7));
     
    	}
     
    }


    --- Update ---

    It's working fine now

  7. #7
    Junior Member
    Join Date
    Sep 2022
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList

    import java.util.Scanner;
     
    public class Main {
    	private static void Generator (int numberofchars, String password) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("How many characters do you want in your password: Pick a number betwwen 4 and 64 ");
    		numberofchars = input.nextInt();
    		input.close();
    		if ((numberofchars >= 4) && (numberofchars <=8)) {
    			System.out.println("I suggest you have a password greater than 8 characters");
    		}
    		if ((numberofchars < 4) || (numberofchars > 64)) {
    			System.out.println("Your password must greater than 3 characters and less than 65 characters");
    		}
    	}
     
    	public static void main(String[] args) {
    		/* November 25, 2022
    		 * Generates a password between 4 and 64 characters
    		 */
    		char alpha [] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    		int num [] = {0,1,2,3,4,5,6,7,8,9};
    		char special [] = {'.','?','!',',',':',';','-','[',']','{','}','(',')','"'};
    		Generator();
     
    	}
     
    }


    --- Update ---

    GETTING ERROR ON Generator();

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM