Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help finding problem
#1
I have been following these tutorials but I can't seem to find the problem. I changed some names just so that it makes me think a little more.
Here is what i have:
Code:
public class digitextractor {
    int number = 0;
    String snumber = "";
    
    public digitextractor(int numberSequence) {
        int number = numberSequence;
    }
    
    public digitextractor(String numberSequence){
        snumber = numberSequence;
    }

    public void returnorder(){
        
        int integer1 = 0;
        int integer2 = 0;
        int integer3 = 0;
        int integer4 = 0;
        int integer5 = 0;
        
        integer1 = (number % 10);
        integer2 = (number % 100) / 10;
        integer3 = (number % 1000) / 100;
        integer4 = (number % 10000) / 1000;
        integer5 = (number % 100000) / 10000;
        
        System.out.println(integer1 + "\n" + integer2 + "\n" +
    integer3 + "\n" + integer4 + "\n" + integer5);
    }
    
    
    public void returnorderThroughString(){
        char part1;
        char part2;
        char part3;
        char part4;
        char part5;
        
        part1 = snumber.charAt(4);
        part2 = snumber.charAt(3);
        part3 = snumber.charAt(2);
        part4 = snumber.charAt(1);
        part5 = snumber.charAt(0);
        
        System.out.println(part1 + "\n" + part2 + "\n" + part3 + "\n" + part4 + "\n" + part5);
    }
}

Code:
import java.util.Scanner;
public class digitfinished {
    
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        // process starts
        System.out.println("Welcome to this thing." + "\n" + "Enter your number");
        String in = input.nextLine();
        digitextractor product = new digitextractor(Integer.parseInt(in));
        product.returnorderThroughString();
        System.out.println("Using Strings");
        digitextractor output = new digitextractor(in);
        output.returnorderThroughString();
    }

}
#2
I fixed the error, but the results aren't what they are supposed to be.
Using this and 54321 as an input I get this:
Welcome to this thing.
Enter your number
54321
Your Numbers:
0
0
0
0
0
Your Values
1
2
3
4
5

New code:
Code:
import java.util.Scanner;
public class digitfinished {
    
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        // process starts
        System.out.println("Welcome to this thing." + "\n" + "Enter your number");
        String in = input.nextLine();
        digitextractor product = new digitextractor(Integer.parseInt(in));
        //New Instance
        
        /*problem, used product.returnorderThroughString instead of
         * using product.returnorder
        */
        product.returnorder();
        System.out.println("Your Values");
        digitextractor output = new digitextractor(in);
        output.returnorderThroughString();
        //End Instance
    }

}
Code:
public class digitextractor {
    int number = 0;
    String snumber = "";
    
    public digitextractor(int numberSequence) {
        int number = numberSequence;
    }
    
    public digitextractor(String numberSequence){
        snumber = numberSequence;
    }

    public void returnorder(){
        //This line is not part of tutorial
        System.out.println("Your Numbers:");
        
        int integer1 = 0;
        int integer2 = 0;
        int integer3 = 0;
        int integer4 = 0;
        int integer5 = 0;
        
        integer1 = (number % 10);
        integer2 = (number % 100) / 10;
        integer3 = (number % 1000) / 100;
        integer4 = (number % 10000) / 1000;
        integer5 = (number % 100000) / 10000;
        
        System.out.println(integer1 + "\n" + integer2 + "\n" +
    integer3 + "\n" + integer4 + "\n" + integer5);
    }
    
    
    public void returnorderThroughString(){
        char part1;
        char part2;
        char part3;
        char part4;
        char part5;
        
        part1 = snumber.charAt(4);
        part2 = snumber.charAt(3);
        part3 = snumber.charAt(2);
        part4 = snumber.charAt(1);
        part5 = snumber.charAt(0);
        
        System.out.println(part1 + "\n" + part2 + "\n" + part3 + "\n" + part4 + "\n" + part5);
    }
}


Forum Jump:


Users browsing this thread: 1 Guest(s)