11 Replies - 861 Views - Last Post: 13 February 2013 - 03:32 PM Rate Topic: -----

#1 ph0z  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 10-February 13

Question about outputting array list

Posted 10 February 2013 - 04:02 PM

Public class test {
    public static void main(String[] args){
      int[] result = new int[5];
        
        for (int i = 0; i < result.length; i++){
                result[i] = i;
        }
        for (int value : result){
                System.out.print (value);
        }
    }
}



I was wondering why is for needed with (int value : result) to print out to the console the array list properly. The output would be

0 1 2


but if we take out the for (int value : result) and just put System.out.print (result), the output would be
[I@19189e1.


Is This A Good Question/Topic? 0
  • +

Replies To: Question about outputting array list

#2 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Question about outputting array list

Posted 10 February 2013 - 04:04 PM

System.out.print (result)


This will print the memory address of the array, whereas;
for (int value : result){
                System.out.print (value);
        }


will print each individual element of that array

EDIT: here is a good tutorial on arrays: http://www.dreaminco...tion-to-arrays/

This post has been edited by k3y: 10 February 2013 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 ph0z  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 10-February 13

Re: Question about outputting array list

Posted 10 February 2013 - 04:41 PM

View Postk3y, on 10 February 2013 - 04:04 PM, said:

System.out.print (result)


This will print the memory address of the array, whereas;
for (int value : result){
                System.out.print (value);
        }


will print each individual element of that array

EDIT: here is a good tutorial on arrays: http://www.dreaminco...tion-to-arrays/


Thanks for the quick reply k3y, I appreciate it!
Was This Post Helpful? 0
  • +
  • -

#4 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Question about outputting array list

Posted 10 February 2013 - 04:52 PM

No problem ph0z;
If my post helped you out please hit the + button. It let's me know that I am actually helping people =D
Was This Post Helpful? 1
  • +
  • -

#5 ph0z  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 10-February 13

Re: Question about outputting array list

Posted 10 February 2013 - 05:55 PM

I didn't want to make another thread so i'll just add it into this one. Another problem I have is, I'm trying to get the user's input to add to the empty list but the code doesn't seem to run correctly.

public class Test2 {
        
    public static void main(String[] args) {
    	
    	Scanner scan = new Scanner(System.in);
    	
       	int[] intSet = new int[4];
       	
       	for (int j = 0; j > intSet.length; j++){
       		System.out.print ("Enter digits: ");
       		intSet[j] = scan.nextInt();
       	}
}



Can someone tell me what I'm doing wrong? When I do run the program it doesn't ask for any input and just says "Process Completed", thanks again :)
Was This Post Helpful? 0
  • +
  • -

#6 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Question about outputting array list

Posted 10 February 2013 - 06:06 PM

Make this line

for (int j = 0; j > intSet.length; j++){


this

for (int j = 0; j < intSet.length; j++){


You were using the > sign. That's wrong !
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Question about outputting array list

Posted 10 February 2013 - 06:06 PM

for (int j = 0; j > intSet.length; j++){

should be

for (int j = 0; j < intSet.length; j++){
Was This Post Helpful? 1
  • +
  • -

#8 ph0z  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 10-February 13

Re: Question about outputting array list

Posted 10 February 2013 - 06:22 PM

really? I totally looked past that? Wow fail on my part LOL thanks again guys
Was This Post Helpful? 0
  • +
  • -

#9 ph0z  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 10-February 13

Re: Question about outputting array list

Posted 13 February 2013 - 12:11 AM

I'm having issues with outputting the correct values for this histogram program. After entering the numbers and having it outputting, it seems to overlap and count extra. Here is my program code

import java.util.Scanner;
public class Histogram {
       
    public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     int userInt;
     int intAmount;
     
  	 String[] stars = {" 1 - 10 | ", "11 - 20 | ", "21 - 30 | ",
  	 "31 - 40 | ", "41 - 50 | ", "51 - 60 | ", "61 - 70 | ", "71 - 80 | ", "81 - 90 | ",
  	 "90 - 100 | "};
    
     
     //Ask amount of numbers to intialize integers in intSet
     System.out.print ("Enter amount of numbers: ");
     intAmount = scan.nextInt();
      		
     //Add user input into intSet
     int[] intSet = new int[intAmount];
     for (int i = 0; i < intSet.length; i++){
      		System.out.print ("Enter a number: ");
      		userInt = scan.nextInt();
      		intSet[i] = userInt;
     }
    
     //Output stars for following range
     for (int j = 0; j < intSet.length; j++){
     	if (intSet[j] <= 10){
     		stars[0] += "*";
     	}
     	if (intSet[j] > 10 && intSet[j] >= 20){
     		stars[1] += "*";
     	}
     	if (intSet[j] > 20 && intSet[j] >= 30){
     		stars[2] += "*";
     	}
     	if (intSet[j] > 30 && intSet[j] >= 40){
     		stars[3] += "*";
     	}
     	if (intSet[j] > 40 && intSet[j] >= 50){
     		stars[4] += "*";
     	}
     	if (intSet[j] > 50 && intSet[j] >= 60){
     		stars[5] += "*";
     	}
     	if (intSet[j] > 60 && intSet[j] >= 70){
     		stars[6] += "*";
     	}
     	if (intSet[j] > 70 && intSet[j] >= 80){
     		stars[7] += "*";
     	}
     	if (intSet[j] > 80 && intSet[j] >= 90){
     		stars[8] += "*";
     	}
     	if (intSet[j] > 90 && intSet[j] >= 100){
     		stars[9] += "*";
     	}
     }
 
   //Print out histogram of numbers
   System.out.println(stars[0]);
   System.out.println(stars[1]);
   System.out.println(stars[2]);
   System.out.println(stars[3]);
   System.out.println(stars[4]);
   System.out.println(stars[5]);
   System.out.println(stars[6]);
   System.out.println(stars[7]);
   System.out.println(stars[9]);
   
   }
     
}



Here is a sample run of the program

Enter amount of numbers: 20
Enter a number: 1
Enter a number: 5
Enter a number: 6
Enter a number: 20
Enter a number: 10
Enter a number: 22
Enter a number: 30
Enter a number: 12
Enter a number: 60
Enter a number: 70
Enter a number: 80
Enter a number: 99
Enter a number: 21
Enter a number: 5
Enter a number: 4
Enter a number: 14
Enter a number: 34
Enter a number: 38
Enter a number: 40
Enter a number: 55

 1 - 10 | *****
11 - 20 | ***********
21 - 30 | *********
31 - 40 | ******
41 - 50 | *****
51 - 60 | ****
61 - 70 | *** 
71 - 80 | **
81 - 90 |
91 - 100 |



I've rechecked how I setup my boolean for my if statements and pretty sure there all right. I'm guessing it might be something wrong with my 1st for loop and adding the user's integers into the set. Any help would be appreciated, thanks :)/>
Was This Post Helpful? 0
  • +
  • -

#10 Chrisangel29  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 89
  • Joined: 25-January 13

Re: Question about outputting array list

Posted 13 February 2013 - 02:51 AM

View Postph0z, on 13 February 2013 - 12:11 AM, said:

I'm having issues with outputting the correct values for this histogram program. After entering the numbers and having it outputting, it seems to overlap and count extra. Here is my program code

import java.util.Scanner;
public class Histogram {
       
    public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     int userInt;
     int intAmount;
     
  	 String[] stars = {" 1 - 10 | ", "11 - 20 | ", "21 - 30 | ",
  	 "31 - 40 | ", "41 - 50 | ", "51 - 60 | ", "61 - 70 | ", "71 - 80 | ", "81 - 90 | ",
  	 "90 - 100 | "};
    
     
     //Ask amount of numbers to intialize integers in intSet
     System.out.print ("Enter amount of numbers: ");
     intAmount = scan.nextInt();
      		
     //Add user input into intSet
     int[] intSet = new int[intAmount];
     for (int i = 0; i < intSet.length; i++){
      		System.out.print ("Enter a number: ");
      		userInt = scan.nextInt();
      		intSet[i] = userInt;
     }
    
     //Output stars for following range
     for (int j = 0; j < intSet.length; j++){
     	if (intSet[j] <= 10){
     		stars[0] += "*";
     	}
     	if (intSet[j] > 10 && intSet[j] >= 20){
     		stars[1] += "*";
     	}
     	if (intSet[j] > 20 && intSet[j] >= 30){
     		stars[2] += "*";
     	}
     	if (intSet[j] > 30 && intSet[j] >= 40){
     		stars[3] += "*";
     	}
     	if (intSet[j] > 40 && intSet[j] >= 50){
     		stars[4] += "*";
     	}
     	if (intSet[j] > 50 && intSet[j] >= 60){
     		stars[5] += "*";
     	}
     	if (intSet[j] > 60 && intSet[j] >= 70){
     		stars[6] += "*";
     	}
     	if (intSet[j] > 70 && intSet[j] >= 80){
     		stars[7] += "*";
     	}
     	if (intSet[j] > 80 && intSet[j] >= 90){
     		stars[8] += "*";
     	}
     	if (intSet[j] > 90 && intSet[j] >= 100){
     		stars[9] += "*";
     	}
     }
 
   //Print out histogram of numbers
   System.out.println(stars[0]);
   System.out.println(stars[1]);
   System.out.println(stars[2]);
   System.out.println(stars[3]);
   System.out.println(stars[4]);
   System.out.println(stars[5]);
   System.out.println(stars[6]);
   System.out.println(stars[7]);
   System.out.println(stars[9]);
   
   }
     
}



Here is a sample run of the program

Enter amount of numbers: 20
Enter a number: 1
Enter a number: 5
Enter a number: 6
Enter a number: 20
Enter a number: 10
Enter a number: 22
Enter a number: 30
Enter a number: 12
Enter a number: 60
Enter a number: 70
Enter a number: 80
Enter a number: 99
Enter a number: 21
Enter a number: 5
Enter a number: 4
Enter a number: 14
Enter a number: 34
Enter a number: 38
Enter a number: 40
Enter a number: 55

 1 - 10 | *****
11 - 20 | ***********
21 - 30 | *********
31 - 40 | ******
41 - 50 | *****
51 - 60 | ****
61 - 70 | *** 
71 - 80 | **
81 - 90 |
91 - 100 |



I've rechecked how I setup my boolean for my if statements and pretty sure there all right. I'm guessing it might be something wrong with my 1st for loop and adding the user's integers into the set. Any help would be appreciated, thanks :)/>/>


i dont understand what is your problem but i guess you missing one array element here

System.out.println(stars[7]);
// System.out.println(stars[8]); ???
System.out.println(stars[9]);




Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Question about outputting array list

Posted 13 February 2013 - 10:29 AM

if (intSet[j] > 10 && intSet[j] >= 20){


I doubt you want a >= there
and instead of hardcoding all the stuff a single loop should do the trick

for(int i = 0; i < 10; ++i) {
   int min = i * 10;
   int max = min + 10;
   for(int j = 0; j < inset.length; ++j) {
      if(inset[j] > min && inset[j] <= max)
         star[i] += "*";
   }
}


Was This Post Helpful? 1
  • +
  • -

#12 ph0z  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 10-February 13

Re: Question about outputting array list

Posted 13 February 2013 - 03:32 PM

View Postpbl, on 13 February 2013 - 10:29 AM, said:

if (intSet[j] > 10 && intSet[j] >= 20){


I doubt you want a >= there
and instead of hardcoding all the stuff a single loop should do the trick

for(int i = 0; i < 10; ++i) {
   int min = i * 10;
   int max = min + 10;
   for(int j = 0; j < inset.length; ++j) {
      if(inset[j] > min && inset[j] <= max)
         star[i] += "*";
   }
}


After all the repetitiveness of writing the if statement I was thinking there must be an easier way to do this. Thanks pbl
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1