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

Thread: Problem with the code

  1. #1
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with the code

    Hello

    I have this code:

    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.util.Scanner;
    import java.util.ArrayList;
     
     
    class Specification {
     
    	String name;
    	String IP;
    	String OS;
    	int RAM;
    }
     
    ...
     
    //adding information
    	public void AddInfo(String n, String f, String o, int r){		   			  
     			  setName(n);
     			  setIP(f);
     			  setOS(o);
     			  setRAM(r); 
     			  System.out.println("Record done!");
    	}
     
    ...
     
    	public static void main(String[] args) {		
    		int i;
    		Scanner scan=new Scanner(System.in);
    		ArrayList<Specification> a = new ArrayList<Specification>();
    		a.add(new Specification());
     
     
    		while(true){
     
     
    		   	System.out.println("Choose your option");
    		   	System.out.println("1. Add information");
    		   	System.out.println("2. Print the entered information");
    		   	System.out.println("3. Write to file");
    		   	System.out.println("4. Search for an item");
    		   	System.out.println("Enter a number dofferent from those from above (ex.5)");		   	
    		   	int choice1=scan.nextInt();
    		   	if(choice1>4)break;
     
    		   		switch (choice1) {
     
    		   		case 1: {
    		   			  System.out.println("How many records do you want to make?");
    		   			  int enterings;
    		   			  enterings=scan.nextInt();
    		   			  for(i=0;i<enterings;i++)
    		   			  { System.out.println("Enter name");
    		   			    String entered_name=scan.next();
    		   			    System.out.println("Enter IP");
    		   			    String entered_IP=scan.next();
    		   			    System.out.println("Enter OS");
    		   			    String entered_OS=scan.next();
    		   			    System.out.println("Enter RAM");
    		   			    int entered_RAM=scan.nextInt();
    		   			    a.get(i).AddInfo(entered_name, entered_IP, entered_OS, entered_RAM);		   			    
    		   			  }	   			      
    		   		} break;
     
    ...
     
            }
        }
    }
    When I am running the program and i am making records, i have no problem entering information about the object with index 0. When i enter information about object with index 1 i get this error:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Specification.main(Specification.java:178)

    My aim is to make several records to the ArrayList
    Last edited by copeg; August 9th, 2010 at 09:01 AM. Reason: Please use the code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with the code

    Seems your code only adds a single object to the ArrayList, which is placed at index 0. When you try to retrieve index 1, you are trying to access an object at an index in the ArrayList that is not there.

  3. #3
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the code

    I have recently realised that but i can`t figure out how to fix this issue. Any suggestions?

  4. #4
    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: Problem with the code

    Test the size of the array list and do not try to get any elements that are not in the list.
    If there is a single element, then the largest index you can use is 0. If there are 2 in the list, the largest is 1.
    Note the largest possible index equals the length - 1;

    Perhaps you could describe what the program is supposed to do. Then we'd know what is wrong with the code.

    One way to do that is to add comments to the program describing what is to do.
    Last edited by Norm; August 9th, 2010 at 09:20 AM.

  5. #5
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the code

    the task is:

    Create a command line application which will store information for servers with the following fields:
    Server Name, IP, OS, RAM
    The application should be able to add, edit and delete records. Search options should be available as well.
    The records are stored in file

    So far i have problems with the ADD feature.

  6. #6
    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: Problem with the code

    Can you describe what the ADD feature should do?
    Describe the steps your program needs to take to get the data, organize it into an object and save it.

    Comment on your code:
    Don't use single letters for variable names. Use a name that describes what the variable is for.
    Last edited by Norm; August 9th, 2010 at 09:51 AM.

  7. #7
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the code

    The add feature should enter information through command line for the fields of the class(Server Name(String), IP(String), OS9String), RAM(int)). This information then should be written in a text file. I have an idea of what should i do but i don`t know how to write it in Java. The idea is that I shoudl make a method that returns an object from the type of my class (Specification). Then i should use the add() method to write this in my ArrayList object.

  8. #8
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the code

    I have actually just figured the solution out.

    public Specification (String name, String IP, String OS, int RAM){

    this.name=name;
    this.IP=IP;
    this.OS=OS;
    this.RAM=RAM;
    }

    //adding info
    public Specification AddInfo(String name, String IP, String OS, int RAM){
    return new Specification(name, IP, OS, RAM);
    }

    ...

    case 1: {
    System.out.println("How many records do you want to make?");
    int enterings;
    enterings=scan.nextInt();
    for(i=0;i<enterings;i++)
    { System.out.println("Enter name");
    String entered_name=scan.next();
    System.out.println("Enter IP");
    String entered_IP=scan.next();
    System.out.println("Enter OS");
    String entered_OS=scan.next();
    System.out.println("Enter RAM");
    int entered_RAM=scan.nextInt();
    a.add(a.get(i).AddInfo(entered_name, entered_IP, entered_OS, entered_RAM));
    }
    } break;
    ...

    Thanks for the attention

  9. #9
    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: Problem with the code

    a.add(a.get(i).AddInfo(entered_name, entered_IP, entered_OS, entered_RAM));
    Poor technique.
    Create a new object using its constructor and add that new object to a

  10. #10
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the code

    Quote Originally Posted by Norm View Post
    Poor technique.
    Create a new object using its constructor and add that new object to a
    how exactly to do that? I mean, aren`t constructors used when i create the object within the main method in my code? In my case the information about the objects is entered through the command line (using the Scanner class)

  11. #11
    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: Problem with the code

    This line uses the class's constructor to create a new object:
    new Specification(name, IP, OS, RAM);

    put that(with correct variable names) in the call to add()

Similar Threads

  1. problem in my code
    By wannabe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2010, 07:53 AM
  2. Problem with Recursive code
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 22nd, 2010, 09:36 PM
  3. Problem to organize my code in classes
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 21st, 2010, 12:13 PM
  4. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  5. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM