简体   繁体   中英

Scan and put it to Array

I want scan sentence and count how many word is. And than put the sentence to Array. And print it.

It works until System.out.println("단어개수: " + count); but it doesn't work after that.

import java.util.Scanner; public class Midterm_HW00 {

public static void main(String[] args) {
    
    System.out.println("Insert sentence: ");
    
    
    Scanner scanner = new Scanner(System.in);
    int count =1;
    
    String sentence = scanner.nextLine();   //문자열 읽기    
            
    for(int i = 0; i < sentence.length(); i++) {   
        if(sentence.charAt(i)==' ') {   //단어 개수를 띄어쓰기 개수로 계산
            count++;
            
        }
    }
    System.out.println("단어 개수: " + count);

    String[] wordArray = new String[30];   //배열 선언
    int word = wordArray.length;
    
    for(int j=0; j<word; j++){
        wordArray[j] = scanner.next();            

       System.out.println("" + wordArray[j]);

    
    
    scanner.close();
   }
}

}

you can get all this by using simple method:

String[] array = yourString.split(" ");
int amountOfWords = array.length;
Scanner scanner = new Scanner(System.in);
String sentence = scanner.nextLine();

String[] words = sentence.split("[ ]+");
System.out.println("단어 개수: " + words.length);

for(String word : words){
    System.out.println(word);
}

you can use string split by a space to generate an array like this String[] wordArray = sentence.split("\\s+");

code:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Insert sentence: ");
    
    
    Scanner scanner = new Scanner(System.in);
    int count =1;
    
    String sentence = scanner.nextLine();   //문자열 읽기    
            
    for(int i = 0; i < sentence.length(); i++) {   
        if(sentence.charAt(i)==' ') {   //단어 개수를 띄어쓰기 개수로 계산
            count++;
            
        }
    }
    System.out.println("단어 개수: " + count);

    String[] wordArray = sentence.split("\\s+");   //배열 선언
    
    
    for(int j=0; j<wordArray.length; j++){       

       System.out.println("" + wordArray[j]);

   }
}
import java.util.*;
public class Main {
    
public static void main(String[] args) {
    
    System.out.println("Insert sentence: ");
            
    Scanner scanner = new Scanner(System.in);
    int count =1;
    
    String sentence = scanner.nextLine();   
            
    for(int i = 0; i < sentence.length(); i++) {   
        if(sentence.charAt(i)==' ') {   
            count++;
        }
    }
    System.out.println("단어 개수: " + count);
           
    String[] wordArray = new String[] {sentence};
    
    for(int j=0; j<count; j++){
       System.out.println("" + wordArray[j]);
       scanner.close();
   }
}

}

import java.util.*;

public class Solution {
   public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        String sentence = sc.nextLine();
        // finding the number of word
        for (int i=0; i<sentence.length(); i++) {
            if (sentence.charAt(i) == ' ')
                count += 1;
        }
        System.out.println("The count is : " + count);
        // store the sentence to a string array using split(split by the space)
        String [] sentenceToArray = sentence.split(" ");
        // print all elements inside the array
        for (int i=0; i<sentenceToArray.length; i++)
             System.out.print(sentenceToArray[i]);
   }
}

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