Difficulty understanding classes
I am having a difficult time understanding how classes work and cant figure out how to take my currentToken make it into a word then use the word class to scramble it or not then return the word either scrambled or not into a println.
Main Class:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class Readability
{
public static void main(String args)
throws FileNotFoundException {
Scanner doc = new Scanner( new File("README.txt"));//imports README.txt
System.out.println(tokens);
}
public static ArrayList<String> punctuation(Scanner doc){//creates new method called punctuation
ArrayList<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()){//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
wordObject.toString();
if (currentToken.length()>0){//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(currentToken);
}
}
return tokens;
}
}
Word Class:
import java.util.ArrayList;
import java.util.Collections;
public class Word{
String word;
public Word(String w){
word=w;
}
public String toString(){
if (word.length()<6) {
return word;
}
else {
String first = word.substring(0,2);
int length = word.length();
String middle = word.substring(2,length-2);
ArrayList<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String last = word.substring(length-2);
return first+scrambled+last;
}
}
}
java class
|
show 4 more comments
I am having a difficult time understanding how classes work and cant figure out how to take my currentToken make it into a word then use the word class to scramble it or not then return the word either scrambled or not into a println.
Main Class:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class Readability
{
public static void main(String args)
throws FileNotFoundException {
Scanner doc = new Scanner( new File("README.txt"));//imports README.txt
System.out.println(tokens);
}
public static ArrayList<String> punctuation(Scanner doc){//creates new method called punctuation
ArrayList<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()){//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
wordObject.toString();
if (currentToken.length()>0){//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(currentToken);
}
}
return tokens;
}
}
Word Class:
import java.util.ArrayList;
import java.util.Collections;
public class Word{
String word;
public Word(String w){
word=w;
}
public String toString(){
if (word.length()<6) {
return word;
}
else {
String first = word.substring(0,2);
int length = word.length();
String middle = word.substring(2,length-2);
ArrayList<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String last = word.substring(length-2);
return first+scrambled+last;
}
}
}
java class
Is there any problem with your program (which one)?
– Henry
Nov 27 '18 at 9:04
@Henry My println is giving me an error: Cannot find symbol: variable tokens. I also am not able to tell if my current tokens are being read by the word class and scrambled at all and if those are even going to my main method in my main class.
– ic0n
Nov 27 '18 at 9:06
return first+scrambled+last; this will lead to issues, since scrambled isn't a String you are concatenating
– Stultuske
Nov 27 '18 at 9:09
@ValentinCarnupunctuation
method is static. no need to instatiateReadability
– Sharon Ben Asher
Nov 27 '18 at 9:09
@ValentinCarnu I didn't mention that at all. why on earth would a toString method return a List? Change the returntype, and you'll get compilation issues
– Stultuske
Nov 27 '18 at 9:13
|
show 4 more comments
I am having a difficult time understanding how classes work and cant figure out how to take my currentToken make it into a word then use the word class to scramble it or not then return the word either scrambled or not into a println.
Main Class:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class Readability
{
public static void main(String args)
throws FileNotFoundException {
Scanner doc = new Scanner( new File("README.txt"));//imports README.txt
System.out.println(tokens);
}
public static ArrayList<String> punctuation(Scanner doc){//creates new method called punctuation
ArrayList<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()){//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
wordObject.toString();
if (currentToken.length()>0){//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(currentToken);
}
}
return tokens;
}
}
Word Class:
import java.util.ArrayList;
import java.util.Collections;
public class Word{
String word;
public Word(String w){
word=w;
}
public String toString(){
if (word.length()<6) {
return word;
}
else {
String first = word.substring(0,2);
int length = word.length();
String middle = word.substring(2,length-2);
ArrayList<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String last = word.substring(length-2);
return first+scrambled+last;
}
}
}
java class
I am having a difficult time understanding how classes work and cant figure out how to take my currentToken make it into a word then use the word class to scramble it or not then return the word either scrambled or not into a println.
Main Class:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class Readability
{
public static void main(String args)
throws FileNotFoundException {
Scanner doc = new Scanner( new File("README.txt"));//imports README.txt
System.out.println(tokens);
}
public static ArrayList<String> punctuation(Scanner doc){//creates new method called punctuation
ArrayList<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()){//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
wordObject.toString();
if (currentToken.length()>0){//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(currentToken);
}
}
return tokens;
}
}
Word Class:
import java.util.ArrayList;
import java.util.Collections;
public class Word{
String word;
public Word(String w){
word=w;
}
public String toString(){
if (word.length()<6) {
return word;
}
else {
String first = word.substring(0,2);
int length = word.length();
String middle = word.substring(2,length-2);
ArrayList<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String last = word.substring(length-2);
return first+scrambled+last;
}
}
}
java class
java class
asked Nov 27 '18 at 8:59
ic0nic0n
12
12
Is there any problem with your program (which one)?
– Henry
Nov 27 '18 at 9:04
@Henry My println is giving me an error: Cannot find symbol: variable tokens. I also am not able to tell if my current tokens are being read by the word class and scrambled at all and if those are even going to my main method in my main class.
– ic0n
Nov 27 '18 at 9:06
return first+scrambled+last; this will lead to issues, since scrambled isn't a String you are concatenating
– Stultuske
Nov 27 '18 at 9:09
@ValentinCarnupunctuation
method is static. no need to instatiateReadability
– Sharon Ben Asher
Nov 27 '18 at 9:09
@ValentinCarnu I didn't mention that at all. why on earth would a toString method return a List? Change the returntype, and you'll get compilation issues
– Stultuske
Nov 27 '18 at 9:13
|
show 4 more comments
Is there any problem with your program (which one)?
– Henry
Nov 27 '18 at 9:04
@Henry My println is giving me an error: Cannot find symbol: variable tokens. I also am not able to tell if my current tokens are being read by the word class and scrambled at all and if those are even going to my main method in my main class.
– ic0n
Nov 27 '18 at 9:06
return first+scrambled+last; this will lead to issues, since scrambled isn't a String you are concatenating
– Stultuske
Nov 27 '18 at 9:09
@ValentinCarnupunctuation
method is static. no need to instatiateReadability
– Sharon Ben Asher
Nov 27 '18 at 9:09
@ValentinCarnu I didn't mention that at all. why on earth would a toString method return a List? Change the returntype, and you'll get compilation issues
– Stultuske
Nov 27 '18 at 9:13
Is there any problem with your program (which one)?
– Henry
Nov 27 '18 at 9:04
Is there any problem with your program (which one)?
– Henry
Nov 27 '18 at 9:04
@Henry My println is giving me an error: Cannot find symbol: variable tokens. I also am not able to tell if my current tokens are being read by the word class and scrambled at all and if those are even going to my main method in my main class.
– ic0n
Nov 27 '18 at 9:06
@Henry My println is giving me an error: Cannot find symbol: variable tokens. I also am not able to tell if my current tokens are being read by the word class and scrambled at all and if those are even going to my main method in my main class.
– ic0n
Nov 27 '18 at 9:06
return first+scrambled+last; this will lead to issues, since scrambled isn't a String you are concatenating
– Stultuske
Nov 27 '18 at 9:09
return first+scrambled+last; this will lead to issues, since scrambled isn't a String you are concatenating
– Stultuske
Nov 27 '18 at 9:09
@ValentinCarnu
punctuation
method is static. no need to instatiate Readability
– Sharon Ben Asher
Nov 27 '18 at 9:09
@ValentinCarnu
punctuation
method is static. no need to instatiate Readability
– Sharon Ben Asher
Nov 27 '18 at 9:09
@ValentinCarnu I didn't mention that at all. why on earth would a toString method return a List? Change the returntype, and you'll get compilation issues
– Stultuske
Nov 27 '18 at 9:13
@ValentinCarnu I didn't mention that at all. why on earth would a toString method return a List? Change the returntype, and you'll get compilation issues
– Stultuske
Nov 27 '18 at 9:13
|
show 4 more comments
1 Answer
1
active
oldest
votes
Not sure if this is what you need, but maybe it helps
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class Readability {
public static void main(String args) throws FileNotFoundException {
Scanner doc = new Scanner(new File("README.txt"));//imports README.txt
List<String> tokens = Readability.punctuation(doc);
tokens.stream().forEach(token -> System.out.println(token));
}
public static List<String> punctuation(Scanner doc) {//creates new method called punctuation
List<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()) {//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
if (currentToken.length() > 0) {//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(wordObject.scramble());
}
}
return tokens;
}
}
class Word {
private String word;
public Word(String w) {
word = w;
}
public String scramble() {
if (word.length() < 6) {
return word;
} else {
String first = word.substring(0, 2);
int length = word.length();
String middle = word.substring(2, length - 2);
List<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String scrambledWord = scrambled.stream().collect(Collectors.joining());
String last = word.substring(length - 2);
return first + scrambledWord + last;
}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53495954%2fdifficulty-understanding-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not sure if this is what you need, but maybe it helps
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class Readability {
public static void main(String args) throws FileNotFoundException {
Scanner doc = new Scanner(new File("README.txt"));//imports README.txt
List<String> tokens = Readability.punctuation(doc);
tokens.stream().forEach(token -> System.out.println(token));
}
public static List<String> punctuation(Scanner doc) {//creates new method called punctuation
List<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()) {//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
if (currentToken.length() > 0) {//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(wordObject.scramble());
}
}
return tokens;
}
}
class Word {
private String word;
public Word(String w) {
word = w;
}
public String scramble() {
if (word.length() < 6) {
return word;
} else {
String first = word.substring(0, 2);
int length = word.length();
String middle = word.substring(2, length - 2);
List<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String scrambledWord = scrambled.stream().collect(Collectors.joining());
String last = word.substring(length - 2);
return first + scrambledWord + last;
}
}
}
add a comment |
Not sure if this is what you need, but maybe it helps
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class Readability {
public static void main(String args) throws FileNotFoundException {
Scanner doc = new Scanner(new File("README.txt"));//imports README.txt
List<String> tokens = Readability.punctuation(doc);
tokens.stream().forEach(token -> System.out.println(token));
}
public static List<String> punctuation(Scanner doc) {//creates new method called punctuation
List<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()) {//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
if (currentToken.length() > 0) {//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(wordObject.scramble());
}
}
return tokens;
}
}
class Word {
private String word;
public Word(String w) {
word = w;
}
public String scramble() {
if (word.length() < 6) {
return word;
} else {
String first = word.substring(0, 2);
int length = word.length();
String middle = word.substring(2, length - 2);
List<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String scrambledWord = scrambled.stream().collect(Collectors.joining());
String last = word.substring(length - 2);
return first + scrambledWord + last;
}
}
}
add a comment |
Not sure if this is what you need, but maybe it helps
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class Readability {
public static void main(String args) throws FileNotFoundException {
Scanner doc = new Scanner(new File("README.txt"));//imports README.txt
List<String> tokens = Readability.punctuation(doc);
tokens.stream().forEach(token -> System.out.println(token));
}
public static List<String> punctuation(Scanner doc) {//creates new method called punctuation
List<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()) {//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
if (currentToken.length() > 0) {//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(wordObject.scramble());
}
}
return tokens;
}
}
class Word {
private String word;
public Word(String w) {
word = w;
}
public String scramble() {
if (word.length() < 6) {
return word;
} else {
String first = word.substring(0, 2);
int length = word.length();
String middle = word.substring(2, length - 2);
List<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String scrambledWord = scrambled.stream().collect(Collectors.joining());
String last = word.substring(length - 2);
return first + scrambledWord + last;
}
}
}
Not sure if this is what you need, but maybe it helps
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class Readability {
public static void main(String args) throws FileNotFoundException {
Scanner doc = new Scanner(new File("README.txt"));//imports README.txt
List<String> tokens = Readability.punctuation(doc);
tokens.stream().forEach(token -> System.out.println(token));
}
public static List<String> punctuation(Scanner doc) {//creates new method called punctuation
List<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()) {//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
if (currentToken.length() > 0) {//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(wordObject.scramble());
}
}
return tokens;
}
}
class Word {
private String word;
public Word(String w) {
word = w;
}
public String scramble() {
if (word.length() < 6) {
return word;
} else {
String first = word.substring(0, 2);
int length = word.length();
String middle = word.substring(2, length - 2);
List<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String scrambledWord = scrambled.stream().collect(Collectors.joining());
String last = word.substring(length - 2);
return first + scrambledWord + last;
}
}
}
answered Nov 27 '18 at 9:50
Valentin CarnuValentin Carnu
374210
374210
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53495954%2fdifficulty-understanding-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is there any problem with your program (which one)?
– Henry
Nov 27 '18 at 9:04
@Henry My println is giving me an error: Cannot find symbol: variable tokens. I also am not able to tell if my current tokens are being read by the word class and scrambled at all and if those are even going to my main method in my main class.
– ic0n
Nov 27 '18 at 9:06
return first+scrambled+last; this will lead to issues, since scrambled isn't a String you are concatenating
– Stultuske
Nov 27 '18 at 9:09
@ValentinCarnu
punctuation
method is static. no need to instatiateReadability
– Sharon Ben Asher
Nov 27 '18 at 9:09
@ValentinCarnu I didn't mention that at all. why on earth would a toString method return a List? Change the returntype, and you'll get compilation issues
– Stultuske
Nov 27 '18 at 9:13