Jump to content

User's Input in Arrays

PugoOfficial

Greetings Everyone!

I have a hard time creating a program where the programmer makes the user input elements or numbers into the program and then displaying it
for example the user will input 1 2 3 4 5 then the program must print or show 1 2 3  4 5


Thanks in Advance Guys!

 

Edit: I'm using JCreator LE (JAVA)

CPU: Intel Core i7-4790K @ 4.0GHz | COOLING: Corsair H100i Liquid Cooler | MOTHERBOARD: ASUS Maximus VII Formula ATX | MEMORY: Corsair Vengeance Pro Series 16GB (2 x 8) DDR3-1866 | STORAGE: Intel 730 Series 480GB SSD + Seagate Barracuda 3TB HDD | PSU: Corsair AX860i 80+ Platinum | GPU: ASUS GeForce GTX 780Ti DirectCU II (2-Way SLI) | CASE: Phanteks Enthoo Luxe (Black) | DISPLAY: ASUS PB278Q 27.0" (2560 x 1440) | KEYBOARD: Razer BlackWidow Chroma | MOUSE: Razer Deathadder Chroma | SOUND: Logitech Z906 5.1 Speakers / Razer Kraken Chroma | OS: Microsoft Windows 8.1 (64-bit)

Link to comment
Share on other sites

Link to post
Share on other sites

what language?

CPU: Intel Core i5 4690K @ 4.6Ghz CPU Cooler: Noctua NH-D15 GPU: GTX 1070 TI RAM: Crucial Ballistix Tactical 16GB (4x4) Mobo: ASUS Z97-PRO(Wi-Fi ac) PSU: Corsair RM Series RM750 Case: Fractal Design Define R4 no window

Link to comment
Share on other sites

Link to post
Share on other sites

what language?

Ooops! Sorry I forgot to put in the language >.< it's Java I'm using JCreator LE my bad :)

CPU: Intel Core i7-4790K @ 4.0GHz | COOLING: Corsair H100i Liquid Cooler | MOTHERBOARD: ASUS Maximus VII Formula ATX | MEMORY: Corsair Vengeance Pro Series 16GB (2 x 8) DDR3-1866 | STORAGE: Intel 730 Series 480GB SSD + Seagate Barracuda 3TB HDD | PSU: Corsair AX860i 80+ Platinum | GPU: ASUS GeForce GTX 780Ti DirectCU II (2-Way SLI) | CASE: Phanteks Enthoo Luxe (Black) | DISPLAY: ASUS PB278Q 27.0" (2560 x 1440) | KEYBOARD: Razer BlackWidow Chroma | MOUSE: Razer Deathadder Chroma | SOUND: Logitech Z906 5.1 Speakers / Razer Kraken Chroma | OS: Microsoft Windows 8.1 (64-bit)

Link to comment
Share on other sites

Link to post
Share on other sites

That's best done with an array as far as I know.

*also my java skills are not that great but this definitely works, if you need any more java answers feel free to shoot a PM

package ltt;import java.util.Scanner; //this is really important! you can't read input without this/** * * @author Tycho */public class Ltt {    static Scanner sc = new Scanner(System.in); //this is the initialisation of the scanner that will read the input    /**     * @param args the command line arguments, ignore this for now     */    public static void main(String[] args) {                //initialisation of our variables        int amount;        int TheArrayWeAreUsing[];                //start of the program        System.out.print("How many numbers do you want the array to cointain? "); // asking how many numbers we want        amount = Integer.parseInt(sc.nextLine()); //reading the amount of numbers we want        TheArrayWeAreUsing = new int[amount]; //creating a new instance of the array                //now we are going to fill up the array with the amount of numbers we specified earlier        for (int i = 0; i < amount; i++) {            System.out.print("Give number " + (i + 1) + ": "); //asking for the number we want the user to give, i is the current number the user is putting in            TheArrayWeAreUsing[i] = Integer.parseInt(sc.nextLine()); // assinging the input to the i spot of the array        }        //generating the output        for (int j = 0; j < TheArrayWeAreUsing.length; j++) {            System.out.println("Number " + (j + 1) + " from the array is: " + TheArrayWeAreUsing[j]);        }        //end of program    }}

CPU: Intel Core i5 4690K @ 4.6Ghz CPU Cooler: Noctua NH-D15 GPU: GTX 1070 TI RAM: Crucial Ballistix Tactical 16GB (4x4) Mobo: ASUS Z97-PRO(Wi-Fi ac) PSU: Corsair RM Series RM750 Case: Fractal Design Define R4 no window

Link to comment
Share on other sites

Link to post
Share on other sites

-nom-

Hmm, in general I feel like the user should just keep entering input, then type in a keyword to end the sequence and allow program to display it.

I'm sh!t at java but you could simply replace the asking for amount and for loop with a do-while(input != keyword)

lastly, @PugoOfficial is that an assignment by any chance?

Link to comment
Share on other sites

Link to post
Share on other sites

Hmm, in general I feel like the user should just keep entering input, then type in a keyword to end the sequence and allow program to display it.

I'm sh!t at java but you could simply replace the asking for amount and for loop with a do-while(input != keyword)

lastly, @PugoOfficial is that an assignment by any chance?

Hi! @LePawel

No it isn't an assignment we took an laboratory exam yesterday and fortunately I passed good thing the question\problem that was given to me was easy

 

 

That's best done with an array as far as I know.

*also my java skills are not that great but this definitely works, if you need any more java answers feel free to shoot a PM

package ltt;import java.util.Scanner; //this is really important! you can't read input without this/** * * @author Tycho */public class Ltt {    static Scanner sc = new Scanner(System.in); //this is the initialisation of the scanner that will read the input    /**     * @param args the command line arguments, ignore this for now     */    public static void main(String[] args) {                //initialisation of our variables        int amount;        int TheArrayWeAreUsing[];                //start of the program        System.out.print("How many numbers do you want the array to cointain? "); // asking how many numbers we want        amount = Integer.parseInt(sc.nextLine()); //reading the amount of numbers we want        TheArrayWeAreUsing = new int[amount]; //creating a new instance of the array                //now we are going to fill up the array with the amount of numbers we specified earlier        for (int i = 0; i < amount; i++) {            System.out.print("Give number " + (i + 1) + ": "); //asking for the number we want the user to give, i is the current number the user is putting in            TheArrayWeAreUsing[i] = Integer.parseInt(sc.nextLine()); // assinging the input to the i spot of the array        }        //generating the output        for (int j = 0; j < TheArrayWeAreUsing.length; j++) {            System.out.println("Number " + (j + 1) + " from the array is: " + TheArrayWeAreUsing[j]);        }        //end of program    }}

Thanks @Tycho for the effort and time of the program you created even though it wasn't used in the question\problem given to me in my laboratory exam but you know better prepared than be a potato

CPU: Intel Core i7-4790K @ 4.0GHz | COOLING: Corsair H100i Liquid Cooler | MOTHERBOARD: ASUS Maximus VII Formula ATX | MEMORY: Corsair Vengeance Pro Series 16GB (2 x 8) DDR3-1866 | STORAGE: Intel 730 Series 480GB SSD + Seagate Barracuda 3TB HDD | PSU: Corsair AX860i 80+ Platinum | GPU: ASUS GeForce GTX 780Ti DirectCU II (2-Way SLI) | CASE: Phanteks Enthoo Luxe (Black) | DISPLAY: ASUS PB278Q 27.0" (2560 x 1440) | KEYBOARD: Razer BlackWidow Chroma | MOUSE: Razer Deathadder Chroma | SOUND: Logitech Z906 5.1 Speakers / Razer Kraken Chroma | OS: Microsoft Windows 8.1 (64-bit)

Link to comment
Share on other sites

Link to post
Share on other sites

Long way short, two things. To save input, just use an ArrayList. An ArrayList is just like an array, but it can be infinitely expanded and you don't have to tell it how many objects are going inside it.

ArrayList<String> input = new ArrayList<String>();

Then, you want to add your user input to the ArrayList.

input.add(<user input>);

Then, when you're done collecting user input, you would just list it out. I'll use a for loop for this.

for(int i = 0; i < input.size; i++){    System.out.println(input.get(i)); // or however else you'd output it}

I might be wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×