0

The program gets an integer from the user and gets a line from user and checks if they are integer and keeps them if they are.

I want to get a line from user in a loop that checks if each value is an integer or not,and then put it into a string of characters.(I will be using the values as integer later). I tried getting the values as an integer from the first, but it still doesn't work(?)(I printed it and got 0) This is my overall work:


    package spaceship;

    import java.util.Scanner;

    public class SpaceShip {

        static int meteorNumber; //For taking the numbers of meteors in the beginning of the game.
        static String firstLine=""; //For taking the values of the first line.
        static String secondLine=""; //For taking the values of the second line.
        static int i; //Used for the loops.

        public static void gamePlace() {

            Scanner get = new Scanner(System.in); //For taking the values from user.

            System.out.println("Please enter your desired number of meteors and the first line:");
            meteorNumber = get.nextInt();

            for(i=0;i<7*meteorNumber;i++) {
                if(get.hasNextInt() || get.hasNextDouble())
                    firstLine +=  String.valueOf(i);
            }

            System.out.println(firstLine);
        }


        public static void main(String[] args) {
            gamePlace();
        }


    }

input should be something like this: enter image description here

The first line of the input in this picture is meteorNumber+firstLine. I have yet to write the code for the second line.

So for the first line,I want to keep the integer values so that I can use them later.I will probably store them in some kind of character array first,but for some reason the string doesn't take the integer values.

Elena Asi
  • 21
  • 6
  • What is the exactly purpose of the first line? I mean, you enter a number and then a line of something, and that line you want to check whether it's a number or not? In that check, char per char or the whole line? – Josep Jun 05 '20 at 19:49
  • 1
    when you post your code , try to give the sample input and output for better understanding of the question – Rishabh Ankit Jun 06 '20 at 06:42
  • I gave an example. – Elena Asi Jun 06 '20 at 17:33

1 Answers1

0

Your code has many problem First is input with Scanner See here. Second is static String firstLine; You must declare first like static String firstLine = "".

Goodluck to you @@

Puskin
  • 94
  • 1
  • 8