0

This while loop just loops forever. I looked up solutions and attempted to add something that consumes the input, however this did not help. The printf "readDONEZO" is not printed.

Here is my code

public void read(Scanner stdin) {
    int sRow = 0;
    while ( stdin.hasNextLine() ) {
        String theLine = stdin.nextLine();
        String[] split = theLine.split(",");

        int size = split.length; //how many values in array = 3
        for(int i = 0; i < size ; i++){
            String value = split[i];
            int sColumn = i;
            setCellValue(sRow, sColumn, value);
            System.out.printf("%s", getCellValue(sRow,sColumn));
            if (i+1 != size) {
            System.out.printf(",");
            }
        }   
        sRow += 1;
        System.out.printf("\n");
    }
    System.out.printf("readDONEZO\n");
}

Main

import java.io.*;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    int rows = 100;
    int columns = 26;

    StringSpreadsheet a = new StringSpreadsheet(rows, columns);

    Scanner stdin = new Scanner(System.in);
    a.read(stdin);
    System.out.printf("out of read\n");
    a.write();
}   
}

Class

import java.util.Scanner;

public class StringSpreadsheet {

private int rows;
private int columns;
private String[][] cells;
private int allRows;
private int allColumns;

public StringSpreadsheet(int rows, int columns) {
    allColumns = 0;
    allRows = 0;
    this.rows = rows;
    this.columns = columns;
    cells = new String[this.rows][this.columns];
}

public void read(Scanner stdin) {
    while ( stdin.hasNextLine() ) {
        String theLine = stdin.nextLine();
        String[] split = theLine.split(",");

        allColumns = split.length; //how many values in array = 3
        for(int i = 0; i < allColumns ; i++){
            String value = split[i];
            int sColumn = i;
            setCellValue(allRows, sColumn, value);
            System.out.printf("%s", getCellValue(allRows,sColumn));
            if (i+1 != allColumns) {
            System.out.printf(",");
            }
        }   
        allRows += 1;
        System.out.printf("\n");
    }
    System.out.printf("readDONEZO\n");
}



public void write() {                                                           
    for (int i = 0 ; i < allRows ; i++){                                                    
        for(int j = 0 ; j < allColumns ; j++){                                                      
            String value = getCellValue(i, j);
            if ()
            System.out.printf("%s,", value);
        }
    }
}

public String getCellValue(int gRow, int gColumn) {
    return cells[gRow][gColumn];
}

public void setCellValue(int sRow, int sColumn, String value) {
    cells[sRow][sColumn] = value;
}
}
Bartholomas
  • 51
  • 1
  • 10
  • Just for the record: calling a variable stdin is a bit confusing. Or do you think you really do not want to use this method for a Scanner that comes from a different source? – GhostCat Sep 18 '16 at 08:32
  • What is the source you are reading? Is it file, or maybe `System.in`? – Pshemo Sep 18 '16 at 08:38
  • I tried your code and it works fine (I commented the two lines related to cells). Is it possible to see how do you call the method? – acornagl Sep 18 '16 at 08:42
  • If your EOF is an `empty newline`, break the loop when `theLine` equals an `empty newline` – pahan Sep 18 '16 at 08:43
  • [Try this](http://stackoverflow.com/questions/25683299/how-do-i-read-input-until-eof-in-java-for-this-program-on-spoj) – pahan Sep 18 '16 at 08:44
  • input 1,0,0 0,2,0 0,0,3 4,0,0 – Bartholomas Sep 18 '16 at 09:12
  • Since `System.in` is always opened, `hasNextLine` can't determine if there will be next line or not. So it is blocking your code flow until next line will be provided, or it will detect that stream it is reading has no more values (like when it is closed). So if you want to get finite number of iterations avoiding `while(scanner.hasNextLine()){...}` loop if scanner is reading from potentially infinite stream (like from server which is constantly streaming some data like chat messages). – Pshemo Sep 18 '16 at 10:23
  • Instead use some predefined values which will represent end of data (like line holding "exit" word which you will check to decide if you should finish looping), or before reading lines require from user information about amount of lines. – Pshemo Sep 18 '16 at 10:23

1 Answers1

-2

The problem in your code is that you never close the scanner because the stdin is always ready to receive new input.

For this reason, the stdin.hasNextLine() while condition is always true and it makes your while loop an endless loop. If you substitute the input of the scanner (System.in) with the path to a file in your workstation the example will work fine because the file has a final line.

acornagl
  • 521
  • 5
  • 24