Por favor, me ajudem a encontrar o erro no código

Boa tarde ,

O seguinte código está retornando um valor numérico no final sendo que a idéia é substituir por strings.
Alguém poderia me ajudar a encontrar o erro e verificar se existe uma alternativa mais simples de implementá-lo ? :slight_smile:

Segue o código :


import java.io.IOException;
import java.util.Scanner;


public class Main {

	public static void main(String[] args) throws IOException {
		Scanner leitura = new Scanner(System.in);
		int tipoini = 0 , tipofim = 0 ;
		String tipoi = "" , tipof = "";

		do {
			
			System.out.println("Entre com o tipo de valor Inicial");
			System.out.println("1 - Decimal.");
			System.out.println("2 - Octal.");
			System.out.println("3 - Hexadecimal.");
			System.out.println("4 - Binario");
			tipoini = Integer.parseInt(leitura.next());
			tipoi = Integer.toString(tipoini);
			
			System.out.println("Entre com o tipo de valor Final");
			System.out.println("1 - Decimal.");
			System.out.println("2 - Octal.");
			System.out.println("3 - Hexadecimal.");
			System.out.println("4 - Binario");
			tipofim = Integer.parseInt(leitura.next());
			tipof = Integer.toString(tipofim);
			
			if (tipoini == tipofim){
				System.out.println("Os tipos de valores devem ser diferentes !");
		    	System.in.read();
		    }
			
	} while (tipoini == tipofim);

		if (tipoini == 1)tipoi = "Decimal";
		if (tipoini == 2)tipoi = "Octal";
		if (tipoini == 3)tipoi = "Hexaecimal";
		if (tipoini == 4)tipoi = "Binário";
		if (tipofim == 1)tipof = "Decimal";
		if (tipofim == 2)tipof = "Octal";
		if (tipofim == 3)tipof = "Hexaecimal";
		if (tipofim == 4)tipof = "Binário";
		

	System.out.println("Digite o valor " + tipoi + " a ser convertido para " + tipof);
	int n1 = Integer.parseInt(leitura.next());

}

}

Att,

tem um número >1<, jogado no meio do código

#             if (tipoini == tipofim){1  <<<< ó ele aqui
#                 System.out.println("Os tipos de valores devem ser diferentes !");  
#                 System.in.read();  
#             }  

tirando isto, seu programa não faz a conversão. Falta a lógica de conversão.

Ai um exemplo so implementar a função de converter

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUI.java
 *
 * Created on 11/03/2010, 16:52:30
 */
package javaapplication8;

import javax.swing.JOptionPane;

/**
 *
 * @author suporte
 */
public class GUI extends javax.swing.JFrame {

    /** Creates new form GUI */
    public GUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        tipo1 = new javax.swing.JComboBox();
        tipo2 = new javax.swing.JComboBox();
        valor = new javax.swing.JTextField();
        b_resultado = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txt_tipo1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_tipo2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txt_result = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tipo1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Decimal", "Octal", "Hexadecimal", "Binario" }));
        tipo1.setSelectedIndex(2);

        tipo2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Decimal", "Octal", "Hexadecimal", "Binario" }));

        b_resultado.setText("Resultado");
        b_resultado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_resultadoActionPerformed(evt);
            }
        });

        jLabel1.setText("Converter ");

        txt_tipo1.setForeground(java.awt.Color.red);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tipo1, org.jdesktop.beansbinding.ELProperty.create("${selectedItem}"), txt_tipo1, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel2.setText("para");

        txt_tipo2.setForeground(java.awt.Color.red);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tipo2, org.jdesktop.beansbinding.ELProperty.create("${selectedItem}"), txt_tipo2, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel3.setText("=");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tipo1, javax.swing.GroupLayout.Alignment.LEADING, 0, 306, Short.MAX_VALUE)
                    .addComponent(tipo2, javax.swing.GroupLayout.Alignment.LEADING, 0, 306, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_tipo1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_tipo2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_result, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(valor, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(b_resultado)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txt_tipo1, txt_tipo2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tipo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tipo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(b_resultado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt_tipo1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txt_tipo2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txt_result, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>

    private void b_resultadoActionPerformed(java.awt.event.ActionEvent evt) {
        if (tipo1.getSelectedIndex() != tipo2.getSelectedIndex()) {
            Object obj = valor.getText();
            //Logica de converção
            Object convertido = null;
            txt_result.setText(convertido.toString());
        } else {
            JOptionPane.showMessageDialog(this, "Tipos devem ser diferentes");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton b_resultado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JComboBox tipo1;
    private javax.swing.JComboBox tipo2;
    private javax.swing.JLabel txt_result;
    private javax.swing.JLabel txt_tipo1;
    private javax.swing.JLabel txt_tipo2;
    private javax.swing.JTextField valor;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration
}


O código já está corrigido obrigado,
quanto a conversão , irei programa-la posteriormente …
Mas o problema ainda não foi solucionado pois oque eu preciso é que a saída de:

System.out.println("Digite o valor " + tipoi + " a ser convertido para " + tipof);

seja por exemplo :

Digite o valor decimal a ser convertido para octal

e está saindo assim :

Digite o valor 1 a ser convertido para 2

Obrigado pela boa vontade … mas ainda estou engatinhando em e não consegui entender quase nada deste código . :?

Obrigado pela a ajuda … consegui a saída desejada . Segue o código.
Estou tentano fazer um passo a passo para entender as classes métodos, instâncias , estruturas de controle, objetos etc …
Sei que já existe o cóigo pronto mas tentarei aprender escrevendo um e implementando aos poucos .
Obrigado a todos !

import java.util.Scanner;

public class Main {

	public static void main(String[] args){
		Scanner leitura = new Scanner(System.in);
		int tipoini = 0 , tipofim = 0 ;
		String tipoi = "" , tipof = "";

		do {
			
			System.out.println("Entre com o tipo de valor Inicial");
			System.out.println("1 - Decimal.");
			System.out.println("2 - Octal.");
			System.out.println("3 - Hexadecimal.");
			System.out.println("4 - Binario");
			tipoini = Integer.parseInt(leitura.next());
			tipoi = Integer.toString(tipoini);
			
			System.out.println("Entre com o tipo de valor Final");
			System.out.println("1 - Decimal.");
			System.out.println("2 - Octal.");
			System.out.println("3 - Hexadecimal.");
			System.out.println("4 - Binario");
			tipofim = Integer.parseInt(leitura.next());
			tipof = Integer.toString(tipofim);
			
			if (tipoini == tipofim){
				System.out.println("Os tipos de valores devem ser diferentes !");
		    }
			
	} while (tipoini == tipofim);

		if (tipoini == 1)tipoi = "Decimal";
		if (tipoini == 2)tipoi = "Octal";
		if (tipoini == 3)tipoi = "Hexaecimal";
		if (tipoini == 4)tipoi = "Binário";
		if (tipofim == 1)tipof = "Decimal";
		if (tipofim == 2)tipof = "Octal";
		if (tipofim == 3)tipof = "Hexaecimal";
		if (tipofim == 4)tipof = "Binário";
		

	System.out.println("Digite o valor " + tipoi + " a ser convertido para " + tipof);
	int n1 = Integer.parseInt(leitura.next());

}

}