-3

Make a program where you enter a 3-digit integer. The program will then print out what the century, the tens and the singles are.

  • A) Solve the problem with integer division (% and /).

  • B) Solve the task with strings.

This is my homework, I solved the A but need support with B, I displayed A code but it similar to B?

import java.util.Scanner;

public class UppgiftAttaA {

    public static void main(String[] args) {
        int tal = 0;
        int taltio = 0;
        int talen = 0;
        int talhund = 0;
        System.out.println("Skriv ett tal som är större än 100");
        Scanner read = new Scanner(System.in);

        tal = read.nextInt();
        talen = (tal / 1) % 10;
        System.out.println("en tal är " + talen * 1);
        taltio = (tal / 10) % 10;
        System.out.println("tio tal är " + taltio * 10);

        talhund = (tal / 100) % 10;
        System.out.println("hundra tal är " + talhund * 100);
    }
}
jhamon
  • 3,603
  • 4
  • 26
  • 37
Adam Lord
  • 35
  • 9
  • Well, solving the task with strings would mean to get and interpret each character in the string, i.e. `"123"` consists of the characters `'1','2','3'` - the position defines whether it's hundreds, tens or single digits. – Thomas Aug 30 '19 at 08:49
  • @thomas, or you keep the the same code but convert String to int. It's unclear – jhamon Aug 30 '19 at 08:50
  • Also note: this site is English only. Even having "other languages" used inside your code isn't a good idea. Your code is what the readers will look at to understand what you are doing. Using a different language there makes it much harder. – GhostCat Aug 30 '19 at 08:51
  • @jhamon that could be one way to do it but my guess is that the goal is to teach the OP a thing or two about how to use individual characters. It seems as if the difference between `a)` and `b)` is meant to be something more than just a `Integer.parseInt()`. – Thomas Aug 30 '19 at 08:55

1 Answers1

0

You can use String.valueOf(int whatever) to make the integer into a String. Then use String.charAt(int index) to get the character at indices 0,1 and 2.

You should really get used to using the Java doc. It will give you all the necessary functions for various conversions and manipulations. Reading it will help you get familiar to this essential resource. Try to do your own research before posting on Stack Overflow.

the_ritz
  • 322
  • 2
  • 17