Illegal start of expression2 - java

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());
}
}
}
}

Related

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

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");
}
}

Cannot Find Symbol Error when creating an object

I am creating an ArrayList object that contains a class. However, when I call a method from the WordList() class, it says "error: cannot find symbol obj.numWordsOfLength(2)"
import java.util.ArrayList;
public class WordList{
public static void main(String []args){
ArrayList obj = new ArrayList<WordList>();
obj.add("bird");
obj.add("wizard");
obj.add("to");
obj.numWordsOfLength(2);
}
private ArrayList<String> myList;
public WordList(){
myList = new ArrayList<String>();
}
public int numWordsOfLength(int len){
//Code
}
public void removeWordsOfLength(int len){
//Code
}
}
When you call obj.numWordsOfLength(2); you're calling the method numWordsOfLength from ArrayList (which does not exist), not from your WordList class.
First of all, you are adding String to ArrayList, not you're WordList object.
I think what you're trying to achieve will look more like this:
public class WordList {
private List<String> wordList = new ArrayList<>();
public static void main(String[] args) {
WordList wordList = new WordList();
wordList.add("bird");
wordList.add("wizard");
wordList.add("to");
wordList.numWordsOfLength(2);
}
public void add(String word) {
wordList.add(word);
}
public int numWordsOfLength(int len) {
//Code
}
public void removeWordsOfLength(int len) {
//Code
}
}

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);
}
}
}

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);
}

Setting ArrayList items in one Class from another Class

There is a foo class with an ArrayList of double msg called msgstoboo as well as a method setMsg(int index, double input) to alter individual messages in msgstoboo.
There is a networkoffoos class with an ArrayList of foo objects called listoffoos. There is an updatefoomsg method:
public void updatefoomsg (ArrayList<ArrayList<Foo>> Foonetwork)
{
for(int foolayer = 0; foolayer< foonetwork.size(); foolayer++)
for(int fooinlayer = 0; fooinlayer< foonetwork.get(foolayer).size(); fooinlayer++)
for(int msginfoo = 0; msginfoo < foonetwork.get(foolayer).get(fooinlayer).msgstoboo.size(); msginfoo++)
Foonetwork.get(foolayer).get(fooinlayer).setMsg(msginfoo,somerandomvalue)
}
The goal of updatefoomsg is to change the values of individual msgs in msgstooboo. However, no values in the foo class ArrayList `msgstoboo' are altered. Why is this and how do I fix it? Thank you in advance.
UPDATE: Here are the whole foo and networkoffoos classes
public class foo
{
ArrayList<Double> msgstoboo = new ArrayList<Double>(Double);
public foo(int numofmessages)
{
for (int i = 0; i < numofmessages; i++)
{
msgstoboo.add(1);
}
}
public void setMsg(int index, double input)
{
msgstoboo.set(index,input);
}
&&
public class networkoffoos
{
ArrayList<ArrayList<foo>> foonetwork = new ArrayList<ArrayList<foo>>();
public void networkoffoos(int numoffoos)
{
for(int i = 0; i < numoffoos; i++)
foonetwork.add(new foo(somenumberofmsgs))
}
//**AND THE "updatefoomsg" method included in this post**
}
The following works for me. Made some small changes because your code wouldn't compile. Hope it helps.
import java.util.ArrayList;
public class test {
private static ArrayList<ArrayList<Foo>> foonetwork = new ArrayList<ArrayList<Foo>>();
public static void main(String[] args){
networkoffoos(5);
updatefoomsg(foonetwork);
}
public static void networkoffoos(int numoffoos) {
for(int i = 0; i < numoffoos; i++) {
ArrayList<Foo> fooArrayList = new ArrayList<Foo>();
fooArrayList.add(new Foo(10));
foonetwork.add(fooArrayList);
}
}
public static void updatefoomsg (ArrayList<ArrayList<Foo>> foonetwork) {
for(int foolayer = 0; foolayer< foonetwork.size(); foolayer++)
for(int fooinlayer = 0; fooinlayer< foonetwork.get(foolayer).size(); fooinlayer++)
for(int msginfoo = 0; msginfoo < foonetwork.get(foolayer).get(fooinlayer).msgstoboo.size(); msginfoo++)
foonetwork.get(foolayer).get(fooinlayer).setMsg(msginfoo,3);
}
}
and your Foo class
import java.util.ArrayList;
public class Foo {
ArrayList<Double> msgstoboo = new ArrayList<Double>();
public Foo(int numofmessages) {
for (int i = 0; i < numofmessages; i++) {
msgstoboo.add(Double.valueOf(1));
}
}
public void setMsg(int index, double input) {
msgstoboo.set(index, input);
}
}

Categories