Currently trying to print out Reversal class while using command line arguments - java

I've been trying multiple different ways but can't seem to figure out how to get the string reversal to work in command line.
public class Reversal {
static void reverseChar(String input) {
char output[] = new char[input.length()];
int i = input.length();
while (i > 0) {
output[++i] = input.charAt(i);
}
}
public static void main(String[] args) {
}
}

Use the following code.
import java.util.*;
public class Reversal {
static void reverseChar(String input) {
char output[] = new char[input.length()];
int i = input.length();
int count=0;
while (i > 0) {
output[count++] = input.charAt(--i);
}
System.out.println(Arrays.toString(output));
}
public static void main(String[] args) {
reverseChar("Tharindu");
}
}

Related

i want print 1 to n number by help of recursion in java

public class Main {
public static void main(String[] args) {
print(10);
}
static void print(int s)
{
if (1==s) {
System.out.print(s);
}
System.out.print(s);
print(s-1);
}
}
But I want output like this:
12345678910
You existing recursion never ends. You should only make the recursive call if s >= 1.
And to print the numbers in increasing order, you need to first make the recursive call and then print the current number:
static void print(int s)
{
if (s >= 1) {
print(s-1);
System.out.print(s);
}
}
Here, try this.
public static void main(String[] args) {
print(1, 10);
}
static void print(int startValue, int endValue)
{
System.out.print(startValue);
if(startValue < endValue)
print(startValue+1, endValue);
}
Solution 1:- If you want the end result-oriented then you can try this
public static int doCalculaton(int numVal, int endVal) {
System.out.println(endVal - numVal);
if (numVal == 0) {
return numVal;
}
return doCalculaton(numVal - 1, endVal);
}
public static void main(String[] args) {
int toBeCalculateNum = 10;
doCalculaton(toBeCalculateNum, toBeCalculateNum);
}
Solution 2:- If you want to use the result after calling
public static int doCalculaton(int numVal, ArrayList<Integer> numerList) {
numerList.add(numVal);
if (numVal == 0) {
return numVal;
}
return doCalculaton(numVal - 1, numerList);
}
public static void main(String[] args) {
ArrayList<Integer> numerList = new ArrayList<>();
doCalculaton(10, numerList);
Collections.reverse(numerList);
System.out.println(numerList);
// TODO loop and do whatever you want to do
}

Illegal start of expression2

I am trying to write a method to capitalize words ending with an "s" in an arrayList
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("speech.txt"));
ArrayList<String> myList = new ArrayList<String>();
public void input() {
while (input.hasNext()) {
myList.add(input.next());
}
}
public void capPlurals() {
for (int i = 0; i < myList.size(); i++) {
String element = myList.get(i);
if (element.endsWith("s")) {
System.out.println(element.toUpperCase());
}
}
}
}
Test.java:12: error: illegal start of expression
public void input() {
^
Just remove those useless method declarations inside your main() method - as mentioned in the comments you cannot define methods inside other methods.
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("speech.txt"));
ArrayList<String> myList = new ArrayList<String>();
while (input.hasNext()) {
myList.add(input.next());
for (int i = 0; i < myList.size(); i++) {
String element = myList.get(i);
if (element.endsWith("s")) {
System.out.println(element.toUpperCase());
}
}
}
}
}
You can't declare a method inside another method. Declare them outside the main and pass proper arguments.
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("speech.txt"));
ArrayList<String> myList = new ArrayList<>();
addToList(input, myList);
capPlurals(myList);
}
public static void addToList(Scanner input, ArrayList<String> myList) {
while (input.hasNext()) {
myList.add(input.next());
}
}
public static void capPlurals(ArrayList<String> myList) {
for (int i = 0; i < myList.size(); i++) {
String element = myList.get(i);
if (element.endsWith("s")) {
System.out.println(element.toUpperCase());
}
}
}
}

Java string array with setter getter

I wanted to create a simple program for user to insert 3 strings to a private string array in a class and then print it back by creating a new object using object reference but I think I am facing problem in the setter/getter.(Pretty new to class and setter/getter) Here is what I have so far:
import java.util.Scanner;
public class Stringtest {
public static void main(String[] args)
{ Scanner input=new Scanner(System.in);
Stringer Strung=new Stringer();
System.out.println("Strings:"+Strung.print());
}
}
class Stringer
{ Scanner input=new Scanner(System.in);
private String[] aa=new String[3];
aa[0]="zero";
aa[1]="one";
aa[2]="two";
Stringer()
{}
{ System.out.println("Please enter 3 strings:");
for(int i=0;i<4;i++)
{
aa[i]=input.next();
}
}
public void setaa(String[] a)
{
aa=a;
}
public String[] getaa()
{
return aa;
}
public void print(String[] a)
{
for(int b=0;b<4;b++)
{
System.out.printf("%s",a[b]);
}
}
}
Due to populating the array while creating a class instance, you don't require any setters. The only getter requires.
Divide the logic from the runner.
Always use array.length() while looping or use a simple for loop otherwise you'll be getting an indexOfBoudException error.
Didn't get why you are using printf() while printing results.
My solution:
import java.util.Scanner;
public class App {
public static void main(String[] args) {
App.run();
}
private static void run() {
Stringer stringer = new Stringer();
stringer.print(stringer.getStrings());
}
}
class Stringer {
private String[] strings = new String[3];
Stringer() {
System.out.println("Please enter 3 strings:");
for (int i = 0; i < 4; i++) {
Scanner scanner = new Scanner(System.in);
strings[i] = scanner.next();
}
}
String[] getStrings() {
return strings;
}
void print(String[] strings) {
System.out.println("Strings are:");
for (String string : strings) {
System.out.println(string);
}
}
}

Write out the table in other method

Hey i want to write out table in second method.
In first i changed int x into table(each digit in other array index) and in second method i want to write out the table. How to do it ?
int tab [] =new int [4];
public int[] change(int x) {
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
System.out.println(tab.toString());
return tab;
}
public void writeout(int a[]) {
this.tab=a;//how to connect tab from change() with int a[]
for( int i=0;i<=3;i++) {
System.out.println(a[i]);
}
You can use newest Java 8 API to print your array. Your code may look like this:
import java.util.Arrays;
public class App {
int tab[] = new int[4];
public int[] change(int x) {
tab[0] = x/1000;
tab[1] = (x/100)%10;
tab[2] = (x/10)%10;
tab[3] = x%10;
return tab;
}
public void writeout(int array[]) {
Arrays.stream(array).forEach(System.out::println);
}
public static void main(String[] args) {
App app = new App();
app.writeout(app.change(2));
}
}
I fiddled with it. It appears to work after adding a closing brace at the end. tab.toString() doesn't result in sensible results though.
public class MyClass {
public static void main(String args[]) {
MyClass c = new MyClass();
c.writeout(c.change(3));
}
public int[] change(int x) {
int tab [] =new int [4];
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
System.out.println(tab.toString());
return tab;
}
public void writeout(int a[]) {
for( int i=0;i<=3;++i) {
System.out.println(a[i]);
}
}
}
If you want to use class fields, then you could do it like this:
public class MyClass {
public static void main(String args[]) {
MyClass c = new MyClass();
c.change(3);
c.writeout();
}
private int tab [] = new int [4];
public void change(int x) {
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
}
public void writeout() {
for( int i=0;i<=3;i++) {
System.out.println(tab[i]);
}
}
}

Is it possible in java to get multiple inputs in a single line??

I am trying to get multiple inputs in a single code of line..
for example in c++, we could have it like -
int a,b,c;
cin>>a>>b>>c;
is it possible in java also??
You can use an array for this purpose, like:
public static void main(String[] args) {
int[] values = new int[3];
Scanner in = new Scanner(System.in);
for(int i = 0; i < values.length; i++) {
values[i] = in.nextInt();
}
System.out.println(Arrays.toString(values));
}
UPDATE 2
In java 8 the above solution can have a shorter version:
Scanner in = new Scanner(System.in);
Integer[] inputs = Stream.generate(in::nextInt).limit(3).toArray(Integer[]::new);
UPDATE 1
There is another way, which is closer to cin:
public class ChainScanner {
private Scanner scanner;
public ChainScanner(Scanner scanner) {
this.scanner = scanner;
}
public ChainScanner readIntTo(Consumer<Integer> consumer) {
consumer.accept(scanner.nextInt());
return this;
}
public ChainScanner readStringTo(Consumer<String> consumer) {
consumer.accept(scanner.next());
return this;
}
}
public class Wrapper {
private int a;
private int b;
private String c;
public void setA(int a) {
this.a = a;
} /* ... */
}
public static void main(String[] args) {
ChainScanner cs = new ChainScanner(new Scanner(System.in));
Wrapper wrapper = new Wrapper();
cs.readIntTo(wrapper::setA).readIntTo(wrapper::setB).readStringTo(wrapper::setC);
System.out.println(wrapper);
}

Categories