Ask user to enter word until they enter "exit", when they enter exit display all of their entered words without the "exit".
I'm confused how to combine all of their words and display it at the end, I know I will need another loop for that
import java.util.*;
public class testprac {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Enter a word: ");
String word = input.nextLine();
if (word.equals("exit")) {
System.out.println("Exited");
System.out.println("You entered: ");
break;
}
}
}
}
You can use array or List to store the inputs and then loop through the list or array to print.You can also to toString() method to print.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List<String> inputs = new ArrayList<>();
while (true) {
System.out.println("Enter a word: ");
String word = input.nextLine();
if (word.equals("exit")) {
System.out.println("Exited");
System.out.println("You entered: "+inputs);
break;
} else {
inputs.add(word);
}
}
}
}
This should work:
import java.util.*;
public class testprac {
public static void main(String[] args) {
System.out.println("Enter a word:");
Scanner inputScanner = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
while (inputScanner.hasNextLine()) {
String line = inputScanner.nextLine();
Scanner lineScanner = new Scanner(line);
while (lineScanner.hasNext()) {
String s = lineScanner.nextLine();
if (s.equalsIgnoreCase("exit")) {
System.out.println("Exited");
System.out.println("You entered: ");
System.out.println(sb.toString());
lineScanner.close();
System.exit(0);
} else {
sb.append(s);
}
}
}
inputScanner.close();
}
Related
I was given a assignment for my Intro to Computer Science class to make the user enter any word until they guess the secret word, once they get the secret word then the system will say "Stop!" So far I have the user trying once and I want the program to continue looping until the user enters the right word. I really need some help, this is what I have so far.
import java.util.Scanner;
public class HW2
{
public static void main(String[] args)
{
String input; //The users input
// New Scanner for keyboard input
Scanner keyboard = new Scanner(System.in);
//Tell the user to guess the word
System.out.print("Guess the secret word: ");
input = keyboard.nextLine();
if(input.equalsIgnoreCase("college"))
{
System.out.print("Stop !");
}
}
}
All you need is a do-while loop.
import java.util.Scanner;
public class HW2
{
public static void main(String[] args)
{
String input; //The users input
boolean hasGuessedCorrectly = false;
// New Scanner for keyboard input
Scanner keyboard = new Scanner(System.in);
//Tell the user to guess the word
do
{
System.out.print("Guess the secret word: ");
input = keyboard.nextLine();
if (input.equalsIgnoreCase("college"))
{
hasGuessedCorrectly = true;
System.out.print("Stop !");
}
} while (!hasGuessedCorrectly);
}
}
A do-while loop could be used for this task:
import java.util.Scanner;
public class Main {
private static final String SECRET_WORD = "college";
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
do {
System.out.print("Guess the secret word: ");
} while (!SECRET_WORD.equalsIgnoreCase(keyboard.nextLine()));
System.out.println("Stop!");
}
}
Live demo
Consider the while loop:
import java.util.Scanner;
public class HW2
{
public static void main(String[] args)
{
String input = ""; //The users input
// New Scanner for keyboard input
Scanner keyboard = new Scanner(System.in);
// The below code is run until the secret word is guessed.
while (!"college".equalsIgnoreCase(input))
{
//Tell the user to guess the word
System.out.print("Guess the secret word: ");
input = keyboard.nextLine();
/* if(input.equalsIgnoreCase("college"))
{
System.out.print("Stop !");
} This code is no longer necessary, as we have put the stop
condition in the while loop declaration. */
}
// This only runs after the loop has finished
System.out.println("Stop !");
}
}
I can get and print the integer value in java but I am confuse how to get and print string. Can someone help
package hello;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
int integer;
System.out.println("Please Enter Integer");
Scanner sc = new Scanner(System.in);
integer = sc.nextInt();
sc.close();
System.out.println("you entered : " +integer);
}
}
Program output
Please Enter Integer
5
you entered : 5
I am stuck in this program. I don't understand how to get string and print on screen
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
int name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextInt();
sc.close();
System.out.println("Your name"+name);
}
}
You need to change your type value name from int to String. And replace sc.nextInt() by sc.nextLine() or sc.next().
Example
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name " + name);
}
Use sc.nextLine() for reading string inputs
or
sc.next() (But this will read only a word before it encounters a space)
You can also use InputStreamReader for this purpose
eg.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in()));
String input = br.readLine();
name = sc.nextInt(); doesn't work for strings, only for integers, you should use sc.nextline instead.
And also you have to change int name to String name, due to other type of variable.
Your code should look like this:
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name"+name);
}
}
change int name to string name and use sc.nextLine()
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name"+name);
}
}
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++;
}
}
}
import java.util.Scanner;
public class Trial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first string: ");
String one = input.nextLine();
System.out.println("Please enter the second string: ");
String two = input.nextLine();
.....
Try this:
import java.util.Scanner;
public class Trial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first string: ");
String one = input.nextLine();
System.out.println("Please enter the second string: ");
String two = input.nextLine();
StringBuilder sb = new StringBuilder();
for (int i = one.length() - 1, j = two.length() - 1; i >= 0 && j >= 0; i--, j--) {
if (one.charAt(i) != two.charAt(j)) {
break;
}
sb.append(one.charAt(i));
}
System.out.println(sb.reverse().toString());
}
}
I hope, that this piece of code is self-explanatory.
You can also use Google Guava to find a common suffix:
com.google.common.base.Strings.commonSuffix(str1, str2)
import java.util.Scanner;
import com.google.common.base.Strings;
public class Trial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first string: ");
String one = input.nextLine();
System.out.println("Please enter the second string: ");
String two = input.nextLine();
System.out.println("Common suffix: " + Strings.commonSuffix(one, two));
}
}
this is what i have so far, but when i input q it gives me lines of text that are not my desired out put of "Q"
import java.util.Scanner;
public class DollarsandCents
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String input = "q";
double currency;
System.out.println("Enter a currency value or enter q to quit:");
currency = stdIn.nextDouble ();
if (currency >= .00)
{
System.out.printf("Formatted currency value: $%,.2f\n", currency);
}
else
{
System.out.print("Q");
}
} // end main
} // end class DollarsandCents
what do i need to add that would be make it work?
import java.util.Scanner;
public class Currency
{
public static void main(String args[])
{
Scanner stdIn = new Scanner(System.in);
String currencyValue="";
while (true)
{
System.out.print("Enter a currency value or enter q to quit: ");
currencyValue = stdIn.nextLine();
if(currencyValue.equals("q")) {
System.out.println("You pressed q, have a nice day");
break;
}
System.out.printf("Formatted currency value: $%,.2f\n", Double.parseDouble(currencyValue));
}
}
}
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String input = "q";
double currency;
System.out.println("Enter a currency value or enter q to quit:");
currency = stdIn.nextLine ();
if (currency.equals(q))
{
System.out.print("Q");
}
else
{
double curr = Double.parseDouble(currencyValue);
if( Double.compare(curr,0.00) >= 0)
{
System.out.printf("Formatted currency value: $%,.2f\n", curr);
}
else
System.out.printf("Currency value is less than 0.00");
}
} // end main