Java: How to read multiple scanner values for one int

蹲街弑〆低调 提交于 2019-12-10 23:18:40

问题


I have been trying to figure out how calculate the area and volume based on the input obtain from the Scanner Class. The exercise consist of receiving multiple pair of radius and height at once.

I have written the methods and tested them, so those should be working. The problem I am having is when I want to use inputs from 'Scanner' and use them to make the calculations.

Here is my code (I did not include the methods):

    Scanner keyboard = new Scanner(System.in); 

    int radius = 0;
    int height = 0;

    System.out.print("Enter values ");
    String input = keyboard.nextLine();

    String[] items = input.split(" ");
    int[] numbers = new int[items.length];

    for (int i = 0; i < items.length; i++)  
        {
            numbers[i] = Integer.parseInt(items[i]);
        } 

    for (int i = 0; i < numbers.length/2; i++)
    {
        numbers[i] = radius;
        numbers[i + 1] = height;

        double result = area(radius);
        double result1 = area (radius,height);
        double result2 = volume (radius,height);

        System.out.println("");
        System.out.print("r = " + radius + "    ");
        System.out.print("h = " + height);
        System.out.println("");
        System.out.print("Base area:    ");
        System.out.printf("%.2f", result);
        System.out.println("");
        System.out.print("Surface area: ");
        System.out.printf("%.2f", result1);
        System.out.println("");
        System.out.print("Volym:            ");
        System.out.printf("%.2f", result2);
        System.out.println("");
    }
    keyboard.nextLine(); 

Those are the Results:

Input: 2 4 5 1

Output: 


r = 0    h = 0

Base area:        0.00

Surface area:     0.00

Volym:            0.00



r = 0    h = 0

Base area:        0.00

Surface area:     0.00

Volym:            0.00

回答1:


you must do this:

for (int i = 0; i < numbers.length; i+=2)
{
    radius = numbers[i];
    height = numbers[i + 1];
    .
    .
    .
}   



回答2:


These two lines are wrong:

numbers[i] = radius;
numbers[i + 1] = height;

radius and height should be on the left hand side:

radius = numbers[i];
height = numbers[i + 1];

Also, if you want to "group" the inputs into pairs, this won't work:

radius = numbers[i];
height = numbers[i + 1];

This will just group the input like "(0, 1), (1, 2)". Use this instead;

radius = numbers[i * 2];
height = numbers[i * 2 + 1];



回答3:


import java.util.Scanner;
public class Test {
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    int radius = 0;
    int height = 0;

    System.out.print("Enter values ");
    String input = keyboard.nextLine();

    String[] items = input.split(" ");
    int[] numbers;

    if(items.length % 2 == 0){ //Making sure you are having even number of inputs
        numbers = new int[items.length];

        for (int i = 0; i < items.length; i++) {
            numbers[i] = Integer.parseInt(items[i]);
        }

        for(int i = 0,j = -1; i < numbers.length / 2; i++){
            radius = numbers[++j];
            height = numbers[++j];
            System.out.println("radius: " + radius + " height: " + height);

            //Write Your Area Volume Code Here and use the radius and height which is there in the loop for calculation
            //Your code goes here
            //Your code ends here
        }
    }else{
        System.out.println("Incorrect input provided");
    }
    keyboard.close();
}}

Give this piece a try. The sample output is provided below:

Enter values 1 2 3 4 5 6 7 8 9 10 radius: 1 height: 2 radius: 3 height: 4 radius: 5 height: 6 radius: 7 height: 8 radius: 9 height: 10

Thanks




回答4:


Pay attention: radius and height don’t get new values after its first initialization




回答5:


try some as:

public class NewMain
{
class Crate
{
    public int radius;
    public int height;
}

public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter values ");
    String input = keyboard.nextLine();

    String[] items = input.split(" ");
    Crate[] numbers = new Crate[items.length];

    for (int i = 0; i < items.length; i ++)
    {
        numbers[i].radius = Integer.parseInt(items[i * 2]);
        numbers[i].height = Integer.parseInt(items[i * 2 + 1]);
    }
}
}



回答6:


I think you can try this solution.

The method processItems() cleans all the white-spaces between numbers and this line of code if (integers.length % 2 == 0) makes sure we have even number of inputs. So we have always a pair of numbers (Radius and Height).

I also created a class InputPair and an array of InputPair to print the pairs an maintain modularity.

You can test with this for even number of inputs:

2 9 4 7 5 6 7 1 3 8 12 5 8 2 3 9 2 9 2 7

And this for odd number of inputs:

2 9 4 5 6 7 1 3 8 12 5 8 6 3 9 2 9 2 7

You can find here the full Java code

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

    private static class InputPair {
        public int radius;
        public int height;

        public InputPair() {}

        public void printValues() {
            System.out.println("Radius: " + radius + " Height: " + height);
        }
    }

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter values ");
        String input = keyboard.nextLine();

        String[] items = input.split(" ");
        Integer[] integers = processItems(items);
        InputPair[] values = new InputPair[integers.length];

        System.out.println(Arrays.toString(items));
        System.out.println("items length:  " + items.length);

        System.out.println(Arrays.toString(integers));
        System.out.println("items length:  " + integers.length);

        if (integers.length % 2 == 0) { 
            for (int i = 0; i < integers.length / 2; i++) {
                System.out.print("Index:  " + i + " ");
                try {
                    values[i] = new InputPair();
                    values[i].radius = integers[i * 2];
                    values[i].height = integers[i * 2 + 1];
                    values[i].printValues();
                } catch (NullPointerException ne) {
                    System.out.println("Error NullPointerException");
                }

            }
        } else {
            System.out.println("The number of values bust be an even number. ");
        }
    }

    private static Integer[] processItems(String[] items) {
        List<Integer> numbers = new ArrayList<>();

        for (String s : items) {
            //Do your stuff here
            s = s.replaceAll("[^\\d]", "");
            if (!s.equals("")) {
                numbers.add(Integer.parseInt(s));
                System.out.println(Integer.parseInt(s));
            }
        }

        Integer[] stockArr = new Integer[numbers.size()];
        return numbers.toArray(stockArr);
    }
}


来源:https://stackoverflow.com/questions/43568666/java-how-to-read-multiple-scanner-values-for-one-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!