-2

I have a dilemma. I've created this random sentence generator script for java. And I want to show "sentence" in a text view like you would print it out to the system on line 25 in android studio.

Here's my Random Sentence Generator Code

package com.tech.littlest.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Random;
public class Activity2 extends AppCompatActivity {
    final static int NO_DEDICATED_ARRAYS = 12;  // This number is equal to the number of arrays in a string NOTE: This number MUST be constant across all strings
    final static int NO_OUTPUT_SENTENCES = 1;   // How many sentences generated and displayed for the user
    final static String SPACER = " ";   // Inputs a " " character (Space) into the display
    final static String PERIOD = ".";
    static Random r = new Random();
    public static void main( String args[] ){
        String proposition_user[] = { "You should build and invention that", "You should build a robot that", "You should create a whatchamacallit that", "There should be a device that", "Create a futuristic thing that", "Make a thing that", "Make something cool that", "I don't recall there being a thing that", "Create a sick thing that", "Devise a plan to create an object that", "I want a thing that", "If I just had an thingamajig that", "Create a thingimajig that"};
        String plural_noun[] = { "children", "chickens", "the ocean", "cars", "apples", "houses", "water", "buildings", "people", "your friends", "farms", "the human race" };
        String verb[] = { "directs you to", "drives on", "manages", "develops", "makes","eats", "helps", "relocates", "fixes", "feeds", "runs on", "washes" };
        String sentence;
        for (int i = 0; i < NO_OUTPUT_SENTENCES; i++){
            sentence = proposition_user[rand()];
            char c = sentence.charAt(0);
            sentence = sentence.replace( c, Character.toUpperCase(c) );
            sentence += SPACER;
            sentence += (verb[rand()]);
            sentence += (SPACER + plural_noun[rand()]);
            sentence += PERIOD;
            sentence += "";
            System.out.println(sentence);
        }
    }
    static int rand(){
        int ri = r.nextInt() % NO_DEDICATED_ARRAYS;
        if ( ri < 0 )
            ri += NO_DEDICATED_ARRAYS;
        return ri;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
    }
}

My XML file with my textview

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tech.littlest.myapplication.Activity2">


    <TextView
        android:id="@+id/mytextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Thanks!

Aaron Kauffman
  • 103
  • 1
  • 5
  • Long time since I used Android, but does TextView not have a setText or similar? – achAmháin Feb 06 '18 at 22:43
  • Dont use "public static void main( String args[] )", just copy the code to inside onCreate next to setContentView, then create textview reference object which you declare in xml and setText method to set the text. – Muthukrishnan Rajendran Feb 06 '18 at 22:45
  • This question was closed but i think is a different situation here the onCreate() method even doesn´t exist! – Jorgesys Feb 06 '18 at 22:55

2 Answers2

0

From what I am gathering from your code, you can get a reference to your textView by its id for example.

TextView textView = findViewById(R.id.myTextView);

you would put that in the onCreate method as that is the starting point for your app. I don't know why you have a static main method in the activity, to be honest. That code should be in a separate method that isn't main and then reference the textView in your code where you want to print and just say:

textView.setText(sentence);
arahman
  • 585
  • 2
  • 9
0

you are using this methos as entry point of your program!

 public static void main( String args[] ){
  ...
  ...
  }

if you are using android and your class is extending from AppCompatActivity you need the method

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    ...
    ...
   }

The layout loaded with setContentView() must contain the your TextView. Then get the reference and set the text:

TextView textView = findViewById(R.id.myTextView);
textView.setText(sentence);
Jorgesys
  • 124,308
  • 23
  • 334
  • 268