0

First question: I am trying to print out four double values in reverse order so if the user enters "1 2 3 4" I want it to print out "4 3 2 1" Here is my coding.

// User enters 4 float values here
System.out.println("Enter 4 float values");
inputValues2 = input.readLine();
String[] dblValues = inputValues2.split("\\s+");

Second Question: I want to print out the integer portion of a double value. I guess I am trying to convert the double value to an int. I have tried these two different ways and get errors for both of them. Any ideas?? Maybe I should just do some sort of rounding to a whole number??

int dblConvert = dblAverage.intValue();           // One way
int dblConvert = Integer.parseInt(dblAverage);    // Second way
Marcus Burkhart
  • 95
  • 1
  • 14
  • For the first part, what have you done so far? For the second one, simply cast your double to int: `(int) dblAverage`. You can call `dblAverage.intValue()` only on a `Double`, not on a `double` (note the capital D). – jhamon Jan 21 '15 at 09:09
  • 1
    Please restrain to a _single_ question per question and give it a proper title. – Jens Erat Jan 21 '15 at 09:12
  • @JensErat I would if I could ask more than 1 question every 90 minutes.... – Marcus Burkhart Jan 21 '15 at 09:25
  • 1
    Think of it as killing 2 birds with 1 stone – Marcus Burkhart Jan 21 '15 at 09:26
  • Better use the `nextInt()` method from `Scanner` to iterate over the items (storing the items for reverse order still needed). It will give you single integers even if seperated by multiple whitespaces. – KnutKnutsen Jan 21 '15 at 09:26

4 Answers4

9

Find the answers/hints for your questions.

  1. You have to print the content of the array in reverse order.

    for (int i = dblValues.length -1;i >= 0; i--) {
        System.out.println(dblValues[i]);
    }
    

    This is the fundamental way for a begginer. They are some predifined libraries to do this job

  2. You can use int a = (int) Math.round(doubleVar);
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
0

For your second question: there is a Math.round() method. You'll then need to cast the returned long to int in a safe way.

Community
  • 1
  • 1
Villager
  • 604
  • 6
  • 15
0

If you dont just want to print the numbers in reverse order and order them naturally you can add them to a list and sort them

If you also don't care about rounding you can just cast a double

    String s = "4 3 2 1";
    String[] split = s.split("\\s+");
    List<Integer> numbers = new LinkedList<>();

    for (String toAdd : split) {
        numbers.add(Integer.parseInt(toAdd));
    }       

    Collections.sort(numbers);      
    System.out.println(numbers);

    System.out.println((int)45.7654);
Alan Barrows
  • 579
  • 3
  • 10
0

1.) loop from last index and print in reverse order.

2.) Use Math.round() for getting int part

public static void main(String[] arg) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter 4 float values");
        String inputValues2 = input.nextLine();
        String[] dblValues = inputValues2.split("\\s+");

        System.out.println("Reverse order");

        for (int i = dblValues.length - 1 ; i >= 0 ; i--) {
            System.out.println(dblValues[i]);
        }

        System.out.println("int part ");

        for (String s : dblValues) {
            int a = Math.round(new Float(s));
            System.out.println(a);
        }
    }

output

Enter 4 float values
12.4 23.5 34.5 45.3
Reverse order
45.3
34.5
23.5
12.4
int part 
12
24
35
45
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116