Question

(Process a string) Write a program that prompts the user to enter a string and displays...

(Process a string) Write a program that prompts the user to enter a string and displays its length and its first character.

java StringLength

Enter a string:Does quantum determinacy have anything to with whether human cognition is deterministic?
The string is of length 88 and the first character is D

import java.util.Scanner;
class StringLength{
   public static void main (String args[]){
       Scanner input = new Scanner(System.in);
       System.out.println("Enter a string:");
       String ans = input.nextLine();
       System.out.println("The string is of length " + ans.length() + " and the first character is " + ans.substring(0, 1));
   }
}

you displayed:
...
The string is of length 5 and the first character is 5

instead of:
Enter a string:The string is of length 5 and the first character is 5

****   Please Help *******

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

class StringLength {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        // It seems like we shouldn't print a newline after Enter a string:
        // Try this and let me know. If it didn't work, we will try something else
        System.out.print("Enter a string:");
        String ans = input.nextLine();
        System.out.println("The string is of length " + ans.length() + " and the first character is " + ans.charAt(0));
    }
}
Add a comment
Know the answer?
Add Answer to:
(Process a string) Write a program that prompts the user to enter a string and displays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a JAVA program that prompts the user for student grades and displays the highest and...

    Write a JAVA program that prompts the user for student grades and displays the highest and lowest grades in the class. The user should enter a character to stop providing values. Starter code: import java.util.Scanner; public class MaxMinGrades{   public static void main(String[] args){     Scanner input = new Scanner(System.in);     System.out.println("Enter as many student grades as you like. Enter a character to stop.");     double grade = input.nextDouble();     double minGrade = Double.MAX_VALUE;     double maxGrade = Double.MIN_VALUE;     while (Character.isDigit(grade)) {       if (grade == 0)...

  • Complete the following program so it prompts the user for 3 integers and displays the smallest...

    Complete the following program so it prompts the user for 3 integers and displays the smallest of the 3 numbers. For instance, if the user provided 7 4 and 12, then the program would display 4. import java.util.Scanner; public class Program3{ public static void main(String[] args) { Scanner kb = new Scanner(System.in);

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • The program prompts the user to input three words, then display them in      sorted order....

    The program prompts the user to input three words, then display them in      sorted order. Strings have to be compared with compareTo method. */ import java.util.Scanner; public class Lab4Part13{     public static void main(String[] args){         Scanner input = new Scanner(System.in);         // Write rest of the code here     } }

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • in Java This program will output a right triangle based on user specified height triangleHeight and...

    in Java This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a character. Modify the given program to output a right triangle that instead uses the user-specified trianglechar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or* Each subsequent line will...

  • Program Requirements ·         The program prompts the user to enter a loan amount and an interest rate....

    Program Requirements ·         The program prompts the user to enter a loan amount and an interest rate. ·         The application calculates the interest amount and formats the loan amount, interest rate, and interest amount. Then, it displays the formatted results to the user. ·         The application prompts the user to continue. ·         The program stops prompting the user for values after taking 3 loan amounts. ·         The application calculates and displays the total loan and interest amount and ends the program Example Output Welcome to...

  • Please put both of this loops into one program. Set it up so that there is...

    Please put both of this loops into one program. Set it up so that there is one main function and both loops are within it. Thank You. import java.util.Scanner; public class WhileDavidMungomaLab12 { public static void main(String[] args) { int aScore = -1; while (aScore < 0 || aScore > 100) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a valid number that is within the specified range(0 and 100): "); aScore = sc.nextInt(); } } } AND import java.util.Scanner;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT