954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

output numbers from a varible on different lines asap

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?

ultimate_fusion
Light Poster
44 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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("-----------------------");
 }
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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);
}
}

ultimate_fusion
Light Poster
44 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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);
}
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You