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: | 314 Weeks 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