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);
}
}
}
Related
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");
}
}
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());
}
}
}
}
I'm writing a dictionary for Vietnamese but my Treeset just add 1 object. I've been searching for 2 days but i cant figure it out how. hope you guys help me.
public class Word implements Comparable<Word> {
private static String word_target, word_explain;
public static void setWord_target(String word_target) {
Word.word_target = word_target;
}
public static void setWord_explain(String word_explain) {
Word.word_explain = word_explain;
}
public String getWord_explain() {
return word_explain;
}
public String getWord_target() {
return word_target;
}
#Override
public int compareTo(Word word) {
return this.getWord_target().compareTo(word.getWord_target());
}
}
public class Dictionary {
private TreeSet<Word> words = new TreeSet<Word>();
public TreeSet<Word> getWords() {
return words;
}
}
public class DictionaryManagement {
static Scanner reader = new Scanner(System.in);
public static int numbers;
public static void insertFromCommandline(Dictionary dic) {
numbers = reader.nextInt();
reader.nextLine();
for (int i = 0; i < numbers; i++) {
Word putInWord = new Word();
String en_word, vn_word;
System.out.print("English Word: ");
en_word = reader.nextLine();
putInWord.setWord_target(en_word);
System.out.print("VietNameses Word: ");
vn_word = reader.nextLine();
putInWord.setWord_explain(vn_word);
dic.getWords().add(putInWord);
}
}
}
public class DictionaryCommandline {
private static int num = 1;
public static Dictionary showWord = new Dictionary();
public static void showAllWords() {
System.out.println("No |English |Vietnamese");
for (Word wr : showWord.getWords()) {
System.out.println( num++ + " |" + wr.getWord_target() + " |" + wr.getWord_explain());
}
}
public static void dictionaryBasic() {
DictionaryManagement.insertFromCommandline(showWord);
DictionaryCommandline.showAllWords();
}
}
public class Main {
public static void main(String []args) throws Exception {
DictionaryCommandline.dictionaryBasic();
}
}
Example:
Input:
2
English Word:
house
VietNameses Word:
ngoi nha
English Word:
name
VietNameses Word:
ten
-Actual Output:
No English Vietnam
1 name ten
-Expected Output:
No English Vietnam
1 house ngoi nha
2 name ten
#Huy, note that you are using static variables, try using only instance variables for your Word, instances.
This makes your compareTo method always compare the latest words you inserted because static variables are associated only with a class, representing a single value/instance at a time.
Take a look here for a few more words on static # java
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);
}
I ran my code... there was an error on line 28. it's the one that says
hey.remove(whatnumber);
I can't figure out what's wrong with it. I tried using debug, but
I don't get how to use it.
Here is the code.
import java.util.ArrayList;
import java.util.Scanner;
public class ACSL_Grid_Fit {
public static void main(String args[]) {
run();
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
}
public static ArrayList<Integer> hey = new ArrayList<Integer>();
public static int howmany;
public static int whatnumber;
public static int choice;
public static Scanner sc = new Scanner(System.in);
public static void run() {
input();
countcalc();
}
public static void input() {
{
sc.useDelimiter(", |\n");
howmany= sc.nextInt();
for(int x = 1; x<=howmany;x++) {
whatnumber = sc.nextInt();
hey.remove(whatnumber);
}
}
choice = sc.nextInt();
switch(choice) {
case 1:
int i = 0;
while(i<hey.get(0)) {
i++;
}
System.out.println(i);
hey.remove(i);
case 2:
case 3:
}
}
public static void countcalc() {
}
}
To avoid an IndexOutOfBoundsException, you need to add values to list before you try and run()
And with this code here, you have to keep in mind there is no index 0
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
You are starting the index at 1, so 0 is null;
Edit: I just caught this
You're trying to run() before you even have any values in your list
public static void main(String args[]) {
run();
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
}
Just switch them around
public static void main(String args[]) {
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
run();
}
EDIT: I knew this was a problem to begin with
hey.add(w,w);
Your trying to add at index w when no index w exists. hey has a size of 0, until you add to it.
Just do this.
for(int w=1;w<=25;w++) {
hey.add(w);
}
When you want to remove the numbers use this
hey.remove(whatnumber - 1)
// example, since 20 is at index 19, you want to remove whatnumber - 1