TRENDING NEWS

POPULAR NEWS

How To Output An Array To A File Using Java

How do I read a file to a 2D array in Java?

I am trying to read a Sudoku file that looks like this:
. . . 9 . 2 6 . 4
6 . 4 3 . . . 7 .
. 7 . 1 . 4 . . .

. . 3 . 1 9 8 . .
1 5 . . 4 . . 9 7
. . 7 8 2 . 3 . .

. . . 2 . 6 . 5 .
. 3 . . . 7 1 . 2
9 . 2 5 . 1 . . .

into a 2D array. This is the code I have so far:

//ask for user input of filename and scan as string.
System.out.println ("Enter the name of the maze file:");
String filename = new Scanner(System.in).nextLine().trim();
Scanner puzzleScanner = new Scanner(new File(filename));

//Create a new puzzle object to test the puzzle file.
Sudoku p = new Sudoku(puzzleScanner);

private int number;

public Sudoku (Scanner input)
{
int [][] board = new int [8][8];

for (int r = 0; r < 8; r++)
{
for (int c = 0; c < 8; c++)
{
if (input.hasNext())
{
number = (char)input.nextInt();
board[r][c] = number;
}
}

System.out.println();
}
}

I ask the user for a filename, then I am having difficulty reading that file and storing the values into the array. I'm wondering if the white spaces inbetween each number affects the process or if those are ignored automatically. Do I need to trim? Also, I need to print out the above just how it is to the screen, but I'm having a hard time. I had code previously that printed out all 0's, but that was obviously incorrect. Any help would be very much appreciated. Thank you. :D

Reading a Text File and Creating Array in Java?

Hi,

I'm very new to Java and have no idea what to do with this assignment. I am asked to create an array of objects after reading in this text file.

circle cr1 3
triangle tr2 3 4 5
parallelogram prl 3 6 30
rectangle rec 4 6
rhombus r1 3 45
square s1 5
rpolygon rpg4 5 34
rectangle rec4 3 9
circle circ2 10
triangle trg3 5 5 5
parallelogram prllg1 5 7 60

The first number indicates how many are in the array. The rest of the terminology isn't relevant to my question. I just want to know how to read it in.

I am given this to help:
import java.io.*;
import java.util.* ;
/*
* This class reads lines from an input file and show how the Scanner class can be used to parse strings separated by spaces.
* The files "shape_database.txt" and "output.txt" are used for reading and writing.
*
*/
class IO
{ public static void main(String args[])
{
try
{
FileInputStream fstream = new FileInputStream("shape_database.txt");
// DataInputStream in = new DataInputStream(fstream);
BufferedReader br=new BufferedReader(new InputStreamReader(fstream));
String line;
while ( (line = br.readLine()) != null )
{
Scanner sc = new Scanner(line);
System.out.println (sc.next());
//Use the following code to read more from the line.

/* if(further words in the line are to be scanned then call sc.next() again)
System.out.println (sc.next());
*/
}
br.close();
} catch (Exception e) { System.err.println("File input error");}



// Now let us print something to the output


FileOutputStream out;
PrintStream p;
try {
out = new FileOutputStream("output.txt");
p = new PrintStream( out );
p.println ("This is written 1 "); // These 2 lines are printed in the output.txt file.
p.println ("This is written 2 ");
p.close();
}
catch (Exception e) { System.err.println ("Error writing to file"); }


}
}

The array also has to be of base class "shape"

Thanks so much for anyone's help,
Geoff.

How do you make a GIF file in Java using an array of BufferedImages?

StackOverflow to the rescue. Here you go:Animated GIF from BufferedImagesHe's provided the code as a link, but also shown usage in the answer itself.public static void main(String[] args) throws Exception
{
if (args.length > 1)
{
// grab the output image type from the first image in the sequence
BufferedImage firstImage = ImageIO.read(new File(args[0]));

// create a new BufferedOutputStream with the last argument
ImageOutputStream output = new FileImageOutputStream(new File(args[args.length - 1]));

// create a gif sequence with the type of the first image, 1 second
// between frames, which loops continuously
GifSequenceWriter writer = new GifSequenceWriter(output, firstImage.getType(), 1, false);

// write out the first image to our sequence...
writer.writeToSequence(firstImage);
for(int i=1; i {
BufferedImage nextImage = ImageIO.read(new File(args[i]));
writer.writeToSequence(nextImage);
}
writer.close();
output.close();
}
else
{
System.out.println("Usage: java GifSequenceWriter [list of gif files] [output file]");
}
}

How do I save console output to String Java?

If you want to save console data within the java program then here it goes. I have added comments on each line to explain it. Even you don’t get it, ask in comments.import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class Simple {

private static PrintStream originalStream;
private static PrintStream newStream;
private static ByteArrayOutputStream newConsole = new ByteArrayOutputStream();

static {

// store original printstream
originalStream = System.out;

// set new print stream, now anything written via
// System.out will be stored in the byte array 'newConsole'
newStream = new PrintStream(newConsole);
System.setOut(newStream);
}

public static void main(String[] args) {

// anything written via System.out will go to new stream
System.out.println("data on new stream line 1");
System.out.println("data on new stream line 2");

// Change stream to original, now anything written
// via System.out will go to console.
System.setOut(originalStream);

// it goes to console
System.out.println("on console");

// flush data to new console
newStream.flush();

// retrieve data as string from new console,
// now you can use it as you want
String newStreamData = newConsole.toString();

// print new console data on original console
System.out.println("****Retrieved from new Stream****");
System.out.println(newStreamData);

// close the newStream
newStream.close();

}
}
And here is the output of the programon console
****Retrieved from new Stream****
data on new stream line 1
data on new stream line 2

Java 2D Array Matrix Help.?

I have to create a program that reads in a String from a file and outputs the word in hourglass form, sort of like this:


HELLO
E L
L
E L
HELLO

Trouble is, I have NO clue how to start. I know I have to use a 2D array, but that's about it.

If anyone could be of any assistance to help me get started, I would greatly appreciate it.

Java: Create 5 parallel arrays filled with info from text input file?

Here's the method: http://stackoverflow.com/questions/12571013/parallel-arrays-to-store-counts-of-chars-user-enters-5-chars-also-create-an-arr

TRENDING NEWS