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

Thread: Array Problem

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array Problem

    Hello everyone. I'm learning java and i'm really new at this so i know this is probably too simple for you but if you can show my mistake i will be grateful. I made 2 classes. One is the main and the other creates an int array. Everything seems fine when i first start the program but when i try to add a value to the first index of array "if" becomes false and skip to the "else" part so i get "Out of limit!" error that i wrote. Also the show list method is wrong too i guess but i can't find my mistake. Here's my code.


    public class MaximumBulma {
     
    	int[] List;
    	int i;
    	int x=0;
    	int Limit;
     
    	public MaximumBulma(int Capacity){
     
    		List = new int[Capacity];
    		i=0;
    		Capacity=Limit;
     
    	}
     
    	public void AddToList(int eklenen) {
     
    		if (i + 1< Limit){
    		List[i]=eklenen;
    		i++;
    		}
    		else{
    			System.err.println("Out of limit!");
    	}
    	}
     
    	public void ShowList(){
     
     
    		while (x+1<Limit){
    			System.out.println(List[x]);
    			x++;
     
    		}
     
    	}
     
    }


    and the main



    import java.util.Scanner;
    public class Main {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner(System.in);
     
    		System.out.println("Capacity?");
    		int GirilenLimit=scan.nextInt();
    		MaximumBulma Max = new MaximumBulma(GirilenLimit);
     
     
    		System.out.println("Value to add?");
    	int GirilenDeger=scan.nextInt();
     
    	Max.AddToList(GirilenDeger);
    	Max.ShowList();
     
     
     
     
     
    	}
     
    }

    thank you for your help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Problem

    Try debugging the code by adding some println() statements that print out the values of the variables used and tested in the if statements so you can see what the computer sees when the code executes. The printout will help you understand what is happening.
    For example print out the values of i and Limit in the addToList() method to see why the if is false.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    You are right. I did what you offered and saw that my array capacity is 0. But when i look to here for the problem i still can't see the wrong part :/


    		System.out.println("Capacity?");
    		int GirilenLimit=scan.nextInt();
    		MaximumBulma Max = new MaximumBulma(GirilenLimit<-------------);

    	int[] List;
    	int i;
    	int x=0;
    	int Limit; <--------------
     
    	public MaximumBulma(int Capacity<----------){
     
    		List = new int[Capacity];
    		i=0;
    		Capacity=Limit; <------------------
    		System.out.println("Limit=" + Limit);
     
    	}


    somehow the array i try to create doesn't accept the value i scanned as the limit and goes with the 0 which is the value when i declared it first.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Problem

    Look at how to write an assignment statement.
    The value to be assigned is to the right of the =
    The variable to be given the value is to the left of the =

    BTW Naming conventions for variables states that variable names and method names should start with lowercase letters.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    Right... As you said the problem is Capacity=Limit. Now i changed it to Limit=Capacity and works perfectly thank you. And sorry for the method names as i said i'm really new at this. From now on i'll start with lowercase letters By the way sorry for the language problems too. As you can guess, english is not my native language and with so many new terms it's quite hard to explain myself. Thank you again

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Problem

    No problem. I'm very used to English as a second language questions. I was lucky enough to be born where English is the first language.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Stark (May 14th, 2014)

Similar Threads

  1. Another Array Problem
    By hkfrenchtoast in forum What's Wrong With My Code?
    Replies: 11
    Last Post: March 15th, 2014, 08:19 PM
  2. Replies: 2
    Last Post: February 24th, 2014, 10:48 PM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Problem with array
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 27th, 2010, 12:17 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM