heres the problem;

some one inputs a 4 digit number e.g 1234

I stored it as a string and i have to display it in a messagebox like

-----------------------------

the digits are

1

2

3

4

------------------------------------

How?

p.s i know how to do trhe input box and display, but dont know how to put one answer on different lines?

I think the easiest way to do this is to use substring(). Like so...

import java.io.*;

class MessageBoxInput
{
 public static void main(String[] args) throws IOException 
 {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String inData;

	System.out.println("Please input a 4 digit number");
	inData = br.readLine();
	int int1 = Integer.parseInt(inData.substring(0,1));
	int int2 = Integer.parseInt(inData.substring(1,2));
	int int3 = Integer.parseInt(inData.substring(2,3));
	int int4 = Integer.parseInt(inData.substring(3,4));
	
	System.out.println("-----------------------");
	System.out.println("\n" + int1);
	System.out.println("\n" + int2);
	System.out.println("\n" + int3);
	System.out.println("\n" + int4 + "\n");
	System.out.println("-----------------------");
 }
}

thanx mate but I need to output a message box can you fix the code below, you can input the number but it wont appear in the meesage box?

import java.io.*;
import javax.swing.JOptionPane;

class MessageBoxInput
{
 public static void main(String[] args) throws IOException 
 {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String inData;
    String Input = JOptionPane.showInputDialog(null,
    "enter a positive 4 digit number",
    "homework3",
    JOptionPane.QUESTION_MESSAGE);

    inData = br.readLine();
    int int1 = Integer.parseInt(inData.substring(0,1));
    int int2 = Integer.parseInt(inData.substring(1,2));
    int int3 = Integer.parseInt(inData.substring(2,3));
    int int4 = Integer.parseInt(inData.substring(3,4));

    JOptionPane.showMessageDialog(null,
    int1+
    "\n" + int2+
    "\n" + int3+
    "\n" + int4,
    "homework3",
    JOptionPane.QUESTION_MESSAGE);
 }
}

Edited 3 Years Ago by Dani: Formatting fixed

You had most of it right..Just a few lines needed to be left off.

import java.io.*;
import javax.swing.JOptionPane;

class MessageBoxInput
{
public static void main(String[] args) throws IOException 
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String inData = JOptionPane.showInputDialog(null,
"enter a positive 4 digit number",
"homework3",
JOptionPane.QUESTION_MESSAGE);

int int1 = Integer.parseInt(inData.substring(0,1));
int int2 = Integer.parseInt(inData.substring(1,2));
int int3 = Integer.parseInt(inData.substring(2,3));
int int4 = Integer.parseInt(inData.substring(3,4));

JOptionPane.showMessageDialog(null,
int1+
"\n" + int2+
"\n" + int3+
"\n" + int4,
"homework3",
JOptionPane.QUESTION_MESSAGE);
}
}
This article has been dead for over six months. Start a new discussion instead.