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

Thread: I/O List.java

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation I/O List.java

    Write a program that allows the user to enter a list of up to 100 integers. Please check for boundary conditions, for example, the user has to enter more than 0 integers but not more then 100. The end of the list is indicated by the special value -1. The program then reads more integers (max. 100), and, for every value, tells whether or not the value appears in the first list. The program ends when the user enters -1 (for the second time). For example, for the input

    25 50 75 100 -1
    99 25 30 -1

    the program should respond:

    99: not in list
    25: yes, in list
    30: not in list

    So far I have the following

    public class List
    {
    public static void main(String[]args)
    {
    // Input of the FirstList by the user.
    int[] Firstlist = new int[100];
    int i = 0;
    int tempNum = StdIn.readInt();

    while (tempNum != -1)
    {
    Firstlist[i] = tempNum;
    tempNum = StdIn.readInt();
    i = i + 1;
    }
    System.out.println(Firstlist[2]);

    // Comparison
    int [] Secondlist = new int[100];
    int j = 0;
    int tempNum2 = StdIn.readInt();

    while (tempNum2 != -1)
    {
    Secondlist[j] = tempNum2;
    tempNum2 = StdIn.readInt();
    j = j + 1;
    }
    System.out.println(Secondlist[2]);

    }
    }

    How do I compare the second list with the first list...Also note that Im using StdIn.readInt(); and I have all the files to use it...I got them from "introduction to programming an indeseplinary approach" website.

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

    Default Re: I/O List.java

    How do I compare the second list with the first list..
    How do you want to compare the arrays?
    If element by element, use a loop with an if test to compare the elements from each array.
    Otherwise explain what you mean by comparing the arrays.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I/O List.java

    [QUOTE=Norm;60425]How do you want to compare the arrays?


    The program has to tell you whether or not a value in the second list appears in the first list.

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

    Default Re: I/O List.java

    That sounds like a search. Take an element from the second list and search the first list.

    When posting code, please wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I/O List.java

    Instead of using arrays, try using a LinkedList:
    LinkedList<Integer> numbers1 = new LinkedList<Integer>();
    LinkedList<Integer> numbers2 = new LinkedList<Integer>();
     
    while(true){
    System.out.println("Enter number");
    int number = new Scanner(System.in).nextInt();
    if(number == -1){
    if(numbers1.size() > 0){
    break;
    }else{
    System.out.println("Need atleast one element inside list");
    }
    }else{
    if(numbers1.size() <= 100){
    numbers1.add(number);
    }else{
    System.out.println("Can't have more then 100 elements, ending list");
    break;
    }
    }
    }
     
    while(true){
    System.out.println("Enter number");
    int number = new Scanner(System.in).nextInt();
    if(number == -1){
    if(numbers2.size() > 0){
    break;
    }else{
    System.out.println("Need atleast one element inside list");
    }
    }else{
    if(numbers2.size() <= 100){
    numbers2.add(number);
    }else{
    System.out.println("Can't have more then 100 elements, ending list");
    break;
    }
    }
    }
     
    for(int i = 0; i < numbers2.size(); i++){
    System.out.println(String.format("%d: %s", numbers2.get(i), numbers1.contains(numbers2.get(i)) ? "is in list" : "not in list"));
    }

    I might be a little off with the brackets because I wrote that inside here instead of Eclipse, but yeah, you get the point.

Similar Threads

  1. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  2. Testing whether a list of URLs are active or not in java?
    By aybeeryu in forum Java Networking
    Replies: 1
    Last Post: June 3rd, 2011, 09:17 AM
  3. saving list of data from JSP into java object
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: January 12th, 2011, 11:24 AM
  4. Please list some Java obfuscators !!
    By Ritesht93 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 29th, 2010, 06:47 PM
  5. SpinnerListModel setList(List<?> list)
    By roy epperson in forum Collections and Generics
    Replies: 4
    Last Post: November 29th, 2010, 10:30 AM