5 Replies - 766 Views - Last Post: 12 November 2010 - 02:52 AM Rate Topic: -----

#1 Uushie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 23-October 10

Getting rid of the users input numbers

Posted 11 November 2010 - 10:56 PM

So I was thinking about how I could approach this obviously I did not do a great job. I can store all the numbers the user inputs just fine but now I get into where I butcher the code completely.

My goal is to have the user input numbers... as many as he wants as long as it is between 1 and 99. If the user inputs 0, the loop breaks.

I then need to take out all the numbers that the user put in and display all the numbers that were not printed.

So if the user puts in

5, 2, 6, 1, 7, 4, 0

then the output or what is printed would be

3 4 8........ 98 99 100.

Its fairly straight forward but I cannot figure out how to get rid of the numbers that the user inputs and then print out all the numbers the user did not input.

I am aware that I totally butchered that last statement to try and get rid of the numbers but I can't figure it out. Thank you for your help.

import java.util.Scanner;

public class Exercise06_01Extra {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double count = 0;
        int[] array;
        int numbers;
        int num;
        int[] isInput;
        int[] nonInput;
        int num2 = 1; //probably unnecessary
        int numbers2 = 0; //ditto
        int num3 = 1; //ditto
        int numbers3 = 0; //ditto

        array = new int[100];
        isInput = new int[100]; //probably and most likely totally unnecessary
        nonInput = new int[100]; // same as above statement
        numbers = 0;
        while (true) {
            System.out.print("Enter an integer numnber from 1-99: ");
            // The numbers are stored perfectly.
            num = input.nextInt();
            if (num <= 0) {
                break;
            }
            array[numbers] = num;
            numbers++;
            count = count + 1;
        }
        for (int a = 1; a < 100; a++) { // This is most likely unnecessary.
            // The goal was to originally get all the numbers from 1-100 stored.
            // however, this doesn't seem like the right approach.
            nonInput[numbers2] = num2;
            num2 = num2 + 1;
        }

        for (int i = 0; i <= count; i++) { //Butchered this. This is to delete
        //all the numbers that the user inputed. However, it only prints 0s
            for (int a = 0; a <= 99; a++) {
                if ((nonInput[i] <= array[numbers] && nonInput[i] >= array[numbers])) {
                    num3 = nonInput[i] - array[numbers];
                isInput[numbers3] = num3;
            }
        }
        System.out.print(isInput[numbers3] + " ");
    }
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Getting rid of the users input numbers

#2 Uushie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 23-October 10

Re: Getting rid of the users input numbers

Posted 12 November 2010 - 12:28 AM

import java.util.Scanner;

public class Exercise06_01Extra {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double count = 0;
        int[] array;
        int numbers;
        int num = 1;
        array = new int[100];
        numbers = 0;

        while (true) {
            System.out.print("Enter an integer numnber from 1-99: ");
            // The numbers are stored perfectly.
            num = input.nextInt();
            if (num <= 0) {
                break;
            }
            array[numbers] = num;
            numbers++;
            count = count + 1;
        }
            for (int a = 1; a < 100; a++) {
            if (array[a] >= a && array[a] <= a) {
                System.out.print("");
                }
            else
                System.out.print(a + " ");
    }
}
}



Alright, this looks much better after seeing how stupid I am for not realizing how I could simplify it but it is still wrong. I just wanted to post this to get rid of my previous embarrassing code.
Was This Post Helpful? 0
  • +
  • -

#3 Uushie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 23-October 10

Re: Getting rid of the users input numbers

Posted 12 November 2010 - 01:18 AM

I think I'm almost there, Though I cannot get my Is(array[numbers], 1); to be read. I think that might be the last thing needed to get this to work properly... I think.
import java.util.Scanner;

public class Exercise06_01Extra {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double count = 0;
        int[] array;
        int numbers;
        int num;
        array = new int[100];
        numbers = 0;

        while (true) {
            System.out.print("Enter an integer numnber from 1-99: ");
            // The numbers are stored perfectly.
            num = input.nextInt();
            if (num <= 0) {
                break;
            }
            array[numbers] = num;
            numbers++;
            count = count + 1;
        }
              for (int i = 1; i < 100; i++) { 
                  Is(array[numbers], i);
            if (Is(array[numbers], i)) {
                System.out.print(i + " ");
              }
            else
               System.out.print("");
                }
    }

    public static boolean Is(int[] array, int a) {
        int i = 0;
while (i < 100) {
            if (a >= array[i] && a <= array[i]) 
              return false;
        else
            i++;
    }
        return true;
}
}


Was This Post Helpful? 0
  • +
  • -

#4 Uushie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 23-October 10

Re: Getting rid of the users input numbers

Posted 12 November 2010 - 01:53 AM

nvm. I feel stupid now.
Was This Post Helpful? 0
  • +
  • -

#5 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: Getting rid of the users input numbers

Posted 12 November 2010 - 02:17 AM

Are you allowed to use ArrayLists or do you have to use arrays? I thought of a possible solution using ArrayLists, but I fear that it is overkill and that there is a simpler, more efficient solution. Regardless, what I would do would be to store all the numbers from 0 to 100 in one ArrayList. Now, let the user enter the numbers like normal and store this input in a second ArrayList. Note that the numbers the user inputted are the index positions that you want to remove from the first ArrayList. Finally loop through the second, smaller ArrayList and remove the entries in the larger list that correspond to the entries in the smaller list. After that, you can loop through the larger list and print out it's values. I hope that made some sense. I'm gonna keep coming back to this thread, as I'd love to see someone post a better way to do this.
Was This Post Helpful? 0
  • +
  • -

#6 Uushie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 23-October 10

Re: Getting rid of the users input numbers

Posted 12 November 2010 - 02:52 AM

Well I don't understand this at all. I totally get the right answer on NetBeans and it follows all the instructions unless I am missing something. However.... It says my answer is wrong. This code is correct to the best of my knowledge. I extended the amount of variables for the list you can put in due to the fact that in livelab, he does 110 numbers. I also added the other small thing we were supposed to do afterwards. I have no idea why this is wrong now but it is totally pissing me off. Excuse my language.

Originally I was going to do what you said codebug back in my first code which was very poor but I'm pretty sure adding that method solved that problem where it individually checks all the numbers in the user input before putting down a number.

Here is his example.

Enter the integer numbers from 1 to 99:
1 3 87 62 30 90 10 21 46 27 34 23 89 57 34 34 90 34 23 93 44
0

2 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 22 24 25 26 28 29 31 32 33 35 36 37 38 39 40 41 42 43 45 47 48 49 50 51 52 53 54 55 56 58 59 60 61 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 88 91 92 94 95 96 97 98 99 are the missing numbers.


Enter the integer numbers from 1 to 99:
1 3 87 62 30 90 10 21 46 27
12 40 83 9 39 88 95 59 20 37
80 40 87 67 31 90 11 24 56 77
11 48 51 42 8 74 1 41 36 53
52 82 16 72 19 70 44 56 29 33
54 64 99 14 23 22 94 79 55 2
60 86 34 4 31 63 84 89 7 78
43 93 97 45 25 38 28 26 85 49
47 65 57 67 73 69 32 71 24 66
92 98 96 77 6 75 17 61 58 13
35 81 18 15 5 68 91 50 76
0

All numbers are covered.

import java.util.Scanner;
public class Exercise06_01Extra {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double count = 0;
        int[] array;
        int numbers;
        int num;
        array = new int[200];
        numbers = 0;

        while (true) {
            System.out.print("Enter an integer numnber from 1-99: ");
            num = input.nextInt();
            if (num <= 0) {
                break;
            }
            array[numbers] = num;
            numbers++;
            count = count + 1;
        }
        int tally = 0;
              for (int i = 1; i <= 99; i++) {
                  Is(array, i);
            if (Is(array, i)) {
                System.out.print(i + " ");
              }
            else
                System.out.print("");
                  tally = tally + 1;
                }
        if (tally >= 98)
           System.out.println("All numbers are covered.");
        else
           System.out.print(" are the missing numbers.");
    }
    public static boolean Is(int[] array, int a) {
        int i = 0;
while (i < 200) {
            if (a >= array[i] && a <= array[i])
              return false;
        else
            i++;
    }
        return true;
}
}



I am full of sadness.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1