Ask Experts Questions for FREE Help !
    imuser123456's Avatar
    imuser123456 Posts: 1, Reputation: 1
    New Member

    Jun 29, 2014, 06:19 AM
    How to store Scanner data in string in java?
    I am new to java, currently I am working on a program regarding networking.
    My program compiles without error.

    I have placed 3 checks in my program. It passes through check 1 but does not reach 2.
    I want string player2msg to store whatever data comes from scanner fromServer.
    I expected player2Msg = fromServer.nextLine(); to work but it does not.

    import java.util.*;

    public class newClient {

    static Scanner shipPlace = new Scanner(;
    static Scanner shipAlignment = new Scanner(;

    public static void main(String[] args) throws Exception{
    Socket servConn = new Socket("localhost", 10176);
    Scanner fromServer = new Scanner(servConn.getInputStream());
    Scanner fromUser = new Scanner(; // command line input.
    PrintWriter toServer = new PrintWriter(servConn.getOutputStream(), true);
    BattleBoard player2 = new BattleBoard();
    int hit;
    boolean gameOver;
    String player1msg;
    int x_cor_target;
    int y_cor_target;
    String gameEnd = "All of your ships destroyed.";
    String reHit = "Already shot at target. Please specify another target";
    int player2Moves = 0;
    int newCounter = 0;
    boolean myMessage = false;
    boolean gameStart = false;
    String player2Msg;

    System.out.println("A connection has been made. Please wait while other player plant ships. \n");
    System.out.println("Plant your own ships");

    for (int I = 0; I<5; I++) {
    newCounter = plantNavy(player2,I);

    System.out.println("Please wait while other player plant ships");

    player1msg = "Player2: I have planted my ships";
    fromUser = new Scanner(player1msg);

    while(true) {
    try {
    if (fromServer.hasNext()) {
    System.out.println("Server Says: " + fromServer.nextLine());
    player2Msg = fromServer.nextLine();
    if (player2Msg.equals("Player1: I have planted my ships")) {
    gameStart = true;
    } catch(Exception e) {

    if (myMessage == true && gameStart == true) {
    fromUser = new Scanner(;

    if (fromUser.hasNext()) {

Check out some similar questions!

Having difficulty with Java String [ 1 Answers ]

Can someone please me how I can write a Java code to transform the quantity 6 As into the string AAAAAA. I'm trying this, but it does not work: public class Letters { public static void main (String args) { String words=" "; String numberofwords="A"; int wordcount=6;

Auto fit of data in excel cell using java [ 2 Answers ]

I am using BIRT technology, Can you help to Autofit the size of column width with longest data ?

How to store/get data from the access database that is located on any connected PC [ 0 Answers ]

I want to develop an application in java in which login table is located on server and client will log in by verifying username and password from that login table made in ms access. *** help me

Store Data [ 1 Answers ]

deign a class called date. The class should store a date in three integers month day and year. There should be members functions to print the date in the following forms: 12/25/2006 decemeber 25, 2006 25 December 2006

View more questions Search

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.