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: catching Set

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default catching Set

    I just started today with Java. The thing is as follows:

    From class Notes in my class I have this properties and methods

    import java.util.*;
     
    public class Notes {
     
    	//properties
    	ArrayList<String[]> notes;
     
    	//constructor
    	public Notes() {
    		// TODO Auto-generated constructor stub
    		notes = new ArrayList<String[]>();
    		//notes = new HashSet<String>();
    	}
     
    	//Adds a new note
    	public void set(String title, String body, String location) {
    		String note[] = {title, body, location};
    		notes.add(note);
    	}
     
    	//Retrieves all notes
    	public ArrayList get() {
    		return notes;
    	}
     
    	//Deletes a note
    	public boolean del() {
    		return true;
    	}

    I am trying to catch the message sent from Notes in line 11 from the Main class

    import java.io.*;
    import java.util.*;
     
    public class Main {
     
    	public static void main(String[] args) {
     
    		Notes notes = new Notes();
    		notes.set("User 1", "This is a voicemail message for Jonathan", "World wide web");
                    //line 11 where I'm trying to catch the message sent from Notes
    		ArrayList<String> allNotes = notes.get();
     
    		for (String i: allNotes) {
    			System.out.println(notes.get());
    		}
    	}
     
    }

    And the code doesn't show any syntax error so far. When I run the code the console throws the next

    Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.String; cannot be cast to class java.lang.String ([Ljava.lang.String; and java.lang.String are in module java.base of loader 'bootstrap')
    	at Main.main(Main.java:13)

    What I am doing wrong? any help to learn to catch the Set and print it out please?

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

    Default Re: catching Set

    notes in the Notes class holds String arrays.
    The for each statement is expecting to get String objects not an array of Strings.

    Declare allNotes to be a list of String[] vs String
    Change the for each loop to extract each element as String[] vs String
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: catching Set

    Quote Originally Posted by Norm View Post
    notes in the Notes class holds String arrays.
    The for each statement is expecting to get String objects not an array of Strings.

    Declare allNotes to be a list of String[] vs String
    Change the for each loop to extract each element as String[] vs String
    Many thanks for your help. I could fix it up thanks to you

Similar Threads

  1. Cant seem to get my loop to work after catching an exception.
    By lonnieg1214 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: July 4th, 2014, 11:20 AM
  2. Replies: 2
    Last Post: June 20th, 2013, 10:02 PM
  3. Not catching socketexceptions in Vista and Windows 7 only in XP
    By housemansson in forum Java Networking
    Replies: 2
    Last Post: August 23rd, 2011, 11:43 AM
  4. [SOLVED] Problems with Try/Catch Catching Wrong Exception
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 10th, 2011, 08:08 PM
  5. Quick question with throwing and catching an exception.
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 12th, 2011, 10:24 PM

Tags for this Thread