2

im struggling with the below question.

Question: Input contains the number of blocks n (1 ≤ n ≤ 20) and weights of the blocks w1, …, wn (integers, 1 ≤ wi ≤ 100000) delimited by white spaces. the input should be taken from the user.

  1. how should i write the input with spaces? (lets say my input for no. of blocks is n =5 and the weights of the blocks be : 2 3 55 6 33 - because n=5)
  2. how should i read the input which are given with white spaces and store it in some list or array ? using what commands.

please find my code:

public static void main(String[] args) {
    int b1 = 0;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter no. of blocks: ");
    b1 = in.nextInt();
    if (b1<=20) {
        in.nextLine();
        int[] arr = new int[b1];
        for (int i=0; i<b1; i++) {
            System.out.println("Enter a weights of ths blocks: ");
            if (arr[i]<=100000) {
          arr[i] = in.nextInt();




            }
    }
        }

i dont think this is the right way coz the input should be delimited with spaces..

i was thinking about the ways to proceed but couldnt come up with any solution. can u please help me our on this guys. thanks.

3
  • Please paste some code on what you have tried so far, and where you are stuck. Commented Jan 10, 2015 at 19:23
  • So if you were thinking about ways to proceed please show them in your question. And by the way: What kind of input? Textfile, user input or what? If the input is a text file provide an exact example of it. Commented Jan 10, 2015 at 19:23
  • ive pasted my code. kindly help Commented Jan 10, 2015 at 19:28

4 Answers 4

7
  1. The input can be given at command line as "input1 input2 input3" (Weights separated by spaces)

  2. You can use Scanner to read the input. Consider the sample code below:

Assuming the input is only integers.

 Scanner scanner = new Scanner(System.in);
 int numOfBlocks = scanner.nextInt();
 int weightArray[] = new weightArray[numOfBlocks];
 for(int i=0;i<numOfBlocks;i++)
       {
        weightArray[i] = scanner.nextInt();
       }
 scanner.close();
//your logic
Sign up to request clarification or add additional context in comments.

2 Comments

thanks.. this is easy to understand. one small query, how does it take the numbers with space as delimiter ?
The nextInt() method takes input with space as delimiter. Say your input is "1 2 3 4". int x = scanner.nextInt() stores 1, int y = scanner.nextInt() stores 2 and so on.
3

There's a very easy way to read input delineated by spaces: use string.split(delimit):

String input = "this is a test";
String[] tokens = input.split(" ");

That'll give you an array, tokens, of "this", "is", "a", and "test".

Then you can convert to an int array like this:

int[] inputNumbers = new int[tokens.length];
for(int i = 0; i < tokens.length; i++) {
    inputNumbers[i] = Integer.parseInt(tokens[i]);
}

Just make sure that you are sure that you have numbers as an input.

4 Comments

is it the same for integer also ?
@DevDev see my edit. You need to convert the strings manually.
Yep but with different patternt \d is appropriate.
@AlexK thanks.. this is very helpful. and easy too. thanks again.
1

Here is my solution: The user enters first the number of weights of the block and then presses enter. Afterwards he enters all the weigths of the block in one line seperated by spaces (4 2 3 1) and then presses enter. The check if the single values are in correct range, you have to do by yourself.

public static void main(String[] args) {
    int b1 = 0;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter no. of blocks: ");
    b1 = Integer.parseInt(in.nextLine());
    int[] arr = new int[b1];
    if ((b1 <= 20) && (1 >= 0)) {
        System.out.println("Enter all weights of the block seperated by spaces and then press enter:");
        String readLine = in.nextLine();
        String[] weights = readLine.split(" ");
        for (int i = 0; i < b1; ++i) {
            arr[i] = Integer.parseInt(weights[i]);
        }
    }
    in.close(); //don't forget to close the resource!
}

1 Comment

If so please accept the answer. I tried it with your code so I hope you fully understand.
0
import java.util.*;
class Test
{
    public static void main(String[] args) 
    {
        Scanner in=new Scanner(System.in);
        String s[]= in.nextLine().split(" ");

        int[] a=new int[s.length];
        for(int i =0 ;i < s.length;i++){
            a[i]= Integer.parseInt(s[i]);
        }
        for(int i=0;i<s.length;i++)
        {
            System.out.println(a[i]);
        }
    }
}

1 Comment

When answering a 5 year old question with an accepted answer, it would be of benefit to everyone reading your answer if you could explain what your answer adds in comparison to the existing answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.