How to make it so that theres a wait time between dialog - java

how would i make it so that in between the System.out.println theres a pause, so it doesnt all spill out at once,
import java.io.*;
import java.util.Scanner;
public class tester {
public static void main(String[] args) {
System.out.println("Stranger: Good morning");
System.out.println("Stranger: you had a terrible Dream..");
System.out.println("Stranger: what is your name?");
System.out.print("Name:");
Scanner kbReader = new Scanner (System.in);
String s = kbReader.next();
System.out.println("Nice to meet you " + s);
System.out.println("my name is Master Wizard.");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
}
}

You could try something like...
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("Stranger: Good morning");
delay(500);
System.out.println("Stranger: you had a terrible Dream..");
delay(500);
System.out.println("Stranger: what is your name?");
System.out.print("Name:");
Scanner kbReader = new Scanner(System.in);
String s = kbReader.next();
System.out.println("Nice to meet you " + s);
System.out.println("my name is Master Wizard.");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
}
public static void delay(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException exp) {
}
}
}
For example

Related

Java: issue calculating average from txt file

Hello everybody first post on here!
i'm currently having some issues with my readfromfile() to calculate an average my issue is that its printing the ten numbers "stuck together"
like 12345678910 i dont understand how i can calculate an average like this i tried token/10 and it returns 0000000000
any suggestions getting an average from this mess?
i tried returning token with %n%s which looks better but still when i divide by 10 it doesnt give me a correct number what am i doing wrong
package average;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Formatter;
import java.util.InputMismatchException;
import java.util.Scanner;
public class average {
private static Formatter output;
private static Scanner input;
public static void main(String[] args) {
openFileWrite();
writeToFile();
closeFile();
openFileRead();
readFromFile();
closeFileRead();
}
public static void openFileRead() { // gets file for "read"
try {
input = new Scanner(Paths.get("Numbers.txt"));
} catch (IOException e) {
System.out.println("Unable to read file");
}
}
public static void openFileWrite() { // gets file for "write"
try {
output = new Formatter("Numbers.txt");
} catch (FileNotFoundException e) {
System.out.println("Unable to open file");
}
}
public static void readFromFile() {
while (input.hasNextInt()) {
int token = input.nextInt();
System.out.print(token);
}
}
public static void writeToFile() {
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 numbers");
try {
for (int i = 0; i < 10; i++) {
System.out.println("Another Number Please");
int total = input.nextInt();
output.format("%s%n", total);
}
} catch (InputMismatchException e) {
System.out.println("Please do not enter any letters");
writeToFile();
}
}
//required to close file for write
public static void closeFile() {
output.close();
}
//required to close file for read
public static void closeFileRead() {
input.close();
}
}
Just change your readFromFile method as:-
public static void readFromFile() {
double average = 0;
while (input.hasNextInt()) {
int token = input.nextInt();
average+=token;
}
System.out.println("Average ="+average/10);
}

The constructor Scanner(InputStream) is undefined

I have imported java.util.* but the IDE can't recognize Scanner. If I change it to import java.util.Scanner; it's fine. But I need it on java.util.* because the catch exception is part of java.util. This code is from a textbook by the way.
EDIT: I'm using Eclipse.
import java.util.*;
public class GetInteger {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter an integer: ");
int i = GetAnInteger();
System.out.println("You entered " + i);
}
public static int GetAnInteger() {
while(true) {
try {
return sc.nextInt();
}
catch (InputMismatchException e) {
sc.next();
System.out.println("That's not an integer. Try again: ");
}
}
}
}

How can I delay my program so there's a pause in between text?

Example of my code:
System.out.println("Why hello there!");
System.out.println("Welcome to 'Ancient Battles and Adventures!");
***DELAY WOULD GO HERE***
System.out.println("Now, what is your name?");
public class MyClass {
public static void main(String[] args) {
System.out.println("Why hello there!");
System.out.println("Welcome to 'Ancient Battles and Adventures!");
try {
Thread.sleep(x);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Now, what is your name?");
}
}
You can try this code for success
class ex
{
public static void main(String args[])
{
System.out.println("Why hello there!");
System.out.println("Welcome to 'Ancient Battles and Adventures!");
try
{
Thread.sleep(1000);// here you can delay for one second
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("Now, what is your name?");
}
}

interrupt class Main, if the method returns "false"

I would like to block a Card in my abstract ATM.
At this manner, I need to interrupt the program if the PIN wasn't accepted after the third attempt so that the Main class won't execute the next Methods.
Is it System.exit(0) the optimal decision? I chose this one because it's simple, but I'm not sure.
public boolean authenticity(int tries) {
if (tries <= 3)
{
short pin = sc.nextShort();
if (pin == 1234) {
System.out.println("PIN is correct");
System.out.println("Card is active for operation!");
return true;
} else {
System.out.println("PIN isn't correct! You used " + tries +" attempt!");
return authenticity(++tries);
}
}
System.out.println("\nCard was blocked!");
System.exit(0);
return false;
}
class Main looks so:
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
ATM atm = new ATM();
MasterCard aeroflotCard = new MasterCard();
atm.initCard(aeroflotCard);
aeroflotCard.authenticity(1); // if pin is wrong, than you are looser:)
System.out.println("\nRefill your balance:");
aeroflotCard.add(sc.nextInt());
aeroflotCard.balance();
}
You may try the following piece of code:
public boolean authenticity(int tries) throws yourException {
if (tries <= 3) {
// ...
} else {
throw new yourException("\nCard was blocked!");
}
}
In the main method:
public static void main(String[] args) {
try {
aeroflotCard.authenticity(1);
System.out.println("\nRefill your balance:");
aeroflotCard.add(sc.nextInt());
aeroflotCard.balance();
} catch (yourException e) {
System.err.println(e.getMessage());
}
}
In the yourException class:
public class yourException extends Exception {
// here you can override needed methods
}

Syntax error on token(s), misplaced construct(s) Code Help Needed

I use eclipse to help me code & I have been having issues with the error message "Syntax error on token(s), misplaced construct(s)" coming up, I'm not entirely sure what is wrong with my code.
The goal of this code is to write a program where a user enters their name and age and the program checks to see the age is between 0 and 125. If not, the program shows an error code (use Exception Class)
Here is my current code: Errors are showing up in lines 1 and 4
public class ThreadsUnitProject1 {
import java.lang.String;
import java.io.*;
public static void main(String args[]);
class InvalidAgeException extends Exception {
private static final long serialVersionUID = 1L;
public InvalidAgeException() {
super("The age you entered is not between 0 and 125");
}
}
class QuestionOne extends Thread {
public void main(String args[]) {
System.out.println("What is your name?");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name;
try {
name = br.readLine();
}
catch(IOException e) {
System.out.println("Error: " + e);
System.exit(1);
}
System.out.println("Hello " + name + ", how old are you?");
String i;
int age;
try {
i = br.readLine();
age = Integer.valueOf(i);
}
catch(IOException e) {
System.out.println("Error: " + e);
System.exit(1);
}
catch(InvalidAgeException e) {
System.out.println("Error: " + e);
System.exit(1);
}
finally {
System.out.println("No errors found.");
}
}
}
}
Thank you thank you thank you for all of your help, I have been coding for awhile, but I'm new to Java.
Thanks again!
-Kristen
public static void main(String args[]) is a method it needs to create a block with curly braces. It doesn't contain the block in the ThreadsUnitProject1 class.
public static void main(String args[]){}
Also the import statements should be outside the class declaration.
Full Example
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ThreadsUnitProject1 {
public static void main(String args[]) {
}
class InvalidAgeException extends Exception {
private static final long serialVersionUID = 1L;
public InvalidAgeException() {
super("The age you entered is not between 0 and 125");
}
}
class QuestionOne extends Thread {
public void main(String args[]) {
System.out.println("What is your name?");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String name = "";
try {
name = br.readLine();
} catch (IOException e) {
System.out.println("Error: " + e);
System.exit(1);
}
System.out.println("Hello " + name + ", how old are you?");
String i;
int age;
try {
i = br.readLine();
age = Integer.valueOf(i);
} catch (IOException e) {
System.out.println("Error: " + e);
System.exit(1);
} finally {
System.out.println("No errors found.");
}
}
}
}
Use {} after public static void main(String args[]), not ;.

Categories

Resources