You are on page 1of 5

1a) Write a Java program that reads a line of integers, and then displays each

integer, and the sum of all the integers.

Code:

package javalab;
import java.util.*;
public class IntegerSum
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the integers one after the other: ");
String digit = scan.nextLine();
StringTokenizer token = new StringTokenizer(digit);
int a = 0;
int sum=0;
System.out.println("The integers entered are : ");
while(token.hasMoreTokens())
{
String s = token.nextToken();
a = Integer.parseInt(s);
System.out.print(a+" ");
sum = sum + a;
}
System.out.println();
System.out.println("Sum of the entered integers is: "+sum);
}
}

Output:
1b) Write a Java program to find both the largest and smallest number in a list of
integers.

Code:

package javalab;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Collections;
public class LargestSmallest{
public static void main(String[] args) {
LinkedList<Integer> anj = new LinkedList<Integer>();
anj.add(35);
anj.add(5);
anj.add(16);
anj.add(57);
anj.add(20);
System.out.println("Minimum value of the Linked List is: " + Collections.min(anj));
System.out.println("Maximum value of the Linked List is: " +
Collections.max(anj));
}
}

Output:
3) Write a java program that loads names and phone numbers from a text file
where the data is organized as one line per record and each field in a record are
separated by tab(\t). It takes a name or phone number as input and prints the
corresponding other value from the hash table

Code:

import java.util.*;
import java.io.*;
public class third_one {
public static void main(String[] args) {
try
{
FileInputStream doc = new FileInputStream("C:\\Users\\ANJANA
MAGANTI\\Desktop\\JAVALAB.txt");
Scanner scan = new Scanner(doc).useDelimiter("\\s+");
Hashtable<String, String> hash = new Hashtable<String, String>();
String[] arrayList;
String a;
System.out.println("Hash Table");
System.out.println("Name : MobileNo");
while(scan.hasNext())
{
a = scan.nextLine();
arrayList = a.split("\\s+");
hash.put(arrayList[0], arrayList[1]);
System.out.println(arrayList[0] + ":" + arrayList[1]);
}
System.out.println("Options are:");
System.out.println("1. Search by name");
System.out.println("2. Search by mobileno.");
System.out.println("3. Exit");
String choice = " ";
String name, mobile;
Scanner sc = new Scanner(System.in);
while(choice != "3")
{
System.out.println("Enter your choice: ");
choice = sc.next();
switch(choice)
{
case "1":
{
System.out.println("Enter the name: ");
name = sc.next();
if(hash.containsKey(name))
System.out.println("Mobile number is " +hash.get(name));
else
System.out.println("No such name");
}
break;
case "2":
{
System.out.println("Enter mobile number: ");
mobile = sc.next();
if(hash.containsValue(mobile))
{
for(@SuppressWarnings("rawtypes")Map.Entry e:hash.entrySet())
{
if(mobile.equals(e.getValue()))
System.out.println("Name is " + e.getKey());
}
}
else
System.out.println("Name not found");
}
break;
case "3":
{
choice = "3";
System.out.println("Exited");
}
break;
default:
System.out.println("Choice out of range");
break;
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
File input:

Output:

You might also like