> How to ignore string in standard input? JAVA help!?

How to ignore string in standard input? JAVA help!?

Posted at: 2014-12-18 
You should use

input.next()

to get the next token (word or number).

Then you would test if the token is numeric. There are various ways of doing that.. You could use a try block. Or you could check each character in the token.

If you are saying that you KNOW each line will start with a word you want to ignore, you can simply read it in and move on to reading your ints.

The code you have doesn't make much sense.

Something like this should work:

import java.io.*;

import java.util.*;

public class PopulateArrayFromFile {

public static void main(String[] args) throws FileNotFoundException{

File f = new File("file.txt");

Scanner sc = new Scanner(f);

int[][] myArray = new int[3][3];

for (int i=0; i<3; i++) {

sc.next();

for (int j=0; j<3; j++) {

myArray[i][j]=sc.nextInt();

}

}

System.out.println("Array contents:");

for (int i=0; i<3; i++) {

for (int j=0; j<3; j++) {

System.out.print(myArray[i][j] + " ");

}

System.out.println();

}

}

}

Using scanner I would read this:

abc 10 10 10

def 9 9 9

xyz 2 3 4

How do I ignore the string and just read in the integers and put them in an array. Also is it possible to create 3 arrays from standard input? like

input = 3

int[] arrray = input.nextInt;

for( int i = 0; i < input.nextInt; i++){

array++;

}

Something like that.