简体   繁体   中英

how to declare inputs on one line in java

I'm having trouble making my program read in all of the integers on one line rather than having to enter each number and hit enter after each digit in the terminal window.

For example, the terminal window would read:

For the our text enter the first 9 digits: 013292373"

rather than

For the our text enter the first 9 digits: 0
1
2
3
... etc"

My code looks like this so far:

/**
* This program calculates the last number of a 10 digit ISBN number.
*/  
import java.util.Scanner;
public class ISBNnum
{
public static void main(String[] args)
{
   //Variables and Scanner
   Scanner input = new Scanner(System.in);
   int dOne;
   int dTwo;
   int dThree;
   int dFour;
   int dFive;
   int dSix;
   int dSeven;
   int dEight;
   int dNine;
   int checksum;

   //Input
   System.out.print("For the our text enter the first 9 digits: ");
   dOne = input.nextInt();
   dTwo = input.nextInt();
   dThree = input.nextInt();
   dFour = input.nextInt();
   dFive = input.nextInt();
   dSix = input.nextInt();
   dSeven = input.nextInt();
   dEight = input.nextInt();
   dNine = input.nextInt();

   //Calculation
   checksum = ((dOne * 1) + (dTwo * 2) + (dThree * 3) + (dFour * 4) + (dFive * 5) + (dSix * 6) +
   (dSeven * 7) + (dEight * 8) + (dNine * 9)) % 11;

   //Output
   if (checksum == 10)
   {
       System.out.print("The whole ISBN is "+dOne+dTwo+dThree+dFour+dFive+dSix+dSeven+dEight+
       dNine+"X");
    }
    else if (checksum < 10)
    {
    System.out.println("The whole ISBN is " + dOne + dTwo + dThree + dFour + dFive + dSix +
    dSeven + dEight + dNine + " - " + checksum);
}
}
}

Thanks for your help guys!

Scanner s = new Scanner(System.in).useDelimiter("\\s*");

Adapted from :
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

Try using a scanner and splitting the String into a String[] by the spaces

import java.util.Scanner;

public class Foo {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in)
        String input = s.nextLine();
        String[] parts = input.split(" ");
        for (String part : parts) {
            System.out.println(part);
        }
    }
}

I extended my above answer with the below code. It worked when I compiled and ran it, separating nine numbers by the spaces.

public static void main(String[] args) {
    //Variables and Scanner
    Scanner input = new Scanner(System.in);
    int[] numbers = new int[9];
    int checksum = 0;

    //Input
    System.out.print("For the our text enter the first 9 digits, separated by spaces: ");
    String numInput = input.nextLine();
    String[] parts = numInput.split(" ");
    for (int i = 0; i < parts.length; i++) {
        numbers[i] = Integer.parseInt(parts[i]);
        checksum += numbers[i] * (i + 1);
    }
    //Calculation
    checksum %= 11;
}

Tracing through the code, first I create a Scanner input object, an int[] numbers with 9 indices, and an int checksum instantiated at zero. Next, I prompt the user to input nine numbers, separated by spaces, then receive the entire line with input.nextLine() , which returns the stdin up to a newline character. I then split the numInput into a String[] parts by the spaces, making everything up the first space index zero, second space is index one, etc.. I iterate through the list, changing each String representation of the numbers into int objects and place it in the numbers array at index i . I then update the checksum Integer with numbers[i] (the number just converted) and multiply by i+1 . After the loop, I modulus the checksum by 11.

The input on the terminal will look like

For the our text enter the first 9 digits, separated by spaces: 1 4 6 2 8 7 2 4 10

If you wanted to, change the line where it says String[] parts = input.split(" "); to String[] parts = input.split(","); and you could input your numbers like so:

1,4,6,2,8,7,2,4,10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM