Question

I am trying to take a string of numbers seperated by a single space and covert...

I am trying to take a string of numbers seperated by a single space and covert them into a string array. I have created the following code but it only works if the numbers are seperated a a comma or something similar to that.

Example of what I am trying to achieve:

string input = "1 1 1 1 1" turn it into.... int[] x = {1,1,1,1} so that it is printed as... [1, 1, 1, 1]   

This is the code I have so far....

public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       System.out.println("Welcome to the Guess The CodeGame!");

       System.out.println("Please enter the code you want to use wiht a ' ' in between each number");

       String x = input.next();

       String[] strArray = x.split(","); // this only works with a comma, doesnt work with a x.split(" ");

       int[] intArray = new int[strArray.length];

       for (int i = 0; i < strArray.length; i++) {

           intArray[i] = Integer.parseInt(strArray[i]);

           System.out.println(Arrays.toString(intArray));

       }

       System.out.println(Arrays.toString(intArray));

   }

Homework Answers

Answer #1

Note:

The code you wrote every thing was right.But a small change.To read the String with spaces we have to use

nextLine() method of Scanner class Object.But you used next() method.That was the only change.

next() method of Scanner class is used to read the word.

nextLine() method of Scanner class is used to read the sentence with spaces.

____________________

StringToIntArray.java

import java.util.Arrays;
import java.util.Scanner;

public class StringToIntArray {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Guess The CodeGame!");
System.out.println("Please enter the code you want to use with a ' ' in between each number");
String x = input.nextLine();
String[] strArray = x.split(" "); // this only works with a comma, doesnt work with a x.split(" ");
int[] intArray = new int[strArray.length];
for (int i = 0; i < strArray.length; i++) {
intArray[i] = Integer.parseInt(strArray[i]);

}
System.out.println(Arrays.toString(intArray));

   }

}

_______________________

Output:

Welcome to the Guess The CodeGame!
Please enter the code you want to use with a ' ' in between each number
1 1 1 1 1 1 1
[1, 1, 1, 1, 1, 1, 1]

_____________Thank You

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the range between −25 and 25 THIS IS MY CODE! please edit this...Thank you import java.util.Scanner; public class Array { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int[] integs = new int[51]; System.out.println("Enter integers 0-50 [enter -1 to quit]: "); int nums = scan.nextInt(); while (nums != -1 && nums>=0 && nums<=50) { integs[nums]++; nums = scan.nextInt(); } for...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        int departureTime = input.nextInt();        int travelTime = input.nextInt();        int arrivalTime;            departureTime =12;            departureTime = 0;            arrivalTime = (int) (departureTime + travelTime);            (arrivalTime = (arrivalTime >=12));            if (arrivalTime = arrivalTime %12)            {            System.out.println(arrivalTime);...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner...
public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner keyboard = new Scanner(System.in);        System.out.println("Enter a date in the format month/day/year");        String date = keyboard.nextLine();        //Make a copy        String dateCopy = date;               //Extract the values        //start with month        //indexOf() is used to find the index of a specified character in a givenString        int workingIndex = date.indexOf("/");...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have this much public class Main { int WIDTH = 0; double HEIGHT = 0; public static void drawSmileyFaces() { System.out.println(" :-):-):-):-):-):-)"); } public static void main (String[] args) { for (int WIDTH = 0; WIDTH < 3; WIDTH++ ) { for(double HEIGHT = 0; HEIGHT < 2; HEIGHT++) { drawSmileyFaces(); } } } } I am pretty sure that alot of this is wrong...
So I am trying to write a factor generator. User provides an integer and program needs...
So I am trying to write a factor generator. User provides an integer and program needs to return all factors. I have found an example the code that does this function, but I can't understand how it does that. Please explain line by line how this works.     System.out.print("Enter an integer: ");        int number = input.nextInt();        int index = 2; //Prompts user for an integer and assigns it to variable number, and integer index is declared...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT