10 Replies - 476 Views - Last Post: 18 October 2011 - 09:14 AM Rate Topic: -----

#1 Steve_OH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 17-October 11

Getting my head around arrays

Posted 17 October 2011 - 08:20 PM

Ok, so I am kinda new to arrays and I'm having the hardest time getting my head around a concept in java. I am trying to store and display user input values into my array and then print out the array in a line.

For example, if i was to define with my user input that the array had a length of 4, I could then ask for the values of the array (eg, a random assortment could be 1,7,5,3,2) and then print them out as 1 7 5 3 2 instead of 0 0 0 0 0 as I keep getting...

Any help or advice would be greatly appreciated.


import java.util.*;

public class sequencer {
	
	public int amount;
	public int tally;
	public int counter;
	
	Scanner reader = new Scanner (System.in);
	
	
	public void sorter () {
		int[] numbers;
		tally = 0;
		
		
		System.out.print("How many integers are in this sequence?");
		amount = reader.nextInt();
		numbers = new int [amount];	
		counter = amount;
		
		while (amount < 0){
			System.out.println("dont be silly!");
			amount = reader.nextInt();
		}
		
		
		while (tally < counter){
			
			for (int i=1; i<= amount; i++){
				
				System.out.print("Please enter value "+tally+":");
				inters=reader.nextInt();
				tally ++;	
			}
		}
		
		if (tally >= counter) {
			
			System.out.println("Results: ");
						
			int x;
				
			for (x = 0; x < inters; ++x){  
					System.out.print (" " + numbers [amount]); 
					
			}
			
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Getting my head around arrays

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting my head around arrays

Posted 17 October 2011 - 08:25 PM

numbers = new int [amount];

should be executed after your while(amount < 0)
when you know that amount is valid

and you do not need you tally flag simply

for(int i = 0; i < numbers.length; ++i)
numbers[i] = reader.nextInt();
Was This Post Helpful? 2
  • +
  • -

#3 Steve_OH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 17-October 11

Re: Getting my head around arrays

Posted 17 October 2011 - 08:39 PM

I cant seem to get my head around the while loops. I tried to implement your suggestion and the for loop didn't initialize.

import java.util.*;

public class sequencer {
	
	public int amount;
	public int tally;
	public int counter;
	public int inters;
	
	Scanner reader = new Scanner (System.in);
	
	
	public void sorter () {
		int[] numbers;
		tally = 0;
		
		
		System.out.print("How many integers are in this sequence?");
		amount = reader.nextInt();
		
		counter = amount;
		
		while (amount < 0){
			System.out.println("dont be silly!");
			amount = reader.nextInt();
		}
		
		numbers = new int [amount];	
		
		if (amount > 0){
			
			
			[color="#FF0000"]for (int i = 0; i < numbers.length; ++i){
				numbers[i] = reader.nextInt(); 
				
				System.out.print("Please enter value "+tally+":");
				inters=reader.nextInt();
				tally++;[/color]	
			}
		}
		
		
		if (tally >= counter) {
			
			System.out.println("Results: ");
						
			int x;
				
			for (x = 0; x < inters; ++x){  
					System.out.print (" " + numbers [amount]); 
					
			}
			
		}
	}
}
		
	







I really do appreciate the help! Just trying to grasp the concepts

View PostSteve_OH, on 17 October 2011 - 08:37 PM, said:

I cant seem to get my head around the while loops. I tried to implement your suggestion and the for loop didn't initialize.


For loops, not While loops...
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Getting my head around arrays

Posted 17 October 2011 - 08:40 PM

You will want to:
-Prompt for input first in the loop
-Get the nextInt() from Scanner
-Set numbers[i] = the int you just read in

You do not want to read nextInt() twice in your loop.

Also, might I suggest this supplementary tutorial on arrays. :)
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting my head around arrays

Posted 17 October 2011 - 08:45 PM

Please do not put [html] in your code

import java.util.*;

public class sequencer {
	
	public int amount;
	public int tally;
	public int counter;
	public int inters;
	
	Scanner reader = new Scanner (System.in);
	
	
	public void sorter () {
		int[] numbers;
		
		
		System.out.print("How many integers are in this sequence?");
		amount = reader.nextInt();
				
		while (amount < 0){
			System.out.println("dont be silly!");
			amount = reader.nextInt();
		}
		
		numbers = new int [amount];	
					
		for (int i = 0; i < numbers.length; ++i){
			numbers[i] = reader.nextInt(); 				
		}
		
		
		System.out.println("Results: ");
				
		for (int x = 0; x < numbers.length; ++x){  
			System.out.print (" " + numbers [x]); 
					
			}
			
		}
	}
}
		


Was This Post Helpful? 0
  • +
  • -

#6 Steve_OH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 17-October 11

Re: Getting my head around arrays

Posted 17 October 2011 - 09:09 PM

View Postmacosxnerd101, on 17 October 2011 - 08:40 PM, said:

You will want to:
-Prompt for input first in the loop
-Get the nextInt() from Scanner
-Set numbers[i] = the int you just read in

You do not want to read nextInt() twice in your loop.

Also, might I suggest this supplementary tutorial on arrays. :)


I really appreciate your help! The stuff in that blog (just read all of it) I pretty well know already, but that tutorial doesn't really cover my questions (hence I have been stuck). Is there a tutorial about customizable array sizes?


View Postpbl, on 17 October 2011 - 08:45 PM, said:

Please do not put [html] in your code


Where have I done that?

View PostSteve_OH, on 17 October 2011 - 09:07 PM, said:

View Postmacosxnerd101, on 17 October 2011 - 08:40 PM, said:

You will want to:
-Prompt for input first in the loop
-Get the nextInt() from Scanner
-Set numbers[i] = the int you just read in

You do not want to read nextInt() twice in your loop.

Also, might I suggest this supplementary tutorial on arrays. :)


I really appreciate your help! The stuff in that blog (just read all of it) I pretty well know already, but that tutorial doesn't really cover my questions (hence I have been stuck). Is there a tutorial about customizable array sizes?


View Postpbl, on 17 October 2011 - 08:45 PM, said:

Please do not put [html] in your code


Where have I done that?


OH nevermind, when I made it red... Gotcha...
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Getting my head around arrays

Posted 17 October 2011 - 09:23 PM

Quote

I really appreciate your help! The stuff in that blog (just read all of it) I pretty well know already, but that tutorial doesn't really cover my questions (hence I have been stuck). Is there a tutorial about customizable array sizes?

I know. I figured it would be good extra reading on arrays though. As for customizable array sizes, you will want to use an ArrayList. Locke has a tutorial on static arrays vs. ArrayLists.

To manually "resize" an array, you will want to:
-Create a new array of larger size
-Copy the elements from the old array to the new array
-Assign the new array to the old array variable

ArrayList is easier to use, though. :)
Was This Post Helpful? 1
  • +
  • -

#8 Steve_OH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 17-October 11

Re: Getting my head around arrays

Posted 17 October 2011 - 09:41 PM

That looks extremely helpful, but the requirements of what I am working on are that I have to only work with 1 1D array...

Thanks for the reference, will use it in future!

Dont worry, there is more to my assignment than this (In case you worried I was getting you to do my assignment for me). I have to still do some things with my results, but this is the part that I am getting stuck on. Once I understand the concept I'll be apples
Was This Post Helpful? 0
  • +
  • -

#9 Steve_OH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 17-October 11

Re: Getting my head around arrays

Posted 18 October 2011 - 12:21 AM

I totally just figured it out!!

Thanks so much for all your help guys!

as a reference there was a few small changes I had to make and now it works perfectly.

import java.util.*;

public class sequencer {
	
	public int amount;
	public int tally;
	public int counter; 
	
	Scanner reader = new Scanner (System.in);
	
	
	public void sorter () {
		int[] numbers;
		int i = 0;
		int inters = 0;
		tally = 0;
		
		System.out.print("How many integers are in this sequence?");
		amount = reader.nextInt();
		
		counter = amount;
		
		while (amount < 0){
			System.out.println("dont be silly!");
			amount = reader.nextInt();
		}
		
		numbers = new int [amount];	
		
		if (amount > 0){
			
			
			for (i = 0; i < numbers.length; ++i){ 
				
				System.out.print("Please enter value "+tally+":");
				inters=reader.nextInt();
				numbers[i] = inters;
				tally++;
			
			}
		}
		
		
		if (tally >= counter) {
			
			System.out.println("Results: ");
						
			
				
			for (i = 0; i < numbers.length; ++i){  
					System.out.print (" " + numbers[i]); 
					
			}
			
		}
	}
}



Was This Post Helpful? 1
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Getting my head around arrays

Posted 18 October 2011 - 08:14 AM

Glad you got it worked out! Thanks for sharing your solution! :)
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting my head around arrays

Posted 18 October 2011 - 09:14 AM

Some little things you didn't seem to have understood :)
		
		numbers = new int [amount];	
		
		// this if is useless  if amount == 0
		if (amount > 0){ 
			
			// then numbers.length will be 0 
			// so this loop won't execute (i = 0; i < 0;
			for (i = 0; i < numbers.length; ++i){ 
				
				System.out.print("Please enter value "+tally+":");
				inters=reader.nextInt();
				numbers[i] = inters;
				tally++;  // you increment tally here numbers.length of time
				          // out of the loop the value cannot be different than amount
			
			}
		}
		
		// so it is impossible that tally can be != counter
		// actually counter is useless, it is == amount
                if (tally >= counter) {
			
			System.out.println("Results: ");
						
			
				
			for (i = 0; i < numbers.length; ++i){  
					System.out.print (" " + numbers[i]); 
					
			}
			
		}
	}
}


So your variables tally and counter are not required at all
So have to if() that are useless

Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1