How do I apply the outputWithoutWhitespace() method? Java - java

My current code is as follows, I need to print the user's input using the outputWithoutWhitespace() method but I am having a hard time understanding where to apply it/how to actually use it. I believe I should be using \t. How can I take away the whitespace when printing with the outputWithoutWhitespace() method?
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
int userCount = userInput.length();
return userCount;
}
}

package whitesp;
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
String omitSpace=userInput.replace(" ","");
int userCount = omitSpace.length();
return userCount;
}
}

Related

How to call a variable in another method to your main method

I'm new to Java and I'm having a bit of trouble trying to call a variable (firstName) from another method to my main. Here is my code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter full name: ");
String fullName = reader.nextLine();
System.out.print("Enter graduation year: ");
int gradYear = reader.nextInt();
System.out.print("Greetings, " + firstName + ", your initials are ");
}
public static void getFirstName(String fullName) {
Scanner reader = new Scanner(System.in);
String firstName = fullName.substring(0,1).toUpperCase() + fullName.substring(1).toLowerCase();
}
}
First of note that after using nextInt() the following nextLine() won't work.
I guess, this is what you are trying to do.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter full name: ");
String fullName = reader.nextLine();
System.out.print("Enter graduation year: ");
int gradYear = reader.nextInt();
System.out.print("Greetings, " + getFirstName(fullName) + " your initial is, " + getInitial(fullName));
reader.close();
}
private static String getInitial(String fullName) {
return fullName.substring(0,1).toUpperCase();
}
public static String getFirstName(String fullName) {
return fullName.substring(1).toLowerCase();
}
You can return that value from a method and assign to a variable.
Let me show you.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter full name: ");
String fullName = reader.nextLine();
System.out.print("Enter graduation year: ");
int gradYear = reader.nextInt();
String firstName = getFirstName(fullName);
System.out.print("Greetings, " + firstName + ", your initials are ");
}
public static String getFirstName(String fullName) {
// Scanner reader = new Scanner(System.in);
String firstName = fullName.substring(1).toLowerCase(); // assuming first letter is the initials; best would be to substring by space or similar
return firstName;
}

When I put number in array size The project work fine but when i put int subNo give me error

I am new in this field.
package test;
import java.util.Scanner;
public class Subject {
public static void main(String [] args){
System.out.println("Please Enter Subject No");
subNo =scan.nextInt();
System.out.println("Subject No Is : " + subNo);
for (int i = 0; i < subNo; i++) {
System.out.println("Please Enter Subject Name " + (i + 1));
subName[i] = scan.next();
}
}
public static int subNo;
public static String[] subName = new String [subNo] ;
static Scanner scan = new Scanner (System.in);
}
You have to initialize your array after you read subNo :
public static String[] subName;//<---------------not initialize it here
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please Enter Subject No");
subNo = scan.nextInt();
subName = new String[subNo];//<------------------initialize it here
System.out.println("Subject No Is : " + subNo);
Because when you run your program your array will be inisialize with the static subNo and not with the new one in your main
You must define the subName variable after of subNo variable, because this will be assigned after entering a value, for example:
public static int subNo;
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please Enter Subject No");
subNo = scan.nextInt();
System.out.println("Subject No Is : " + subNo);
String[] subName = new String[subNo];
for (int i = 0; i < subNo; i++) {
System.out.println("Please Enter Subject Name " + (i + 1));
subName[i] = scan.next();
}
}

Use a while loop to display letters entered by a user in vertical order?

I'm trying to create a script that will ask a user to input a word and then display back the letters entered by the user in vertical order. The problem is that I'm required to use a while loop, any ideas??????
import java.util.Scanner;
public class VerticalWords {
public static void main(String[] args) {
System.out.println("Enter A word");
Scanner scan = new Scanner(System.in);
String word = scan.nextLine();
for(char a : word.toCharArray())
{
System.out.println("Letter: " + a);
}
}
}
I have tried that code, and it works but its not a while loop ^
Try this:
System.out.println("Enter A word");
Scanner scan = new Scanner(System.in);
int a = 0;
String word = scan.nextLine();
while(a < word.length){
System.out.println(word.charAt(a));
a++;
}
import java.util.Scanner;
public class VerticalWords {
public static void main(String[] args){
// TODO Auto-generated method stub
System.out.println("Enter A word: ");
Scanner scan = new Scanner(System.in);
int a = 0;
String word = scan.nextLine();
while(a < word.length())
{
System.out.println("Letter: " + (word.charAt(a)));
a++;
}
}
}

How do I create a user-defined method that returns the length of a string?

I am trying to write a program that takes a user's input and outputs the number of characters they typed in. I have to do this by creating a method that calculates the amount of characters, then call that method in main to output the results. I was encouraged to use a for loop, but I don't see how that would work. I can calculate the number of characters using length(), but I can't figure out how to make my method work. This is what I have so far:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = "";
System.out.println("Enter a sentence: ");
System.out.print("You entered: ");
userInput = scnr.nextLine();
System.out.println(userInput);
return;
}
public static int GetNumOfCharacters(int userCount) {
int i = 0;
String userInput = "";
userCount = userInput.length();
return userCount;
}
}
My method is not returning the length of the string, it just gives me 0 or an error.
Right now, you are never calling your "GetNumOfCharacters" method in your main. The way Java programs work, is by calling the main method and executing line per line what lies there. So you need to call you method from inside the main method. On the other hand, it should get the Stirng as a parameter, so you can get its length. It would look something like this:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = "";
System.out.println("Enter a sentence: ");
userInput = scnr.nextLine();
System.out.print("You entered: ");
System.out.println(userInput);
int lenInput = GetNumOfCharacters(userInput);
System.out.println("The length was: "+lenInput+" characters");
}
public static int GetNumOfCharacters(String userInput) {
int len = userInput.length();
return len;
}
A problem is that you are not actually calling the method
so try
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String userInput = scnr.nextLine();
System.out.print("You entered: ");
System.out.println(userInput);
System.out.println ("The length is " + GetNumOfCharacters (userInput))
}
// need to pass string into this method
public static int GetNumOfCharacters(String myString) {
int userCount = myString.length();
return userCount;
}
}
Your question included the line:
I was encouraged to use a for loop, but I don't see how that would
work.
There's no elegant way to do this in Java because you are assumed to use String.length() to get the length of strings. There is no 'end of string' marker as there is in, say, C. However you could mimic the same effect by catching the exception thrown when you access past the end of the string:
for (int len = 0; ; len++) {
try {
text.charAt(len);
} catch (StringIndexOutOfBoundsException ex) {
return len;
}
}
That's not a nice, efficient or useful piece of code but it does demonstrate how to get the length of a string using a for loop.
Problems with your code:
No Function call
Add function call in main() as int count=GetNumOfCharacters(userInput);
Parameter datatype mismatch
change the datatype in function definition from int to String as public static int GetNumOfCharacters(String userInput) {
Unwanted return statement in main()
remove the return from main()
Not displaying the value returned from GetNumOfCharacters
Add System.out.print("Number of characters: "+ count); inside main()
Code:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = "";
System.out.println("Enter a sentence: ");
System.out.print("You entered: ");
userInput = scnr.nextLine();
System.out.println(userInput);
int count=GetNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int GetNumOfCharacters(String userInput) {
int userCount = userInput.length();
return userCount;
}
OR
Function is not really needed,you can remove the function and do it like this:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = "";
System.out.println("Enter a sentence: ");
System.out.print("You entered: ");
userInput = scnr.nextLine();
System.out.println(userInput);
System.out.print("Number of characters: "+ userInput.length());
}
If you don't want to use predefined methods, you can do like this..
public static void main(String args[]){
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String userInput = scnr.nextLine();
System.out.println("You entered: "+userInput);
char a[]=userInput.toCharArray();
int count=0;
for(char c : a){
count++;
}
System.out.println("length of the string is:"+count);
}

print a returned string in java

I am trying to print the returned string value in the main method but I can't figure it out. can someone give me a hand?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String in_word;
int number_input;
Scanner input_word = new Scanner (System.in);
Scanner input_times = new Scanner (System.in);
System.out.println("Enter word: ");
in_word = input_word.next();
System.out.println("Enter number of times to concatenate: ");
number_input = input_times.nextInt();
multiConcat(in_word, number_input);
}
public static String multiConcat(String word, int times) {
//String word;
String s = "";
int number;//times, ;
for (number = 0; number < times; number++)
s = word.concat(s);
return s;
}
}
System.out.println(multiConcat(in_word, number_input));

Categories