Creation of Acroynm with Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am currently attempting to make a program that takes any string input and splits by the spaces to create an acronym. Currently the program only prints out the first letter of the first word input. I believe that the problem is with my for loop. Could someone care to take a look?
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class WordSplitter {
public static void main(String[] args) {
String phraseToChange;
Scanner input = new Scanner(System.in);
System.out.println("This program builds acronyms");
System.out.println("Enter a phrase:");
phraseToChange = input.next();
String[] phraseChanger = phraseToChange.split(" ");
for (int i = 0; i <= phraseChanger.length; i++) {
String s = phraseChanger[i];
System.out.println(s.charAt(0));
}
}
}

You never actually build the acronym using the first letter from each word. Try this code instead:
String [] phraseChanger = phraseToChange.split(" ");
StringBuilder sb = new StringBuilder();
for (int i=0; i < phraseChanger.length ; i++) {
sb.append(phraseChanger[i].charAt(0));
}
System.out.println(sb.toString());

instead of this phraseToChange = input.next(); you have to put phraseToChange = input.nextLine(); it will work

Related

mute program that I code is not working when I enter a word which ends with m letter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
The program have to mute when user input includes "m","a","l" letters side by side. But when I enter a word which ends with "m" word, program doesn't work. I left some pictures about code.
import java.util.Scanner;
public class mute
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String enteredmessage;
System.out.print("Enter the message:");
enteredmessage = scan.nextLine();
char[] array = enteredmessage.toCharArray();
mutecommand(array);
scan.close();
}
public static void mutecommand(char[] dizi) {
for (int i = 0; i < dizi.length; i++) {
if(dizi[i] == 'm' && dizi[i+1] == 'a' && dizi[i+2]=='l')
{
dizi[i]='*';
dizi[i+1]='*';
dizi[i+2]='*';
}
}
for (char a : dizi) {
System.out.print(a);
}
}
}
[https://i.stack.imgur.com/tHOFw.png]
[https://i.stack.imgur.com/UP32b.png]
It's because of the fact, that you are iterating through the array and if you at the last item and do an i + 2 so it's out of the array

How do I display the actual input instead of option number? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to get this to display the actual input instead of the option number.
import java.util.*;
public class RandomGenerator {
public static void main(String[] args) {
int length;
Scanner input = new Scanner(System.in);
System.out.println("How many options?"); //user input food options
length = input.nextInt();
String[] names = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Enter option #" + (counter+1) + ":");
names[counter] = input.next();
}
input.close();
System.out.println("You are going to eat " + new Random().nextInt(names.length));
You've already generated a random number here:
new Random().nextInt(names.length)
You can use this random number to access an element in the names array.
int randomNumber = new Random().nextInt(names.length);
String option = names[randomNumber]; // here is the important bit!
Now you can print option out!
System.out.println("You are going to eat " + option);

I am trying to input sentences in string arrays and display them in reverse order [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So far I have set it up so that the user can enter the number of sentences and input into each position of the String array using a for loop.
public class Test5 {
public static String inputline;
public static void main(String[] args) {
System.out.print("Enter the number of lines:");
Scanner kb=new Scanner(System.in);
int number=kb.nextInt();
String []line=new String[number];
for(int i=0;i<line.length+1;i++){
line[i]=kb.next();
}
}
}
First and foremost your code is going to read in 1 more time than you want which will cause an array out of bounds exception. Next you will want to do nextLine() to account for the new line character being entered by the user. Try this:
System.out.print("Enter the number of lines:");
Scanner kb=new Scanner(System.in);
int number=Integer.parseInt(kb.nextLine());
String []line=new String[number];
//loop through only the size of the array
for(int i=0; i < line.length; i++){
line[i]=kb.nextLine();
}
//now to output the array in reverse order you need to start from the
//other end of the array
for(int i = line.length - 1; i >= 0; i--){
System.out.println(line[i]);
}
//always close the Scanner when done
kb.close();
Some useful resources about Scanners - https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Printing stars depending on the txt folder [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a txt file with numbers and I must read the file and print stars depending on the numbers that I am given.How can i do that?
public class Testing
{
public static void main(String [] args)
throws IOException
{
FileInputStream fileInputStream = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(fileInputStream);
double doubleNum=(scanner.nextDouble());
int intNumb=(int) doubleNum;
for (int i=0;i<intNumb;i++)
while (scanner.hasNextInt())
{
System.out.println("*");
}
}
}
Try to be more specific please. What is the input and what is the desired output ? I hope I'll be able to help you if this below doesn't help you.
FileInputStream fileInputStream = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(fileInputStream);
while (scanner.hasNextDouble()) {
int num = Double.valueOf(scanner.nextDouble()).intValue();
for (int i = 0; i < num; i++) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
In the case you needed outputs to be like **.*** for e.g. 40.001
FileInputStream fileInputStream = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(fileInputStream);
while (scanner.hasNext()) {
// scanner.next() gets the next token
// replaceAll() replaces every digits with * (\d is equal to [0-9] and it needs to be escaped with \ giving \\d)
System.out.println(scanner.next().replaceAll("\\d", "*"));
}
scanner.close();
fileInputStream.close();
That's the file im given.
The expected output is * depending on each number.

creating a new string from a char array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm seeing some weird occurrence that if I don't use my original input string to be converted back into a string from a char array, the changes don't occur in the new string?
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print("Type in String:");
String theString = scan.nextLine();
theString = theString.replaceAll("\\s+", " ");
char [] convert = theString.toCharArray();
convert[0] = Character.toUpperCase(convert[0]);
for(int i = 0; i < convert.length; i++){
if(Character.isWhitespace(convert[i])){
convert[i+1] = Character.toUpperCase(convert[i+1]);
}
}
theString = String.valueOf(convert);
System.out.println(theString);
If line theString on the 2nd the to last line was changed to lets say:
String newString = String.valueOf(convert);
or
String newString = String(convert);
My output does not change the input! This program in summary capitalizes the first character of every word in a string. Can someone explain why occurrence is happening?
Its works. I tested it. I think you forget to change
System.out.println(theString);
to
System.out.println(newString);
Full code:
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Type in String:");
String theString = scan.nextLine();
theString = theString.replaceAll("\\s+", " ");
char [] convert = theString.trim().toCharArray();
convert[0] = Character.toUpperCase(convert[0]);
for(int i = 0; i < convert.length; i++){
if(Character.isWhitespace(convert[i])){
convert[i+1] = Character.toUpperCase(convert[i+1]);
}
}
String newString = String.valueOf(convert);
System.out.println(newString);
}
input: hello world
output: Hello World
Thats what you expected :) And i fixed the ArrayIndexOutOfBounds Exception when the String ends with a whitespace.
char [] convert = theString.trim().toCharArray();
It works fine, but what you want to consider is after scanning in the String to use
theString.trim();
This will remove whitspaces from the end, because right now if I enter "hello hello " (notice the space at the end) it will throw an OutOfBOundsException because of your i+1 in the loop.
Or just check inside your loop if the index i+1 exists.

Categories

Resources