Printing a names in java? - java

Can you tell me how to solve that problem ?
Scanner scanner = new Scanner(System.in);
package Exercise2501;
import java.util.Scanner;
public class Friends
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("How many names do you want to enter?");
int number = scanner.nextInt();
for (int i = 0; i < number; i++)
{
System.out.println("Type a few names. ");
String names = scanner.nextLine();
}
String names = scanner.nextLine();
System.out.println(names);
}
}

package Exercise2501;
import java.util.Scanner;
public class Friends {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//Get the number of entries
System.out.println("How many names do you want to enter?");
int number = scanner.nextInt();
//create an array with that number
String names[] = new names[number];
//Ask the user to enter the Names
System.out.println("Type a few names. ");
//Store the names in the array
for (int i = 0; i < number; i++)
{
names[i] = scanner.nextLine();
}
//Do the remaining you want
}
}

Related

How do I access the String value inside a for loop?

Here I am not able to access the value of the name outside of the string even if I use other string the value is not initializing.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("\n\tWelcome to the Store");
System.out.print("\nPls enter the number of items you want to bill ");
int n = sc.nextInt();
String name;
for(int i = 1;i<=100;i++) {
System.out.print("Enter the name of the item no "+i+" ");
name = sc.next();
if (i == n) {
break;
}
}
System.out.println();
for(int m=1;m<=n;m++) {
//System.out.println(name);
}
}
You need to change name to be an array since it should contain several values.
String[] names = new String[n];
I also think you should use a while loop instead. Something like
Scanner sc = new Scanner(System.in);
System.out.println("\n\tWelcome to the Store");
System.out.print("\nPls enter the number of items you want to bill ");
int n = sc.nextInt();
String[] names = new String[n];
int i = 0;
while (i < n) {
System.out.print("Enter the name of the item no " + i + " ");
names[i] = sc.next();
i++;
}
System.out.println();
for (int m = 0; m < n; m++) {
System.out.println(names[m]);
}
Your question is not clear. But I hope this will fix it. Be sure to initialize variable n with a value that you want.
import java.util.*;
class example{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String[] name = new String[100];
int n=3; // make sure to change this one
for(int i = 1;i<=3;i++){
System.out.print("Enter the name of the item no "+i+" ");
name[i] = sc.next();
}
for(int i = 1;i<=n;i++){
System.out.print(name[i]+"\n");
}
}
}

How to read and use the String array using scanner class or other in java

How to read and use the String array using scanner class or other in java
i could read the String into array like that below
arr[0]="apple";
arr[1]="mango";
arr[2]="banana";
.
.
.
and soon
but in by using scanner class how it possible pls tell me
and any help full "for each" in java String arrays...
To read into string array and then read the populated array you could use:
public static void main(String [] args){
// to populate string array`enter code here`
Scanner sc = new Scanner(System.in);
int length = 10, pos = -1;
String arr[] = new String[length];
while(++pos < length){
System.out.print("Enter string : ");
arr[pos] = sc.nextLine();
}
// to read the already populated string array1
for(String s: arr){
System.out.println(s);
}
}
Program to read specific number of elements from the console and write them on the console.
public class Test {
public static void main(String args[]) {
System.out.println("Enter string array size");
Scanner in = new Scanner(System.in);
int i = in.nextInt();
String[] arr = new String[i];
for (int j = 0; j < i; j++) {
System.out.println("Enter next element");
arr[j] = in.next();
}
for (String s : arr) {
System.out.println("Next element[" + s+"]");
}
}
}
import java.util.Scanner;
public class StringRWrite {
public static void main(String[] args) {
Scanner sc1=new Scanner(System.in);
System.out.println("enter the length of String array");
int n=sc1.nextInt();
String[] larray=new String[n];
for(int i=0;i<n;i++){
System.out.println("enter the "+(i+1)+" String :");
larray[i]=sc1.next();
}
System.out.println("Strings Entered by user:");
for(String s:larray){
System.out.println(s);
}
}
}
import java.util.Scanner;
public class StringReadAndWrite {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the length of String array");
int n=sc.nextInt();
String[] sarray=new String[n];
for(int i=0;i<n;i++){
System.out.println("enter the "+(i+1)+" String :");
sarray[i]=sc.next();
}
System.out.println("Strings Entered by user:");
for(String s:sarray){
System.out.println(s);
}
}
}
import java.util.Scanner;
public class BasketballPlayers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter the number of basketball players");
int numberOfPlayers = input.nextInt();
String[] playersArray =new String[numberOfPlayers];
for(int i=0;i< numberOfPlayers;i++){
System.out.println("enter the "+(i+1)+" String :");
playersArray[i]= input.next();
}
System.out.println("BasketBall Player names:");
for(String s:playersArray){
System.out.println(s);
}
}
}

How to write a method that reads input from a keyboard (up to 40 numbers) and stops reading at (-999)

import java.util.Scanner;
public class Prototype {
public static void main(String args[]) {
int values = new int[40];
Scanner kbd = new Scanner(System.in);
System.out.println("Please enter 40 numbers:");
String input = kbd.nextInt();
}
}
This is the code I have at the minute and is as far as I got, my knowledge on arrays is pretty limited.
Scanner input = new Scanner(System.in);
int i =0;
while(i<40) {
int number = input.next();
if(number== -999) {
break;
}
i++;
}

print a returned string in java

I am trying to print the returned string value in the main method but I can't figure it out. can someone give me a hand?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String in_word;
int number_input;
Scanner input_word = new Scanner (System.in);
Scanner input_times = new Scanner (System.in);
System.out.println("Enter word: ");
in_word = input_word.next();
System.out.println("Enter number of times to concatenate: ");
number_input = input_times.nextInt();
multiConcat(in_word, number_input);
}
public static String multiConcat(String word, int times) {
//String word;
String s = "";
int number;//times, ;
for (number = 0; number < times; number++)
s = word.concat(s);
return s;
}
}
System.out.println(multiConcat(in_word, number_input));

How do I Store 6 integers from Scanner Console into a Set

I know this is simple. How would I take input from my console and store the input into a Set that can later be used to be returned on a Method. This is what I have so far.
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class InputConsole {
public static void main(String[] args) {
Set<Integer> s = new HashSet<Integer>(6);
int[] numbers = new int[6];
Scanner input = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.print("Please enter number ");
numbers[i] = input.nextInt();
{
}
}
}
}
I am using and Array just to test with. The Array is set to 6 so if I type 6 numbers in the console it will stop. I have instantiated the HashSet but I don't know how to go about storing the numbers from the console into it.
Use method Set::add()
for (int i = 0; i < numbers.length; i++)
{
System.out.print("Please enter number ");
s.add(input.nextInt());
}
You don't need int[] array
EDIT:
Whole main()
public static void main(final String ... args)
{
final int inputs = 6;
final Set<Integer> s = new HashSet<Integer>(6);
final Scanner input = new Scanner(System.in);
for (int i = 0; i < inputs; i++)
{
System.out.print("Please enter number #" + (i + 1) + ":");
s.add(input.nextInt());
}
System.out.println("Well done!");
System.out.println(s);
}
import java.util.*;
class Hashsetdemo
{
public static void main(String args[])
{
HashSet h=new HashSet(6);
int [] no = new int[6];
Scanner s=new Scanner(System.in);
for (int i=0;i<no.length;i++)
{
System.out.println("please enter number");
h.add(s.nextInt());
}
System.out.println(h);
}
}

Categories