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

Thread: Help me check my assignment if it is correct.

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me check my assignment if it is correct.

    Hi my professor wants me to make a program this this set of guidline.

    You are required to write a command line program in Java that takes in 5 integer
    numbers from a user. You must store the numbers in an array. Use an appropriate loop
    to loop through the array and check each value. If any of the numbers in the array is less than 4, print out to the screen

    “The number” [number] “is less than 4!”

    i don't know if my code is correct and have any errors, please if you see any, try to tell me what it is and how to fix it. if it is good to go, please tell me also.

    import java.util.scanner;

    public class assignmentA
    {
    	public static void main(String[] args){
     
    	int[] newArray=new int[5];
     
     
    	while(newArray < 4){
     
    		System.out.println("Please Enter 1st number: ");
     
    		Scanner scan = new Scanner(System.in);
     
    		newArray[0] = scan.nextInt();
     
    		System.out.println("Please Enter 2nd number: ");	
     
    		newArray[[1] = scan.nextInt();
     
    		System.out.println("Please Enter 3rd number: ");
     
    		newArray[2] = scan.nextInt();
     
    		System.out.println("Please Enter 4th number: ");
     
    		newArray[3] = scan.nextInt();
     
    		System.out.println("Please Enter 1st number: ");
     
    		newArray[4] = scan.nextInt();
     
    		if(newArray < 4){
     
    		System.out.println("The number "+ newArray +" is less than 4!");
    		}
     
    		}
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    Have you tried compiling this?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    i compiled it in an online compile called Codepad and the error only says

    error: 'import' does not name a type

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    Why not use an actual compiler?

    Check your capitalization and spelling. Java is case sensitive.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    ok i have checked again and the errors says.

    Main.java:3: class assignmentA is public, should be declared in a file named assignmentA.java
    public class assignmentA
    ^
    Main.java:10: operator < cannot be applied to int[],int
    while(newArray < 4){
    ^
    Main.java:34: operator < cannot be applied to int[],int
    if(newArray < 4){
    ^
    3 errors
    Last edited by mamaass; May 29th, 2012 at 07:42 AM.

  6. #6
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    Quote Originally Posted by mamaass View Post
    ok i have checked again and the errors says.

    Main.java:3: class assignmentA is public, should be declared in a file named assignmentA.java
    public class assignmentA
    ^
    Main.java:10: operator < cannot be applied to int[],int
    while(newArray < 4){
    ^
    Main.java:34: operator < cannot be applied to int[],int
    if(newArray < 4){
    ^
    3 errors
    technically, i only have 2 errors since the 1st is easy to fix. anyone knows how to fix the remaining two?

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    How are you planning on fixing the first error? Again, keep in mind that Java is case sensitive, and the standard naming conventions mean that class names should start with a capital letter.

    For your other error, the compiler is saying that it can't use the < operator on an array. That makes sense, as what would it mean for an array to be less than another array?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    How are you planning on fixing the first error? Again, keep in mind that Java is case sensitive, and the standard naming conventions mean that class names should start with a capital letter.
    yes. i will be changing that in a bit. the name of the file should be the same with the class name.

    For your other error, the compiler is saying that it can't use the < operator on an array. That makes sense, as what would it mean for an array to be less than another array?
    do you know how to fix that one? it says i need to make the the number that user inputted so as it must not be less than 4.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    Quote Originally Posted by mamaass View Post
    do you know how to fix that one? it says i need to make the the number that user inputted so as it must not be less than 4.
    It really depends on what you want to happen. You're trying to compare an entire array to a single number, which you can't do. So are you trying to compare the size of the array? A specific element? All elements? A total? Something else?

    Recommended reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    im trying to to check all the elements that the users will be inputting if it is less than 4. if it is then it must show this:

    System.out.println("The number "+ newArray +" is less than 4!");

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    Quote Originally Posted by mamaass View Post
    im trying to to check all the elements that the users will be inputting if it is less than 4. if it is then it must show this:

    System.out.println("The number "+ newArray +" is less than 4!");
    I recommend carefully going through that tutorial I linked for you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    reading it now. in this case, what would you use? while loop or if-else statement?

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    Quote Originally Posted by mamaass View Post
    reading it now. in this case, what would you use? while loop or if-else statement?
    What happened when you tried each?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    i tried the if statement.

    import java.util.scanner;
     
    public class assignmentA
    {
            public static void main(String[] args){
     
            int[] newArray=new int[5];
     
     
     
                    System.out.println("Please Enter 1st number: ");
     
                    Scanner scan = new Scanner(System.in);
     
                    newArray[0] = scan.nextInt();
     
                    System.out.println("Please Enter 2nd number: ");        
     
                    newArray[1] = scan.nextInt();
     
                    System.out.println("Please Enter 3rd number: ");
     
                    newArray[2] = scan.nextInt();
     
                    System.out.println("Please Enter 4th number: ");
     
                    newArray[3] = scan.nextInt();
     
                    System.out.println("Please Enter 1st number: ");
     
                    newArray[4] = scan.nextInt();
     
                    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
     
                    System.out.println("The number "+ newArray +" is less than 4!");
                    }
     
                    }
            }
    }

    when i compiled it, it had many errors than the while loop.

    Main.java:33: ')' expected
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: ';' expected
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: illegal start of expression
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: ';' expected
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: illegal start of expression
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: ';' expected
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: not a statement
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: ';' expected
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: not a statement
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: ';' expected
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: not a statement
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:33: ';' expected
    if(newArray[0],newArray[1],newArray[2],newArray[3],newArray[4] < 4){
    ^
    Main.java:40: class, interface, or enum expected
    }
    ^
    13 errors

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    That is not correct syntax for an if statement. Again, the basic tutorials are your best friend. The first result for a google search of "java if statements" will give you the correct syntax.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    how can i use an array in an if statements then? kinda confused when i put them together. its not state in the toturials.

  17. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    Quote Originally Posted by mamaass View Post
    how can i use an array in an if statements then? kinda confused when i put them together. its not state in the toturials.
    Start small. How do you print out the value of an index of an array? How do you use an if statement? When you get both of those things working separately, then think about combining them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #18
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    ok i changed some of the parts to

    import java.util.scanner;
     
    public class assignmentA
    {
    	public static void main(String[] args){
     
    	int[] newArray=new int[5];
     
     
     
    		System.out.println("Please Enter 1st number: ");
     
    		Scanner scan = new Scanner(System.in);
     
    		newArray[0] = scan.nextInt();
     
    		System.out.println("Please Enter 2nd number: ");	
     
    		newArray[1] = scan.nextInt();
     
    		System.out.println("Please Enter 3rd number: ");
     
    		newArray[2] = scan.nextInt();
     
    		System.out.println("Please Enter 4th number: ");
     
    		newArray[3] = scan.nextInt();
     
    		System.out.println("Please Enter 1st number: ");
     
    		newArray[4] = scan.nextInt();
     
    		if(newArray < 4){
     
    		System.out.println("The number "+ newArray +" is less than 4!");
    		}
     
     
    	}
    }

    and there is only 1 error left.

    Main.java:33: operator < cannot be applied to int[],int
    if(newArray < 4){

  19. #19
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    That's going back to your original error. It doesn't make sense to use the < operator on an entire array.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  20. #20
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    Quote Originally Posted by KevinWorkman View Post
    That's going back to your original error. It doesn't make sense to use the < operator on an entire array.
    what will i do so that it will check the element that the user's input and make sure it's not less than 4?

  21. #21
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me check my assignment if it is correct.

    Quote Originally Posted by mamaass View Post
    what will i do so that it will check the element that the user's input and make sure it's not less than 4?
    I'm not going to do your homework for you. Recommended reading: http://www.javaprogrammingforums.com...e-posting.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  22. #22
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    i made some changes on this one and has no errors on it.

    import java.util.Scanner;
     
    public class assignmentA
    {
    	public static void main(String[] args){
     
    	int[] newArray;
    	newArray= new int[5];
     
     
    		System.out.println("Please Enter 1st number: ");
     
    		Scanner scan = new Scanner(System.in);
     
    		newArray[0] = scan.nextInt();
     
    		System.out.println("Please Enter 2nd number: ");	
     
    		newArray[1] = scan.nextInt();
     
    		System.out.println("Please Enter 3rd number: ");
     
    		newArray[2] = scan.nextInt();
     
    		System.out.println("Please Enter 4th number: ");
     
    		newArray[3] = scan.nextInt();
     
    		System.out.println("Please Enter 5th number: ");
     
    		newArray[4] = scan.nextInt();
     
    		for (int num : newArray) {
     
    		if (num < 4) 
     
    			System.out.println("The number "+ num +" is less than 4!");
    		}
     
     
    	}
    }

  23. #23
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me check my assignment if it is correct.

    u lock
    import java.util.Scanner;

Similar Threads

  1. Can anybody correct the code?
    By ur2cdanger in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2011, 07:07 PM
  2. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  3. Replies: 2
    Last Post: August 2nd, 2011, 08:11 AM
  4. Is My answers correct??
    By Java.Coder() in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 28th, 2010, 06:22 AM
  5. Correct Coupling
    By poulenc in forum Java Theory & Questions
    Replies: 0
    Last Post: December 26th, 2010, 04:28 AM