Add 1st and Last digit of a number - java

I am trying to add the 1st and last digit of a no but after the programs gets to while loop it starts asking for more inputs,like an infinite loop .
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();//no of testcases
int n[]=new int[t];
int last=0,first = 0;
for(int i=0;i<t;i++){
n[i]=sc.nextInt();
sc.nextLine();
last=n[i]%10;
while(n[i]>=10){
first=n[i]/10;
}
System.out.println(first+last);
}

This is how I did it.
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String temp = t+"";
String[] arr = temp.split("");
int x = Integer.parseInt(arr[0]) + Integer.parseInt(arr[arr.length - 1]);
System.out.println(x);
}

That is because you are not updating the value of n[i] with every iteration.
This is my proposed solution for your while loop:
while(n[i]>=10){
n[i]=n[i]/10;
}
first = n[i]

In while loop you need to update the value of n[i] by using n[i]=n[i]/10; which is causing while loop to become infinite loop
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();//no of testcases
int n[]=new int[t];
int last=0,first = 0;
for(int i=0;i<t;i++){
n[i]=sc.nextInt();
sc.nextLine();
last=n[i]%10;
while(n[i]>=10){
n[i]=n[i]/10;
}
first=n[i]/10;
System.out.println(first+last);
}

Related

Java: Declaring array with do... while

My school homework is to declare array with 100 variables.
The actual task is: Declare array with 100 variables. Use do.. while loop to read the data to array. Reading data should be finished when array will be full or when user will enter a negative number.
So far I got:
public static void runTask1() {
Scanner read = new Scanner(System.in);
int[] tab = new int [100];
for (int i = 0; i < tab.length; i++);
System.out.println("Enter number for array ");
tab [] = read.nextInt();
Please help. I'm a total newbie in programming.
You should do your homework yourself ;)
Scanner read = new Scanner(System.in);
int[] tab = new int [100];
int idx=0;
do{
System.out.println("Number for array idx "+idx);
try{
tab[idx] = read.nextInt();
}catch(Exception e){
System.out.println("Wrong input");
}
if(tab[idx]<0) break;
idx++;
}while(idx<100)
Not compiled, just wrote it here.
Try that
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int[] tab = new int [100];
int index = 0;
while(index < tab.length){
System.out.println("Enter number for array ");
tab[index]= read.nextInt();
if(tab[index]<1) break;
index++;
}
System.out.println(Arrays.toString(tab));
}

Ending while loop in Java

I'm making a program for my assignment. This is not the whole program but it's just a part of it.
I want from the user to enter some integer values to be stored in "items" arrays. When the user input "stop" the loop should close and here is the problem.. when I write stop the program stops and give me some errors.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i=0, lines=1;
int[] items = new int[100];
int total = 0;
System.out.println("Enter the items with its price");
while(true){
i=i+1;
if ("stop".equals(scan.nextLine()))
break;
else
items[i] = scan.nextInt();
}
}
There are certain mistakes in your code. It's more better if you could just add the error.
Try this code.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = 0, lines = 1;
int[] items = new int[100];
int total = 0;
System.out.println("Enter the items with its price");
while(true){
String InputTxt = scan.nextLine();
if (InputTxt.equals("stop"))
break;
else{
try{
items[i] = Integer.parseInt(InputTxt);
i++;
}catch(Exception e){
System.out.println("Please enter a number");
}
}
}
}
On top of other answers, I would like to advise you to change the looping from
while(true)
to
//first you need to remove the local variable i
for(int i = 0; i < items.length; ++i)
Using this approach will help you to avoid IndexOutOfBoundsException when users key in more than 100 integer values.
your problem is this line : items[i] = scan.nextInt(); because you are trying to get integer while the input is string stop
EDIT
one possible solution is that you get your data as string and check if it is stop or not and if not then try to parse it to integer like code bellow:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i=0, lines=1;
int[] items = new int[100];
int total = 0;
System.out.println("Enter the items with its price");
while(true)
{
i=i+1;
String str = scan.nextLine()
if ("stop".equals(str))
break;
else
{
items[i] = Integer.parseInt(str)
}
}
}

Counting and resetting a Scanner

I'm an amateur with Java and am trying to put the contents of a scanner into an array. This is what I have currently:
`
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int l = scannerLength(sc);
sc.close(); sc = null; sc = new Scanner(System.in);
int[] input = new int[l];
for (int i = 0; i < l; i++) {
input[i] = sc.nextInt();
System.out.println(input[i]);
}
}
private static int scannerLength(Scanner sc) {
int output = 0;
while(sc.hasNextInt()) {
output++;
sc.nextInt();
}
return output;
}
`
I am trying to effectively 'reset' the Scanner after my helper function determines its length by voiding it and declaring it anew, so I can use nextInt to put the elements into an array. But I get a NoSuchElementException at line 8.
Can anyone tell me why this is? I would think, after resetting the Scanner, that sc.nextInt() at line 8 wouldn't pose a problem.

Runtime Exception- feature not implemented yet

The following block of code showing this error "Runtime Exception- feature not implemented yet" in jelliot, when it reaches the line
char arr [] = w.toCharArray();
And in other compilers,it won't take the number of input it's supposed to take. If I set n=4, it only takes 2 inputs.
Scanner Sc = new Scanner (System.in);
int n = Sc.nextInt();
for(int count = 1;count<=n; count++){
String w = Sc.nextLine();
char arr [] = w.toCharArray();
if(arr.length > 4){
System.out.print(arr[0]);
System.out.print(arr.length-2);
System.out.print(arr[arr.length-1]);
}
else{
System.out.println(w);
}
System.out.println();
}
The exception indicates that this particular method (toCharArray()) of the String class isn't implemented for the (presumably non-standard) implementation of java that you're using. You can work around this by not using a char array, and instead using the String methods length() and charAt(). Try this:
Scanner Sc = new Scanner (System.in);
int n = Sc.nextInt();
for(int count = 1;count<=n; count++){
String w = Sc.nextLine();
if(w.length() > 4){
System.out.print(w.charAt(0));
System.out.print(w.length()-2);
System.out.print(w.chatAt(w.length()-1));
}
else{
System.out.println(w);
}
System.out.println();
}

How to multiply a String by an Integer?

I know there's some way to change a string into an integer but it's not really working out for me when I try to do it.
I was asked to take in an integer 'n' and 'a' string 's' and print 's' 'n' times
Here's my code and my main question / question is how do I easily turn the string into an integer so I can multiply the two together:
public static void main(String args[])
{
System.out.println("Enter the number of times you want to print a string");
Scanner n = new Scanner(System.in);
int x = n.nextInt();
System.out.println("Enter the string you want printed");
Scanner y = new Scanner(System.in);
String s = y.nextLine();
}
You only need one Scanner, and if I understand your question then you might use a loop like, also an int n.
System.out.println("Enter the number of times you want to "
+ "print a string");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println("Enter the string you want printed");
String s = scan.nextLine();
for (int i = 0; i < n; i++) {
System.out.print(s);
}
System.out.println();
Of course, you could put the loop in a method like
private static String multiply(String str, int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(str);
}
return sb.toString();
}
Then you could call it like,
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println("Enter the string you want printed");
String s = scan.nextLine();
System.out.println(multiply(s, n));

Categories

Resources