Jump to content

help my array pls ..~

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
im wondering what had happened to my array ..?
after i entered all the input value (double) into an array , and i was trying to display out the array but the result turns out as 0.0000000 0.000000 0.000000 ....and blah blah blah zero ..~

import java.util.Scanner;

public class Halo
{
    private double scores[];
    private Scanner input;
    
    public Halo(int number)
    {
        scores = new double[number];
        input = new Scanner(System.in);
    }
    
    public void input()
    {
        for(double score : scores)
        {
            System.out.println("double value: ");
            score = input.nextDouble();
        }
    }
    
    public void showArray()
    {
        for(double score : scores)
        {
            System.out.printf("%f\n" , score);
        }
    }
}

import java.util.Scanner;

public class HaloTest
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Number : ");
        int number = input.nextInt();
        
        Halo alo = new Halo(number);
        alo.input();
        alo.showArray();
    }
}


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Don't use a for each loop:

Quote

So when should you use the for-each loop? Any time you can. It really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.
source:The For-Each Loop

So use a 'normal' for loop
[highlight=Java]
for(int i=0 ; i<scores.length ; i++){
System.out.println("double value: ");
scores[i] = input.nextDouble();
}
[/highlight]

you'll notice that when you enter '5.4' it will output: '5.400000000'
To get rid of the 0000 i suggest you take a look at decimalformat
[highlight=Java]
DecimalFormat dfm= new DecimalFormat("#.##");
dfm.format(scores[i])
[/highlight]

#3
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
wow .. ~ thx a lot man ..~
so in what condition for me to use for each loop ?? when displaying the elements in the array ..?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I think it's best to use when nothing will change in the array. So no removal of elements, no adding, no changing. Just reading the array.

I rarely use a for each loop really.

#5
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
i see ...
as the source of DecimalFormat posted , we cannot directly call the constructor of DecimalFormat since the NumberFormat factory methods may return subclasses other than DecimalFormat .

then DecimalFormat dfm = new DecimalFormat("#.##"); ...?

#6
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
so my program will works after i remove the for-each loop to normal for-loop ...?

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Yes, it does work with me...

double i = 5.46541287;
        DecimalFormat dfm = new DecimalFormat("#.##");

        System.out.println(dfm.format(i));
this prints "5,47" for me...