Konwerter liczb z dziesiętnym na inne

0

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


        
        
}

0
int base = 0;

       //base is too big or too small
       if ((base > MAX_BASE) || (base < 2))
          return null;

Nie dziwne, że zawsze zwraca null, chyba (n < 2).

0

@lion137:



import java.util.Scanner;


public class konwerter {

	private static final int MAX_BASE = 36;
	private static final String pattern = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            
        
        
	//	returns converted n or null if an error occurs
	public static String convertTo(int n, int base)
	{
	   String result = "";
          
	   //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;
	}

	public static void main(String[] args) {
              int liczba = 0;
        String bin, oct, hex;
        Scanner sc = new Scanner(System.in);
		   
            System.out.println("Metody do zamiany liczby dziesietnej całkowitej na bin, oct, hex");
            System.out.print("\nPodaj liczbę: ");
            liczba = sc.nextInt(); 
            bin = convertTo(liczba, 2);
           System.out.println("W systemie dwojkowym = " + bin);
           oct = convertTo(liczba, 8);
	   System.out.println("W systemie osemkowym = " + oct);
           hex = convertTo(liczba, 16);
	   System.out.println("W systemie szesnastkowym = " + convertTo(liczba, 16));

	}

}

0

@lion137: a jak za zmienną base podstawię co innego niż 0, to zwraca wartość np. 36 to zwraca tylko 10

0

@lion137: sory panowie, temat ogarnięty - błąd był w definicji zmiennych funkcji public static String convertTo(int n, int base)

1 użytkowników online, w tym zalogowanych: 0, gości: 1