The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 3 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
question about Java file IO input and standard input.

![]()
![]()
![]()
![]()
![]()



........................................................
public class Pokemon {
final public String name;
final public String type;
private int hp;
private int attack;
private int defense;
private int speed;
public Pokemon(String n, String t) {
this.name = n;
this.type = t;
}
public Pokemon(String n, String t, int hp, int attack, int defense, int speed) {
this.name = n;
this.type = t;
this.hp = hp;
this.attack = attack;
this.defense = defense;
this.speed = speed;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
.............................................................
public class Main {
public static void main(String[] args) {
TweetBuilder builder = new TweetBuilder("@mytwitterhandle");
builder.displayName("Fred Nista")
.date("03/21/2016")
.message("This is a tweet");
Tweet mytweet = builder.build();
System.out.println(mytweet.toString());
}
}
...........................................
public class Tweet {
final public String handle;
final public String displayName;
final public String date;
private String message;
public Tweet(String handle, String displayName, String date) {
this.handle = handle;
this.displayName = displayName;
this.date = date;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String toString() {
StringBuilder builder = new StringBuilder(handle);
builder.append("\n")
.append(displayName)
.append("\n")
.append(date)
.append("\n")
.append(message);
return builder.toString();
}
}
...................................
public interface TweetLoader {
public void save(Tweet tweet, String location);
public Tweet load(String location);
}
.............................................
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
// Compile and run this program to see how Java does IO.
public class JavaIO {
public static void main(String[] args) {
// Prompt the user for name
System.out.print("Your name: ");
// Create a Scanner that reads from System's standard in
Scanner stdin = new Scanner(System.in);
// Read a line from the user's keyboard input.
String name = stdin.nextLine();
// Output the name to standard out.
System.out.println("Hello, " + name);
String fileLocation = "name.txt";
// Create or overwrite a file called "name.txt" and put the name and numbers 0 ~ 9 in it.
try (PrintWriter out = new PrintWriter(fileLocation)) {
out.println(name);
for (int i = 0; i < 10; i++) {
out.println(i);
}
} catch (FileNotFoundException e) {
System.err.println("Writing to a file failed. Cause: " + e.getMessage());
System.exit(-1); //If something goes wrong, let's terminate the program.
}
// Create a Path object from our file location
Path path = Paths.get(fileLocation);
try {
// Read from the Path and print all the lines.
List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
for (String line : lines) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println("Reading from a file failed. Cause: " + e.getMessage());
System.exit(-1); //If something goes wrong, let's terminate the program.
}
}
}
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly