d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Question ~~ Arrays / Txt Files
12Next
Add Reply New Topic New Poll
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 22 2013 01:04pm
I need to copy 10 messages from my .txt file using the scanner. I then need to put them into an array, so that later I can I can print a message that has a chance of being any of the 10.
I have tried a few different things, but I do not know much in this area.. So far I have

Code
public static void main(String[] args) {

 try {
  BufferedReader reader = new BufferedReader(new FileReader(new File("winningmessages.txt")));
  List list = new ArrayList();
  String line = reader.readLine();
  while(line != null){
   list.add(line);
   System.out.println(line);
   line = reader.readLine();
  }
  reader.close();
  String[]records = (String[])list.toArray(new String[]{""});
 } catch(Exception ex){
  System.out.println(ex);
 }


The println prints all of the lines at once.. I am trying to store them into an array so that later I can call on the array to display 1 random message.
I was given a hint it should look something like this:
System.out.println(winningMessages[myRand.nextInt(10)]);

Was wondering if anyone could explain how to store this info from text files into an array.. I will have to do this again for losing messages.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Feb 22 2013 01:30pm
The code you have there works fine for storing the lines in an array.
This line will print a random value from the array...
System.out.println(records[new Random().nextInt( records.length )]);

This post was edited by labatymo on Feb 22 2013 01:32pm
Member
Posts: 4,024
Joined: Apr 1 2009
Gold: 64,464.00
Feb 22 2013 02:08pm
Well once you have all of the lines of data stored into the array isn't the remaining task simple?

Just print out some string inside winningMessages[a] // where a is a randomly generated integer between 0 and 9.
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 22 2013 02:15pm
When I get back I will try this. It may have just been this simple. I suppose I was just complicating something simple.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Feb 22 2013 02:18pm
no need to convert the thing to a string array. just use arraylist.get()....
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 22 2013 04:38pm
Quote (labatymo @ Feb 22 2013 01:30pm)
The code you have there works fine for storing the lines in an array.
This line will print a random value from the array...
System.out.println(records[new Random().nextInt( records.length )]);


Ok, so this is what I have
Code
try {
  BufferedReader reader = new BufferedReader(new FileReader(new File("winningmessages.txt")));
  List list = new ArrayList();
  String line = reader.readLine();
  while(line != null){
   list.add(line);
   //System.out.println(line);
   
   
   line = reader.readLine();
  }
  reader.close();
  String[]records = (String[])list.toArray(new String[]{""});
  System.out.println(records[new Random().nextInt( records.length )]);
 } catch(Exception ex){
  System.out.println(ex);
 }


It works. But I need this random line to generate within my actual code. When I try to copy paste the println, the variable "records" cannot be found...
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Feb 22 2013 04:47pm
zzz

post the rest of your code

This post was edited by irimi on Feb 22 2013 04:47pm
Member
Posts: 4,024
Joined: Apr 1 2009
Gold: 64,464.00
Feb 22 2013 04:48pm
Quote (irimi @ Feb 22 2013 06:47pm)
zzz

post the rest of your code


this
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 22 2013 04:53pm
Its a lot, and since its a project I really didn't want to >.>
rather than just saying type this + this, I was hoping someone could explain what does what. The above part with the bufferedreader, etc is all new and I dont understand much about it.
/e Also keep in mind, that i am very beginner. Which is why this probably looks/ is terrible.



Code
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
public class DiceGame2 {

public static void main(String[] args) {

 try {
  BufferedReader reader = new BufferedReader(new FileReader(new File("winningmessages.txt")));
  List winninglist = new ArrayList();
  String line = reader.readLine();
  while(line != null){
   winninglist.add(line);
   //System.out.println(line);
   
   
   line = reader.readLine();
  }
 
 
 
  reader.close();
 
 
  //String[]records = (String[])winninglist.toArray(new String[]{""});
 // System.out.println(records[new Random().nextInt( records.length )]);
 } catch(Exception ex){
  System.out.println(ex);
 }
 
 
 double userCash;
 Scanner input = new Scanner(System.in);
 System.out.println("Please type your first name to begin");
 String user = input.next();
 System.out.println("Hello " + user + " and welcome to the game!");
 
 for (userCash = 10.00; userCash > 0.00;) {
 
 
 
  System.out.println(" ");
  System.out.println(" ");
  System.out.println("Your Current total is: " + userCash + ". Please select a choice.");
  System.out.println(" ");
  System.out.println("CHOICE:  MEANING:      FIXED BET AMOUNT: ");
  System.out.println("even  The total roll is going to be even   $0.50");
  System.out.println("odd  The total roll is going to be odd   $0.50");  
  System.out.println("fib  The total roll is a Fibonacci number   $1.00");  
  System.out.println("double  The two dice will have the same number   $2.00");  
  System.out.println("exact  The user is able to specify the exact dice roll  user's choice");  
  System.out.println("done  Quits game");
  System.out.println(" ");
 
 
  String uc = input.next().toLowerCase();
  if("done".equalsIgnoreCase(uc))
   break;
  switch(uc){
  case "even":
   System.out.println("You chose even!");
   
   Random diceRoll; diceRoll = new Random();
   for (int i = 0; i < 1; ++i) {
     int roll1 = diceRoll.nextInt(6) + 1;
     int roll2 = diceRoll.nextInt(6) + 1;
     int totalRoll = roll1 +roll2;
     
     System.out.println("The role of the dice was: " +totalRoll);  
   if (totalRoll % 2 == 0){
     userCash = userCash + .50;

    }else {
     userCash = userCash - .50;
    }
   }
     break;
   
  case "odd":
   System.out.println("You chose odd!");
   
   Random diceRoll2; diceRoll2 = new Random();
   for (int j = 0; j < 1; ++j) {
     int roll3 = diceRoll2.nextInt(6) + 1;
     int roll4 = diceRoll2.nextInt(6) + 1;
     int totalRoll2 = roll3 +roll4;
     
     System.out.println("The role of the dice was: " +totalRoll2);
   if (totalRoll2 % 2 != 0){
    userCash = userCash + .50;
    }else {
     userCash = userCash - .50;  
    }
   }
   break;
   
  case "fib":
   System.out.println("You chose fibonacci");
   
   Random diceRoll3; diceRoll3 = new Random();
   for (int k = 0; k < 1; ++k) {
      int roll5 = diceRoll3.nextInt(6) + 1;
      int roll6 = diceRoll3.nextInt(6) + 1;
      int totalRoll3 = roll5 +roll6;
     
      System.out.println("The role of the dice was: " +totalRoll3);
    if ((totalRoll3 == 2) || (totalRoll3 == 3) || (totalRoll3 == 5) || (totalRoll3 == 8)){
     userCash = userCash + 1.00;
     }else {
      userCash = userCash - 1.00;
     }
   }
   break;
   
  case "double":
   while (userCash < 2.00){
    System.out.println("You do not have enough money. Choose a cheaper bet!");
     input.next().equals(uc);
     
   }
   System.out.println("You chose double");
   
   Random diceRoll4; diceRoll4 = new Random();
   for (int jk = 0; jk < 1; ++jk) {
     int roll7 = diceRoll4.nextInt(6) + 1;
     int roll8 = diceRoll4.nextInt(6) + 1;
     int totalRoll4 = roll7 +roll8;
     
     System.out.println("The role of the dice was: " +totalRoll4);
  if (roll7 == roll8){
   userCash = userCash + 2.00;
   }else {
    userCash = userCash - 2.00;
    }
   }
   break;
   
  case "exact":
   System.out.println("You chose exact");
   System.out.println("Please guess the total roll of the dice. Numbers between 2-12");
   int userGuess = input.nextInt();
   while (userGuess < 2 || userGuess > 12){
   
    System.out.println("Choose a number BETWEEN 2-12. Try again");
    userGuess = input.nextInt();
   }
 
   System.out.println("Please input how much you would like to bet:");
   double userBet = input.nextDouble();
   while (userBet > userCash){
    System.out.println("You clearly do not have enough money to bet that amount, try again.");
    userBet = input.nextDouble();
   }
   System.out.println("You chose to bet: " + userBet);
   
 Random diceRoll5; diceRoll5 = new Random();
 for (int g = 0; g < 1; ++g) {
  int roll9 = diceRoll5.nextInt(6) + 1;
  int roll10 = diceRoll5.nextInt(6) + 1;
  int totalRoll5 = roll9 +roll10;
   
  System.out.println("The role of the dice was: " +totalRoll5);
 if (userGuess == totalRoll5){
  userCash = userCash + userBet;
  }else {
   userCash = userCash - userBet;
  }
 }
 break;
 
 default: System.out.println("You're in college! It is not difficult to correctly spell a word. Try again!");
   
   }
  }  
 }
}


This post was edited by Malexir on Feb 22 2013 04:56pm
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Feb 22 2013 05:08pm
In your origional code, you were storing the list into an array called records, but looking at your full code, you're just using the list. So replace the System.out.println(records[new Random().nextInt( records.length )]) line with this...
System.out.println(winninglist.get(new Random().nextInt( winninglist.size())));

This post was edited by labatymo on Feb 22 2013 05:09pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll