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));
Related
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);
}
}
}
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
}
}
I've just made this simple student program which reads the data and writes into the file.
What I need is if I want to enter 100 data of students how to make it in a list and that should be from user side
For example,
Enter students you want to enter : 2
Name : Satish devnani
Roll No : 1
Name : Sonu
Roll No : 2
And if user enters 100 then it should be 100.
made until this :
import java.util.*;
import java.io.*;
class filesatish2{
String name;
int number;
//int i=0;
public static void main(String args[]) throws IOException {
//int i;
filesatish2 stname= new filesatish2();
Students.READ();
Students.FILEWRITE();
}
int HOWMANY()
{
int count;
Scanner sc=new Scanner(System.in);
printit("How many data you want to enter ?");
count=sc.nextInt();
//printit(""+count);
return count;
}
void READ()
{
Scanner sc=new Scanner(System.in);
Scanner text=new Scanner(System.in);
printit("Name :");
name=text.nextLine();
printit("Number :");
number=sc.nextInt();
}
public static void printit(String a)
{
System.out.print(a);
}
public void FILEWRITE() throws IOException
{
File student = new File("Student.txt");
FileWriter printer = new FileWriter(student);
student.createNewFile();
printer.write(""+name);
printer.write("\t"+number);
printer.flush();
printer.close();
}
}
try the following:
import java.util.*;
import java.io.*;
class filesatish2 {
List studentList = new ArrayList();
String name;
int number;
int count = 0;
public static void main(String args[]) throws IOException {
// int i;
filesatish2 stname = new filesatish2();
stname.count = stname.HOWMANY();
stname.READ();
stname.FILEWRITE();
}
int HOWMANY() {
int count;
Scanner sc = new Scanner(System.in);
printit("How many data you want to enter ?");
count = sc.nextInt();
// printit(""+count);
return count;
}
void READ() {
Scanner sc = new Scanner(System.in);
Scanner text = new Scanner(System.in);
for (int i = 0; i < count; i++) {
printit("Name :");
name = text.nextLine();
printit("Number :");
number = sc.nextInt();
studentList.add(name + "\t" + number + "\n");
}
}
public static void printit(String a) {
System.out.print(a);
}
public void FILEWRITE() throws IOException {
File student = new File("Student.txt");
FileWriter printer = new FileWriter(student);
student.createNewFile();
for (int i = 0; i < count; i++) {
printer.write((String) studentList.get(i));
}
printer.flush();
printer.close();
}
}
My current code is as follows, I need to print the user's input using the outputWithoutWhitespace() method but I am having a hard time understanding where to apply it/how to actually use it. I believe I should be using \t. How can I take away the whitespace when printing with the outputWithoutWhitespace() method?
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
int userCount = userInput.length();
return userCount;
}
}
package whitesp;
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
String omitSpace=userInput.replace(" ","");
int userCount = omitSpace.length();
return userCount;
}
}
I am trying to write a program that takes a user's input and outputs the number of characters they typed in. I have to do this by creating a method that calculates the amount of characters, then call that method in main to output the results. I was encouraged to use a for loop, but I don't see how that would work. I can calculate the number of characters using length(), but I can't figure out how to make my method work. This is what I have so far:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = "";
System.out.println("Enter a sentence: ");
System.out.print("You entered: ");
userInput = scnr.nextLine();
System.out.println(userInput);
return;
}
public static int GetNumOfCharacters(int userCount) {
int i = 0;
String userInput = "";
userCount = userInput.length();
return userCount;
}
}
My method is not returning the length of the string, it just gives me 0 or an error.
Right now, you are never calling your "GetNumOfCharacters" method in your main. The way Java programs work, is by calling the main method and executing line per line what lies there. So you need to call you method from inside the main method. On the other hand, it should get the Stirng as a parameter, so you can get its length. It would look something like this:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = "";
System.out.println("Enter a sentence: ");
userInput = scnr.nextLine();
System.out.print("You entered: ");
System.out.println(userInput);
int lenInput = GetNumOfCharacters(userInput);
System.out.println("The length was: "+lenInput+" characters");
}
public static int GetNumOfCharacters(String userInput) {
int len = userInput.length();
return len;
}
A problem is that you are not actually calling the method
so try
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String userInput = scnr.nextLine();
System.out.print("You entered: ");
System.out.println(userInput);
System.out.println ("The length is " + GetNumOfCharacters (userInput))
}
// need to pass string into this method
public static int GetNumOfCharacters(String myString) {
int userCount = myString.length();
return userCount;
}
}
Your question included the line:
I was encouraged to use a for loop, but I don't see how that would
work.
There's no elegant way to do this in Java because you are assumed to use String.length() to get the length of strings. There is no 'end of string' marker as there is in, say, C. However you could mimic the same effect by catching the exception thrown when you access past the end of the string:
for (int len = 0; ; len++) {
try {
text.charAt(len);
} catch (StringIndexOutOfBoundsException ex) {
return len;
}
}
That's not a nice, efficient or useful piece of code but it does demonstrate how to get the length of a string using a for loop.
Problems with your code:
No Function call
Add function call in main() as int count=GetNumOfCharacters(userInput);
Parameter datatype mismatch
change the datatype in function definition from int to String as public static int GetNumOfCharacters(String userInput) {
Unwanted return statement in main()
remove the return from main()
Not displaying the value returned from GetNumOfCharacters
Add System.out.print("Number of characters: "+ count); inside main()
Code:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = "";
System.out.println("Enter a sentence: ");
System.out.print("You entered: ");
userInput = scnr.nextLine();
System.out.println(userInput);
int count=GetNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int GetNumOfCharacters(String userInput) {
int userCount = userInput.length();
return userCount;
}
OR
Function is not really needed,you can remove the function and do it like this:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = "";
System.out.println("Enter a sentence: ");
System.out.print("You entered: ");
userInput = scnr.nextLine();
System.out.println(userInput);
System.out.print("Number of characters: "+ userInput.length());
}
If you don't want to use predefined methods, you can do like this..
public static void main(String args[]){
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String userInput = scnr.nextLine();
System.out.println("You entered: "+userInput);
char a[]=userInput.toCharArray();
int count=0;
for(char c : a){
count++;
}
System.out.println("length of the string is:"+count);
}