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

Thread: Arrays

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Arrays

    I have this assignment where i should be able to write in as many whole numbers as i want too and then end it with a negative number.
    After the negative number the code should be able to print all the numbers backwards except for the negative one.

    So something like this:

    Whole N1: 302 "Then hit enter"
    Whole N2: 32 --||--
    Whole N3: 534 --||--
    Whole N4: -302 --||--

    The amount of positive whole numbers are: 3.
    Your numbers backwards is: 203, 23, 435.



    I don't really know of where to begin. The course is new and i am new to java. I can use whatever i like here so arrays would be
    the best thing i guess but i don't know of where to start.

    Any tips?

  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: Arrays

    Look at using a loop to repeatedly ask the user to enter the next number
    Use the Scanner class's methods to read the user's input

    If you use an array to save the user's inputs in, be sure to define it large enough to hold all the inputs.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Arrays

    Okay so i can make a forloop that takes the numbers that i use and print them out.
    But i can't find a way to store them in the array.. or atleast it doesn't look like i am "storing" them right.


    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
    		int NUM[] = new int[100000];
    		int nums = 0;
    		int helhet = 0;
    		int line = 0;
    		//int lenght = input.nextInt();
     
    		System.out.println("Type in a whole number!");
     
    		for (int i = 1; i > 0; i++) {
    			line++;
    			helhet = input.nextInt();
    			System.out.println("Line " + line + ": " + helhet + "");
    			NUM[helhet] = helhet;
    		}
     
    		/// for (int k = lenght - 1; k >= 0; k--) {
    		// System.out.print("K Value: " + k);
     
    //		}
     
    		// if (helhet < 0) {
    		// System.out.print(":" + NUM);
    		// }
    	}
    }

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Arrays

    You don't need to use 'line'. Just use 'i'. And start it at 0 since arrays are zero based. So it would be NUM[i] =helnet;

    Regards,
    Jim

  5. #5
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Arrays

    Okay but how do i store the values that i get from the loop into the array and then print them out later?

  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: Arrays

    how do i store the values that i get from the loop into the array
    After reading the value from the user, use an assignment statement to put the value into a slot in the array:
       theArray[theIndex] = theValue;
    The value of theIndex will need to be changed to the next slot before doing the above assignment again.
    The value in theIndex will start at 0 and be incremented by one to access the slots in the array one by one starting at the first slot.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Arrays

    So.. i guess that this could work. However i feel like the value of "Data" changes and i cannot print all the numbers afterwards. In either case without the if statement then the program print out numbers so you write 38 and you get 38 etc.

    But i still don't get the arrays. Any tips?


    PS: Made a new code real quick because i am not on my main pc. Could've just copied but yeah..


    	public static void main(String[] args) {
     
    		int flee = 0;
    		int Data[];
    		Data = new int[1000000];
    		Scanner in = new Scanner(System.in);
    		int input = 0;
    		System.out.println("Skriv in x antal värden och sedan ett negativt tal för att påvisa att det är klart!");
     
    		for (int i = 1; i > 0; i++) {
    			input = in.nextInt();
    			flee++;
    			System.out.println("Ditt värde är : " + "Antal värde:" + flee + "; " + input);
    			Data[1000000] = input;
     
    		}
    		if (input < 0) { //Problem area! 
    			System.out.print(Data);
    			System.exit(input);
    		}
     
    	}
     
    }

  8. #8
    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: Arrays

    The index to the Data array needs to change its value after each value is stored in the array. It should start at 0 and be incremented by 1 after each use.

    end it with a negative number.
    The value of the number entered by the user needs to be tested with an if statement inside the loop. When the value is < 0 the code must exit the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Arrays

    I am guessing that i am making things worse than they have to be..
    But i am learning at the same time.

    Feels like i could almost get this working... almost.


    public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
    		int NUM[] = new int[500];
    		int line = 0;
    		int cause = 0;
     
    		System.out.println("Type in a whole number!");
     
    		for (int i = 0; i < 500; i++) {
    			line++;
    			NUM[i] = input.nextInt();
    			System.out.println("Line " + line + ": " + ++cause + "");
    			if (NUM[i] < 0) {
    				for (int k = 0; k < NUM[k]; k++) {
    					System.out.println("Here is the numbers: " + ++cause + NUM[k]);
    				}
    			}
    		}
     
     
    	}
    }

  10. #10
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Arrays

    You need to rethink your strategy. You shouldn't be putting the display loop inside the input loop.

    To recap, here is what I think you should be doing.

    1. Enter the numbers via the scanner and put them in an array.
    2. As you enter the numbers, take a count of them with a counter
    Steps 1 and 2 can be done in a simple for loop. When you enter a negative number, exit the loop.

    3. To display them in reverse order, start with the last position you stored one in and decrement the position index.
    4. Print them out.
    Like before, steps 3 and 4 can be done with a loop.

    Regards,
    Jim

  11. #11
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Arrays

    Thanks Jim!

    I almost got it working now!
    But i had seen the assignment wrong. So what the program has to do is to print out all of the whole numbers that are positive and take the
    numbers stored and print them out but backwards.

    It does everything right except for when it prints because i cannot get it to print backwards. So if i input 1 and 2 then i can print back 1 and 2 but not in reverse order
    such as 2 and 1.



    public static void main(String[] args) {
     
    		Scanner in = new Scanner(System.in);
     
    		int num[] = new int[10000];
    		int hell = 0;
     
    		System.out.println("Write a couple of whole numbers and end with a negative one : ");
     
    		for (int i = 0; i < 10000; i++) {
    			num[i] = in.nextInt();
    			System.out.println("Value " + (i + 1) + ": " + num[i]);
    			hell++;
     
    			if (num[i] < 0) {
    				break;
    			}
     
    		}
     
    		String Five = "Your values are: "; //Maybe there is something that could be changed here! Not sure...
    		for (int i = 0; i < hell; i++) {
    			Five += num[i];
    			if (i < hell) {
    				Five += ", ";
    			}
    		}
    		System.out.println(Five);
    		System.out.println("Amount of positive values : " + (hell - 1));
     
    	}
     
    }

  12. #12
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Arrays

    Hint: for loops can go from 0 to n with a positive increment or from n to 0 with a negative increment. You just need to adjust the parameters(e.g. starting point) to handle your list.

    Regards,
    Jim

  13. #13
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Arrays

    Quote Originally Posted by jim829 View Post
    Hint: for loops can go from 0 to n with a positive increment or from n to 0 with a negative increment. You just need to adjust the parameters(e.g. starting point) to handle your list.

    Regards,
    Jim
    Thank you Jim! Got it working now.

Similar Threads

  1. Arrays and MD Arrays
    By mstratmann in forum Object Oriented Programming
    Replies: 4
    Last Post: August 28th, 2013, 04:12 PM
  2. need help with arrays
    By njabulo ngcobo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 7th, 2013, 08:46 PM
  3. Help with Arrays
    By mstratmann in forum Object Oriented Programming
    Replies: 1
    Last Post: June 3rd, 2013, 09:19 PM
  4. 2D arrays
    By Fluffy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2012, 05:11 PM
  5. Help with Arrays
    By xdx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 12:39 AM