Get length of string method Java - java

I need to do this:
Create a program that asks for the user's name and says how many characters the name contains.
Your program should be structured so that you put the calculating of the name length in it's own method: public static int calculateCharacters(String text). The tests will be testing both the method calculateCharacters and the program overall.
I know how to count the characters in a string. I just can't figure out how to do this when I have to use a seperate method. Can someone please help me?
import java.util.Scanner;
public class LengthOfName {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type your name: ");
String name = reader.nextLine ();
int cal = calculateCharacters(name);
System.out.println(Number of characters: " + cal);
}
public static int calculateCharacters(String text) {
int cal = text.length ();
//System.out.println("Number of characters: " + count ());
return cal;`

What's the error? I'm not seeing any errors in logic here, just a couple syntax issues like missing a quotation mark around "Number of characters: ", a missing curly brace at the end of the calculateCharacters method, and an extra whitespace between reader.nextLine and ().
Your method call itself looks fine.

Related

Take the whole line of a java chain and lenth

I am trying to make a java program that given a string, I return the length of it, but I do not know why it does not catch me all. I'm starting with the programing. I've got here.
import java.util.Scanner;
public class lengt {
public static void main (String [] args) {
Scanner in = new Scanner (System.in);
String cad = in.next();
System.out.println (cad);
System.out.println ("The length is:" + cad.length ());
}
}
There is a problem in your solution. in.next(); just take the first word of the chain, the one that is most immediate. If instead of using that you put it in.nextLine(); It takes you all the line you enter. You also have to be careful with length, which goes from 0 to n-1, when it comes to taking the length, since you could take one more and unnecessary. The solution for the problem that you pose would be, and there may be more, but one valid
import java.util.Scanner;
public class Example {
public static void main (String [] args) {
Scanner in= new Scanner (System.in);
String cad = in.nextLine();
System.out.println (cad);
System.out.println ("The length is:" + cad.length() - 1); //you can remove -1
}
}

I need to capitalize everything after a the colon in an entered program

Hello so I am a first year computer science student that needs some help. I am assigned to make a program that I can enter a string into and then based on what I enter it changes. So if I enter something with a colon, the output is everything after the colon capitalized and if there is no colon then everything is capitalized. So far I know I need to use an if function as well as index of but I am not quite sure how. Below I will put what I currently have. The int = indexOf is not working for some reason if anyone can give me any advice I will greatly appreciate it.
import java.util.Scanner;
public class StringFunctions
{
public static void main(String[] args)
{
Scanner user_input = new Scanner( System.in );
String text;
System.out.print ( "Input> " ) ;
String input = user_input.next();
final int a = indexOf( " : " );
if ( a = 1)
System.out.println ( "yay" );
System.out.println( "output> " + input.trim().toUpperCase() );//; initialise instance variables
}
You're using indexOf wrong. The correct syntax to find the index of the first occurrence of : in the string stored in variable text is text.indexOf(":");

Simple Substring Issue

My teacher wants us to make a letter 'o' move around the console. The letter 'o' has been coded to appear in the center of the console screen. I have already created the movingRight and movingDown methods but I'm having difficulty creating the movingLeft and movingUp methods. Here is my code:
import java.util.Scanner;
public class Main {
static String letter = "\n\n\n\n O";
String whenmovingup = letter.substring(0, 1);
char whenmovingleft = letter.charAt(letter.length() - 2);
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(letter);
input.nextLine();
if (input.equals("left")) {
movingLeft();
}
if (input.equals("right")) {
movingRight();
}
if (input.equals("up")) {
movingUp();
}
if (input.equals("down")) {
movingDown();
}
}
public static void movingRight(){
letter = " " + letter;
}
public static void movingDown(){
letter = "\n" + letter;
}
public static void movingLeft(){
letter.remove(whenmovingleft);
}
public static void movingUp(){
letter.remove(whenmovingup);
}
}
I'm having an issue with removing the whenmovingfeft and whenmovingup substrings from my original string letter. It's giving an error ('The method remove(char) is undefined for the type String'), and I'm not sure what needs to be done.
Does anyone know how this can be resolved?
Thanks in advance for all responses.
There is no remove method for a string. However, there is a replace method that may do what you want. Note that it does not modify the string object, but it returns a new string. So you would do:
letter = letter.replace(whenmovingup, "");
Note that there are two slightly different overloads of replace which do different things depending on whether you ask it to remove a String or char. The replace(String, String) method replaces one occurrence, while the replace(char, char) replaces all occurrences. You want just one, so declare whenmovingleft as a String and initialise it appropriately.

What did I do wrong in my program?

I am writing a program where if someone types in the following two lines:
HELLO, I’D LIKE TO ORDER A FZGH
KID’S MEAL
The program will output it like this:
HELLO, I’D LIKE TO ORDER A KID’S MEAL
In other words, the "FZGH" the user inputs into the sentence will be replaced with the second line's words, as you can see: the "FZGH" is replaced by "KID'S MEAL." Kinda get what I mean? If not, I can elaborate more but this is the best I can explain it as.
I'm really close to solving this! My current output is: HELLO, I’D LIKE TO ORDER A FZGH KID’S MEAL
My program didn't replace the "FZGH" with "KID'S MEAL," and I don't know why that is. I thought that by using the .replaceAll() thingy, it would replace "FZGH" with the "KID'S MEAL," but that didn't really happen. Here is my program so far:
public static void main(String[] args) {
sentences();
}
public static void sentences() {
Scanner console = new Scanner(System.in);
String sentence1 = console.nextLine();
String sentence2 = console.nextLine();
//System.out.println(sentence1 + "\n" + sentence2);
String word = sentence1.replaceAll("[FZGH]", "");
word = sentence2;
System.out.print(sentence1 + word);
}
Where did I mess up, resulting in the FZGH still appearing in output?
Use
sentence1 = sentence1.replaceAll("FZGH", "");
String word = sentence2;
Your first (and primary) problem is that you're creating a new String named word, that you're setting to the value of sentence1.replaceAll("[FZGH]", ""). You're then changing the value of word to be sentence2 immediately afterward, so the replacement is lost.
Instead, setting sentence1 to sentence1.replaceAll("FZGH", ""); will change sentence1 to no longer contain the string "FZGH", which is what you're going for. You don't actually need a word value at all, so if you'd like to remove it, it wouldn't hurt.
In addition, using [FZGH] will replace all F's, Z's, G's, and H's from the string- you should use FZGH instead, as this will only remove instances of all four letters in a row.
I think you have a couple of mistakes. Maybe the following is close...
public static void main(String[] args) {
sentences();
}
public static void sentences() {
Scanner console = new Scanner(System.in);
String sentence1 = console.nextLine();
String sentence2 = console.nextLine();
String sentence3 = sentence1+sentence2;
String final = sentence3.replaceAll("FZGH", "");
System.out.print(final);
}
You are reassigning the string "word"
in place of lines :
String word = sentence1.replaceAll("[FZGH]", "");
word = sentence2;
System.out.print(sentence1 + word);
use the following lines
sentence1 = sentence1.replaceAll("[FZGH]", "");
System.out.print(sentence1 + sentence2);
Actually replace method return a string that should be assign again to sentence1. you can run this code its works fine.
public static void main(String[] args) {
sentences();
}
public static void sentences() {
Scanner console = new Scanner(System.in);
String sentence1 = "HELLO, I’D LIKE TO ORDER A FZGH";
String sentence2 = "KID’S MEAL";
//System.out.println(sentence1 + "\n" + sentence2);
sentence1 = sentence1.replace("FZGH", "");
String word = sentence2;
System.out.print(sentence1 + word);
}

charAt method is returning an integer. Need to convert to int

I'm creating a method that will take the given input and set the value to "plate". I then proceed to use the charAt method and try to take the characters(letters/String) input and set it to a new value. But i'm told to set it to a char value. When I run aprint statement with it, the output is just a bunch of integers, or in my case (for the code i'm going to show) it outputs "195" when I put the license plate as abc 123. Also, the model doesn't do anything(or atleast isnt supposed to). If anyone could tell me what exactly i'm doing wrong it would be a great help.
Here is my code so far:
import java.util.Scanner;
public class CarRental {
public static String model;
public static String plate;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Car Model:");
model = input.nextLine();
System.out.println("License Plate: ");
plate = input.nextLine();
char one = plate.charAt(0);
char two = plate.charAt(1);
System.out.println(two + one);
}
}
To make my issue clear, what I'm hoping to do is, assign the actual letters I type into the input to a separate value. I'm using the charAt method and it is returning integers.
if anyone could offer a suggestion it would help alot!
Thanks,
Sully
the + operator treats 2 chars as ints, try
System.out.println("" + two + one);
You can just use
Character.toChars(X)
Where x is your number to convert to char and then display said chars once they've been converted.

Categories