how to take input an array[] of StringBuffer in java - java

I wanted to take a input an array of StringBuffer for user in java but it doesn't work properly:
public class A {
public static void main(String[] args) {
B obj =new B();
Scanner sc= new Scanner(System.in);
int l;
System.out.println("enter the length of string ");
l=sc.nextInt();
StringBuffer sb[]=new StringBuffer[l];
for(int i=0;i<sb.length;i++)
{
System.out.println("enter a string "+(i+1) +" : ");
sb[i]=sb[i].append(sc.nextLine()); // in this line they will give error
}
obj.inputstring( sb);
}

You need to initialize StringBuffer object for each and every sb[i] location.
sb[i]=new StringBuffer(sc.nextLine());
Refer javadoc for Array initialization

Try this.
public static void main(String[] args) {
//B obj =new B();
Scanner sc = new Scanner(System.in);
int l;
System.out.println("enter the length of string ");
l = sc.nextInt();
sc.nextLine();
StringBuffer sb[] = new StringBuffer[l];
System.out.println("sb length==" + sb.length);
for (int i = 0; i < sb.length; i++) {
System.out.println("enter a string " + (i + 1) + " : ");
sb[i] = new StringBuffer(sc.nextLine()); // use StringBuffer(value)
}
for(int j=0;j<sb.length;j++){
System.out.println("string "+j+"="+sb[j]);
}
//for(int j=0;j<=sb.length;j++){}
//System.out.println("sb=="+sb.toString());
}

Related

I can't add a string to itself

I'm new to java and trying to add a string to itself (plus other strings also) and it runs but doesn't do anything at all, as in it just outputs "test", which is what it is before
everything else seems to work
package chucknorris;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input string:");
String input = scanner.nextLine();
int length = input.length();
String output = "test";
for (int current = 0;current <= length;current++) {
String letter = input.substring(current, current);
output = output + letter + " ";
if (current == length) {
System.out.println(output);
}
}
}
}
Try this Solution, but you should use StringBuilder if you want to edit a String for a multiple times
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input string:");
String input = scanner.nextLine();
int length = input.length();
String output = "test";
for (int current = 0;current <= length;current++) {
if (current >= length) {
break;
}
String letter = input.substring(current, current + 1);
output = output + letter;
}
System.out.println(output);
}
}
use concat for the string concatenation.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input string:");
String input = scanner.nextLine();
int length = input.length();
String output = "test";
output = output.concat(output).concat(input).concat("");
System.out.println(output);
}

Why character in StringBuild does not change?

The character must be entered from the console to change to lowercase letters on this line. But it displays the same word and the symbol does not change.
public class Task {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(requestString());
char symbol = requestSymbol().charAt(0);
int count = 0;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == symbol) {
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
count++;
}
}
System.out.println("Number of entries: " + count);
System.out.println("Converted string: " + sb);
}
static String requestString() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter string:");
return scanner.nextLine();
}
static String requestSymbol() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the symbol:");
return scanner.next();
}
}
It seems to be a problem with the line:
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
It should be:
sb.setCharAt(i, Character.toUpperCase(symbol));

Need help for get string in java

I want get first name when input the name with array and looping
Example :
Enter the name :
1. Alvin Indra
2. Sada Rika
Output :
1. Alvin
2. Sada
Code:
public static void main(String[] args) {
int n;
String teman[],namadepan[];
Scanner sc = new Scanner(System.in);
Scanner x = new Scanner(System.in);
System.out.print("Put how many friends : ");
n = sc.nextInt();
teman = new String[n];
namadepan = new String[n];
for(int i=0;i<n;i++){
System.out.print("Friend Of-"+(i+1)+" : ");
teman[i] = x.nextLine();
}
System.out.print("\n");
System.out.println("First Name : ");
for(int i=0;i<n;i++){
if(teman[i] == ' '){ // this is where I need help
System.out.println((i+1)+". "+teman[i].substring(0,i));
}
}
}
Here you go
String name = "John Smith";
System.out.println(name.split(" ")[0]);
Using this will replace that second for loop, you don't need to cycle through each letter to look for a space you can just use the split method on the string and specifiy the character in which to split the string up and then call the first element.
Updated complete implementation
import java.util.Scanner;
public class testest {
public static void main(String[] args){
int n;
String teman[],namadepan[];
Scanner sc = new Scanner(System.in);
Scanner x = new Scanner(System.in);
System.out.print("Put how many friends : ");
n = sc.nextInt();
teman = new String[n];
namadepan = new String[n];
for(int i=0;i<n;i++){
System.out.print("Friend Of-"+(i+1)+" : ");
teman[i] = x.nextLine();
}
System.out.print("\n");
for(int i=0;i<n;i++){
System.out.println("First Name : ");
System.out.println(teman[i].split(" ")[0]);
System.out.print("\n");
}
}
}
You are probably looking for using contains and indexOf methods of String class in java, to use them as :
if (teman[i].contains(" ")) {
System.out.println((i + 1) + ". " + teman[i].substring(0, teman[i].indexOf(" ")));
}

Resource leak: 'sc' is never closed

Program to count how many times a particular character, letter or number occur in a sentence.
However I keep getting message:
Resource leak: 'sc' is never closed
I am using Java and Eclipse. What should I do?
import java.util.Scanner;
class Number-count {
public static void number - count(String args[]) {
String s;
char ch;
int count = 0;
Scanner SC = new Scanner(System. in );
System.out.println("Enter a sentence");
String str = sc.nextLine();
System.out.println("Enter a character to be searched for occurence");
s = sc.nextLine();
char c = s.charAt(0);
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if (ch == c) {
count++;
}
}
System.out.println("Character " + c + " occur " + count + " times");
}
}
Scanner objects need to be closed after one is done using them. So, after you're done with it you should call the following before the end of your main method
SC.close();
after your scanner work completed put: sc.close();
It is working 100%
Try this code
public static void number - count(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
try{
//your code
}
finally {
sc.close();
}
}
If you want to use the scanner globally in a class(which is the case sometimes)
try this:
static Scanner sc = new Scanner(System.in);
/* Making it easy for beginners, when we use Scanner sc it is required to be close once we have taken all inputs from user, to close use sc.close(); */
package basicjava;
import java.util.*;
public class sumbyuser {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your numbers for summation : ");
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();
int sum = a+b;
System.out.println("Summation is : "+sum);
}
}
Try sc.close();
After the using the scanner inputs :
import java.util.*;
public class Func1 {
public static void CalculateSum() {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();
int sum = a + b;
System.out.println("The sum is " + sum);
}
public static void main(String[] args) {
CalculateSum();
}
}

For looping to add variables for a Scanner input

So I'm new to Java and I figured I'd do something simple like a for loop to print out an array of strings or something,
My code ended up like this:
package package.four;
import java.util.Scanner;
public class arrayrecurse {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Please enter 5 words");
String a = in.next();
String b = in.next();
String c = in.next();
String d = in.next();
String e = in.next();
String[] s = {a, b, c, d, e};
for(int i = 0; i< s.length;){
System.out.println(s[i]);
i++;
}
in.close();
}
}
It works fine but my question is if it's possible to make a for loop cycle through variables.
For examples if I wanted something like:
for(words = 5; words > 0;){
String a = in.next();
a++}
Where would it change the variables each time I enter a new word.
Would it be possible to do something like that or do I need to type out the String variable = in.next(); every time I want to enter a new word input from the console?
You can call next() inside the loop, but you need to declare the variable outside the loop if you want to use it afterwards, also, there is no ++ operator for String or array in Java:
String[] inputs = new String[5];
for (int i = 0; i < inputs.length; ++i)
{
inputs[i] = in.next();
}
Use an ArrayList to store the input variables.
That is:
import java.util.*;
public static void main(String[] args) {
List<String> inputVars = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
inputVars.add(sc.next());
}
for (String s: inputVars)
{
System.out.println(s);
}
}
Or alternatively, if you want to change the contents of the ArrayList:
public static void main(String[] args) {
List<String> inputVars = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
inputVars.add(sc.next());
}
for (int i = 0; i < inputVars.size(); i++)
{
System.out.println(inputVars.get(i));
//Change the variable
inputVars.set(i, "Hello, " + inputVars.get(i));
}
}

Categories

Resources