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

Thread: does not mesh with two-eimensional array

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default does not mesh with two-eimensional array

    import java.util.*;
    import javax.swing.*;
    public class Schedule
    {
    	public static void main(String[] args)
    	{
     
     
    		String[] classSchedule =  {"CIS 110", "ENG 111", "MAT 070","WEB 110"}; 
    		String myClass;
    		int x;
    		boolean classExists = false;
    		myClass = JOptionPane.showInputDialog(null, "Enter a class");
    		for(x = 0; x < classSchedule.length; ++x)
    			if(myClass.equals(classSchedule[x]))
    				classExists = true;
    			if(classExists)
    				JOptionPane.showMessageDialog(null, myClass +
    					" exists");
    			else
    				JOptionPane.showMessageDialog(null, myClass +
    					" does not exist");
     
    }
    }


    basically I take this code here, try to implement a second dimension in the array, and suddenly none of the classes esist
    Last edited by helloworld922; December 7th, 2010 at 08:38 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    Wait....I see now.

    Your for loop only deals with the if statement after it. It doesn't bother with anything else.

    You need brackets around the parts in the for loop.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    Also, if that's not what it is, try changing the
    ++x to an x++ to see if that works.

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: does not mesh with two-eimensional array

    I tried this, and it tells me it doesnt exist. and it tells me 8 times
    import java.util.*;
    import javax.swing.*;
    public class test5
    {
    	public static void main(String[] args)
    	{
     
     
    		String[][] classSchedule = { {"CIS 110 ", "ENG 111 ", "MAT 070 ","WEB 110 "},
    				             {"Wen 4:40 ", "Tue 5:40 ", "Mon 1:25 ", "Sat 1:00 "}}; 
    		String myClass;
    		int x;
    		int y;
    		boolean classExists = false;
    		myClass = JOptionPane.showInputDialog(null, "Enter a class");
    		for(x = 0; x < classSchedule.length; ++x)
    		{
    			for(y = 0; y < classSchedule[x].length; ++y)
    			{
    			if(myClass.equals(classSchedule[x]))
    				classExists = true;
    			if(classExists)
    				JOptionPane.showMessageDialog(null, myClass +
    					" exists");
    			else
    				JOptionPane.showMessageDialog(null, myClass +
    					" does not exist");
    				}
    		}
    }
    }
    Last edited by helloworld922; December 7th, 2010 at 08:38 PM.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    Looks like it's working to me.

    What's the problem?

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    String[][] classSchedule = { {"CIS 110 ", "ENG 111 ", "MAT 070 ","WEB 110 "},
    {"Wen 4:40 ", "Tue 5:40 ", "Mon 1:25 ", "Sat 1:00 "}};

    What happens if you change the above to

    String[][] classSchedule = { {"CIS 110 ", "ENG 111 ", "MAT 070 ","WEB 110 ",
    "Wen 4:40 ", "Tue 5:40 ", "Mon 1:25 ", "Sat 1:00 "}};

  7. #7
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: does not mesh with two-eimensional array

    when I run the first code I have posted, it tells me if the class exists or not.

    when i run the second one, no matter what I have typed, it says it doesnt exist and repeats itself 8 times

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    First of all, it'd be

    if (myClass.equals(classSchedule[x][y])
    as it's 2D

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    I still am baffled as to why it's not working, but I've gotten it to only show it once.
    import java.util.*;
    import javax.swing.*;
    public class Schedule
    {
    public static void main(String[] args)
    {
     
     
    String[][] classSchedule =  {{"CIS 110 ", "ENG 111 ", "MAT 070 ","WEB 110 "},
    {"Wen 4:40 ", "Tue 5:40 ", "Mon 1:25 ", "Sat 1:00 "}};
    String myClass = "";
    int x;
    int y;
    boolean classExists = false;
    myClass = JOptionPane.showInputDialog(null, "Enter a class");
    for(x = 0; x < classSchedule.length; ++x)
    {
    for(y = 0; y < classSchedule[x].length; ++y)
     
    if(myClass.equals(classSchedule[x][y]))
    classExists = true;
     
    }
    if(classExists)
    JOptionPane.showMessageDialog(null, myClass +
    " exists");
    else
    JOptionPane.showMessageDialog(null, myClass +
    " does not exist");
    }
    }

  10. #10
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: does not mesh with two-eimensional array

    ive found out im supposed to change the format of the array, it needs to have 4 rows and 2 columns. that doesnt change a lot though

  11. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: does not mesh with two-eimensional array

    package sorting;
     
    import java.util.*;
    import javax.swing.*;
    public class Schedule
    {
    public static void main(String[] args)
    {
     
     
    String[][] classSchedule =  new String[][]{{"CIS 110", "ENG 111", "MAT 070","WEB 110"},
    {"Wen 4:40", "Tue 5:40", "Mon 1:25", "Sat 1:00"}};
     
    for (int v =0; v < classSchedule.length; v++)
    {
    	for (int w =0; w < classSchedule[v].length; w++)
    		System.out.println(classSchedule[v][w]);
    }
    String myClass = "";
    int x;
    int y;
    boolean classExists = false;
    myClass = JOptionPane.showInputDialog(null, "Enter a class");
    for(x = 0; x < classSchedule.length; ++x)
    {
    for(y = 0; y < classSchedule[x].length; ++y)
    {
    if(myClass.equals(classSchedule[x][y]))
    {classExists = true;
     
     
    }
     
    }
    }
    System.out.println(classExists);
    if(classExists==true)
    JOptionPane.showMessageDialog(null, myClass +
    " exists");
    else
    JOptionPane.showMessageDialog(null, myClass +
    " does not exist");
    }
    }

    I kinda thought it might be the spaces.

  12. #12
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: does not mesh with two-eimensional array

    I tried that code as is and got this back

    C:\Documents and Settings\Admin\Desktop>java Schedule3
    Exception in thread "main" java.lang.NoClassDefFoundError: Schedule3 (wrong name
    : sorting/Schedule3)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :621)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:12
    4)
    at java.net.URLClassLoader.defineClass(URLClassLoader .java:260)
    at java.net.URLClassLoader.access$000(URLClassLoader. java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 52)
    at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:320)
    Could not find the main class: Schedule3. Program will exit.

  13. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    Get rid of my package class and it'll work.

    remove "package sorting;"

  14. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    Also, if you're calling it tester, it won't recognize it as I've called it Schedule

  15. #15
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: does not mesh with two-eimensional array

    are you on aim? ive added you because I have further questions

  16. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    I have AIM, but am not on a computer that I could use it at the moment.

    Also, I've currently got it blocked so that only those on my buddy list can IM me. I'll change it later when I get back to my regular computer. I'm using a lab computer right now.

  17. #17
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: does not mesh with two-eimensional array

    well in that case, I need to modify the program to allow the user to search for a class in the array, and display the time related to that class

  18. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    Wouldn't it be better to have two parallel 1D arrays than to have a 2D array?

  19. #19
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: does not mesh with two-eimensional array

    Could somebody help me with my program? It's almost done and I'm stuck and am spending all day when I could be getting it over with, if I weren't waiting patiently all day for help.

    Please help. I have several other things I need to get done too in other classes.

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 12:54 PM
  3. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM
  4. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  5. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM