Witam,
czy ktoś dałby radę zerknąć na poniższy kod, dlaczego nie dochodzi do konwersji liczby tylko program zwraca wartość null. Będę wdzięczny za sugestie.
/*
* Copyright by Tomasz Myszak
* made on 03/18/21
program który konwertuje liczy do rożnych systemów reprezentacji
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
//
// Konwersja z i na system dziesietny
//
// www.algorytm.org
// (c)2006 by Tomasz Lubinski
//
// Use upper Case in the start of you class names:
public final class konwersjaliczb extends JPanel implements ActionListener {
private JTextField textf;
private JTextArea textf1;
private static final int MAX_BASE = 36;
private static final String pattern = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public konwersjaliczb() {
jhand();
}
public void jhand() {
setLayout(new FlowLayout()); // Zawsze ustawiaj układ przed dodaniem komponentów
// możesz użyć układu null, ale musisz użyć metody setBounds ()
// do umieszczania komponentów.
textf = new JTextField(); // utworzenie nowego pola tekstowego
textf.setPreferredSize(new Dimension(400, 30)); //określ wielkość pola
textf.getText();
textf.setEditable(true); // aktywacja pola na wpisanie z klawiatury
textf.setBackground(Color.WHITE);
textf.setForeground(Color.RED);
add(textf);
JTextArea txtf1 = new JTextArea();
//txtf1.setSize(40, 40); Use setPreferredSize instead
txtf1.setPreferredSize(new Dimension(400, 200));
InputStream in = getClass().getResourceAsStream("content.txt");
try {
txtf1.read(new InputStreamReader(in), null);
} catch (IOException e) {
}
txtf1.setEditable(false);
txtf1.setBackground(Color.WHITE);
txtf1.setForeground(Color.BLACK);
add(txtf1);
JButton b = new JButton("ZMIEŃ");
b.addActionListener(this);
b.setLayout(null);
b.setVisible(true);
add(b);
}
//funkcja zwraca liczbę wpisaną w pole textowe przekonwertowaną
public static String convertTo(long text, int n)
{
String result = "";
int base = 0;
//base is too big or too small
if ((base > MAX_BASE) || (base < 2))
return null;
//n is equal to 0, do not process, return "0"
if (n == 0)
return "0";
//process until n > 0
while (n>0)
{
result = pattern.charAt(n % base) + result;
n /= base;
}
return result;
}
// return value of x or -1 if an error occurs
private static int valueOf(char x, int base)
{
for (int i=0; i<base; i++)
{
if (x == pattern.charAt(i))
{
return i;
}
}
return -1;
}
@Override
public void actionPerformed(ActionEvent evt) {
//String text = textf.getText();
long text;
text = Integer.parseInt(textf.getText());
JOptionPane.showMessageDialog(konwersjaliczb.this, "\n" + "w systemie dwojkowym = " + convertTo(text, 2) + "\n" +"w systemie osemkowym = " + convertTo(text, 8) + "\n" + "w systemie szesnastkowym = " + convertTo(text,16));
}
public static void main(String[] args) {
JFrame frame = new JFrame("KONWERSJA LICZB");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
konwersjaliczb p = new konwersjaliczb();
p.setBackground(Color.WHITE);
frame.setContentPane(p);
frame.setSize(500,200);
frame.setVisible(true);
}
}